在C#中从Gmail读取电子邮件

前端之家收集整理的这篇文章主要介绍了在C#中从Gmail读取电子邮件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从Gmail读取电子邮件.我已经尝试过我可以找到的每个API /开源项目,不能让任何一个工作.

有没有人有一个工作代码的样本,可以让我从Gmail帐户验证和下载电子邮件

最终工作版本如下:https://stackoverflow.com/a/19570553/550198

解决方法

使用库: http://mailsystem.codeplex.com/

以下是我的完整代码示例:

电子邮件存储库

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using ActiveUp.Net.Mail;
  4.  
  5. namespace GmailReadImapEmail
  6. {
  7. public class MailRepository
  8. {
  9. private Imap4Client client;
  10.  
  11. public MailRepository(string mailServer,int port,bool ssl,string login,string password)
  12. {
  13. if (ssl)
  14. Client.ConnectSsl(mailServer,port);
  15. else
  16. Client.Connect(mailServer,port);
  17. Client.Login(login,password);
  18. }
  19.  
  20. public IEnumerable<Message> GetAllMails(string mailBox)
  21. {
  22. return GetMails(mailBox,"ALL").Cast<Message>();
  23. }
  24.  
  25. public IEnumerable<Message> GetUnreadMails(string mailBox)
  26. {
  27. return GetMails(mailBox,"UNSEEN").Cast<Message>();
  28. }
  29.  
  30. protected Imap4Client Client
  31. {
  32. get { return client ?? (client = new Imap4Client()); }
  33. }
  34.  
  35. private MessageCollection GetMails(string mailBox,string searchPhrase)
  36. {
  37. MailBox mails = Client.SelectMailBox(mailBox);
  38. MessageCollection messages = mails.SearchParse(searchPhrase);
  39. return messages;
  40. }
  41. }
  42. }

用法

  1. [TestMethod]
  2. public void ReadImap()
  3. {
  4. var mailRepository = new MailRepository(
  5. "imap.gmail.com",993,true,"yourEmailAddress@gmail.com","yourPassword"
  6. );
  7.  
  8. var emailList = mailRepository.GetAllMails("inBox");
  9.  
  10. foreach (Message email in emailList)
  11. {
  12. Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>",email.From,email.Subject,email.BodyHtml.Text);
  13. if (email.Attachments.Count > 0)
  14. {
  15. foreach (MimePart attachment in email.Attachments)
  16. {
  17. Console.WriteLine("<p>Attachment: {0} {1}</p>",attachment.ContentName,attachment.ContentType.MimeType);
  18. }
  19. }
  20. }
  21. }

另一个例子,这次使用MailKit

  1. public class MailRepository : IMailRepository
  2. {
  3. private readonly string mailServer,login,password;
  4. private readonly int port;
  5. private readonly bool ssl;
  6.  
  7. public MailRepository(string mailServer,string password)
  8. {
  9. this.mailServer = mailServer;
  10. this.port = port;
  11. this.ssl = ssl;
  12. this.login = login;
  13. this.password = password;
  14. }
  15.  
  16. public IEnumerable<string> GetUnreadMails()
  17. {
  18. var messages = new List<string>();
  19.  
  20. using (var client = new ImapClient())
  21. {
  22. client.Connect(mailServer,port,ssl);
  23.  
  24. // Note: since we don't have an OAuth2 token,disable
  25. // the XOAUTH2 authentication mechanism.
  26. client.AuthenticationMechanisms.Remove("XOAUTH2");
  27.  
  28. client.Authenticate(login,password);
  29.  
  30. // The InBox folder is always available on all IMAP servers...
  31. var inBox = client.InBox;
  32. inBox.Open(FolderAccess.ReadOnly);
  33. var results = inBox.Search(SearchOptions.All,SearchQuery.Not(SearchQuery.Seen));
  34. foreach (var uniqueId in results.UniqueIds)
  35. {
  36. var message = inBox.GetMessage(uniqueId);
  37.  
  38. messages.Add(message.HtmlBody);
  39.  
  40. //Mark message as read
  41. //inBox.AddFlags(uniqueId,MessageFlags.Seen,true);
  42. }
  43.  
  44. client.Disconnect(true);
  45. }
  46.  
  47. return messages;
  48. }
  49.  
  50. public IEnumerable<string> GetAllMails()
  51. {
  52. var messages = new List<string>();
  53.  
  54. using (var client = new ImapClient())
  55. {
  56. client.Connect(mailServer,SearchQuery.NotSeen);
  57. foreach (var uniqueId in results.UniqueIds)
  58. {
  59. var message = inBox.GetMessage(uniqueId);
  60.  
  61. messages.Add(message.HtmlBody);
  62.  
  63. //Mark message as read
  64. //inBox.AddFlags(uniqueId,true);
  65. }
  66.  
  67. client.Disconnect(true);
  68. }
  69.  
  70. return messages;
  71. }
  72. }

用法

  1. [Test]
  2. public void GetAllEmails()
  3. {
  4. var mailRepository = new MailRepository("imap.gmail.com","YOUREMAILHERE@gmail.com","YOURPASSWORDHERE");
  5. var allEmails = mailRepository.GetAllMails();
  6.  
  7. foreach(var email in allEmails)
  8. {
  9. Console.WriteLine(email);
  10. }
  11.  
  12. Assert.IsTrue(allEmails.ToList().Any());
  13. }

猜你在找的C#相关文章