/// <summary> /// POP3接收邮件 /// </summary> /// <param name="FromAccount">邮箱账户</param> /// <param name="FromPassword">邮箱密码</param> /// <returns></returns> public static IList <ReceiveEmailModel> ReceivePop3Email(string FromAccount, string FromPassword) { List <ReceiveEmailModel> receiveEmailList = new List <ReceiveEmailModel>(); using (var client = new Pop3Client()) { // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(PopServer, PopPort, false); client.Authenticate(FromAccount, FromPassword); for (int i = 0; i < client.Count; i++) { var message = client.GetMessage(i); ReceiveEmailModel receiveEmailModel = new ReceiveEmailModel(); receiveEmailModel.FromAddress = message.From[0].Name; receiveEmailModel.Date = message.Date.DateTime; receiveEmailModel.Subject = message.Subject; receiveEmailModel.Body = message.TextBody; receiveEmailList.Add(receiveEmailModel); } client.Disconnect(true); } return(receiveEmailList); }
/// <summary> /// IMAP接收邮件 /// </summary> /// <param name="FromAccount">邮箱账户</param> /// <param name="FromPassword">邮箱密码</param> /// <returns></returns> public static IList <ReceiveEmailModel> ReceiveImapEmail(string FromAccount, string FromPassword) { List <ReceiveEmailModel> receiveEmailList = new List <ReceiveEmailModel>(); using (var client = new ImapClient()) { // For demo-purposes, accept all SSL certificates client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(ImapServer, ImapPort, true); client.Authenticate(FromAccount, FromPassword); // The Inbox folder is always available on all IMAP servers... var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); //Console.WriteLine("Total messages: {0}", inbox.Count); //Console.WriteLine("Recent messages: {0}", inbox.Recent); for (int i = 0; i < inbox.Count; i++) { var message = inbox.GetMessage(i); ReceiveEmailModel receiveEmailModel = new ReceiveEmailModel(); receiveEmailModel.FromAddress = message.From[0].Name; receiveEmailModel.Date = message.Date.DateTime; receiveEmailModel.Subject = message.Subject; receiveEmailModel.Body = message.TextBody; receiveEmailList.Add(receiveEmailModel); } client.Disconnect(true); } return(receiveEmailList); }