C# POP3、IMAP、SMTP收发邮件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lesnikowski.Client;
using Lesnikowski.Client.IMAP;
using Lesnikowski.Mail;
using Lesnikowski.Mail.Fluent;
using Lesnikowski.Mail.Headers;
using Lesnikowski.Mail.Headers.Constants;

namespace Email
{
    public class Class1
    {
       public void POP3_Subject_Message(String User, String Password,String Server)
       {
          Pop3 pop3 = new Pop3();
          pop3.Connect(Server);              
          pop3.User = User;
          pop3.Password = Password;
          pop3.Login();
          pop3.GetAccountStat();                      
          SimpleMailMessageBuilder builder = new SimpleMailMessageBuilder();
          for (int i = 1; i <= pop3.MessageCount; i++)
          { ISimpleMailMessage mail = builder.CreateFromEml(pop3.GetMessage(i));
            Console.WriteLine(mail.Subject);
          }
           pop3.Close(true);
        }

       public void IMAP_Subject_Message(String User, String Password, String Server)
       {
           Imap imap = new Imap();
           imap.Connect(Server);

           imap.User = User;
           imap.Password = Password;
           imap.Login();

           imap.SelectInbox();
           List uidList = imap.SearchFlag(Flag.Unseen);
           foreach (long uid in uidList)
           {
               ISimpleMailMessage mail = new SimpleMailMessageBuilder()
                   .CreateFromEml(imap.GetMessageByUID(uid));
               Console.WriteLine(mail.Subject);
           }
           imap.Close(true);
        }

       public void SMTP_Send_Message(String User, String Password, String Server)
       {
           ISimpleMailMessage message = Mail
           .Html(@"Html with an image: ")
           .AddVisual(@"c:\lena.jpeg").SetContentId("lena")
           .AddAttachment(@"c:\tmp.doc").SetFileName("document.doc")
           .To("[email protected]")
           .From("[email protected]")
           .Subject("Subject")
           .Create();
           Smtp smtp = new Smtp();
           smtp.Connect(Server);
           smtp.Ehlo(HeloType.EhloHelo, "yourname");

           smtp.User = User;
           smtp.Password = Password;
           smtp.Login();

           smtp.SendMessage(message);

           smtp.Close(true);   
       }
     }
}


你可能感兴趣的:(C# POP3、IMAP、SMTP收发邮件)