Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using the library from: <a href="https://github.com/pmengal/MailSystem.NET" rel="noreferrer">https://github.com/pmengal/MailSystem.NET</a></p> <p>Here is my complete code sample:</p> <h2>Email Repository</h2> <pre><code>using System.Collections.Generic; using System.Linq; using ActiveUp.Net.Mail; namespace GmailReadImapEmail { public class MailRepository { private Imap4Client client; public MailRepository(string mailServer, int port, bool ssl, string login, string password) { if (ssl) Client.ConnectSsl(mailServer, port); else Client.Connect(mailServer, port); Client.Login(login, password); } public IEnumerable&lt;Message&gt; GetAllMails(string mailBox) { return GetMails(mailBox, "ALL").Cast&lt;Message&gt;(); } public IEnumerable&lt;Message&gt; GetUnreadMails(string mailBox) { return GetMails(mailBox, "UNSEEN").Cast&lt;Message&gt;(); } protected Imap4Client Client { get { return client ?? (client = new Imap4Client()); } } private MessageCollection GetMails(string mailBox, string searchPhrase) { Mailbox mails = Client.SelectMailbox(mailBox); MessageCollection messages = mails.SearchParse(searchPhrase); return messages; } } } </code></pre> <h2>Usage</h2> <pre><code>[TestMethod] public void ReadImap() { var mailRepository = new MailRepository( "imap.gmail.com", 993, true, "yourEmailAddress@gmail.com", "yourPassword" ); var emailList = mailRepository.GetAllMails("inbox"); foreach (Message email in emailList) { Console.WriteLine("&lt;p&gt;{0}: {1}&lt;/p&gt;&lt;p&gt;{2}&lt;/p&gt;", email.From, email.Subject, email.BodyHtml.Text); if (email.Attachments.Count &gt; 0) { foreach (MimePart attachment in email.Attachments) { Console.WriteLine("&lt;p&gt;Attachment: {0} {1}&lt;/p&gt;", attachment.ContentName, attachment.ContentType.MimeType); } } } } </code></pre> <hr> <h2>Another example, this time using <a href="https://github.com/jstedfast/MailKit" rel="noreferrer">MailKit</a></h2> <pre><code>public class MailRepository : IMailRepository { private readonly string mailServer, login, password; private readonly int port; private readonly bool ssl; public MailRepository(string mailServer, int port, bool ssl, string login, string password) { this.mailServer = mailServer; this.port = port; this.ssl = ssl; this.login = login; this.password = password; } public IEnumerable&lt;string&gt; GetUnreadMails() { var messages = new List&lt;string&gt;(); using (var client = new ImapClient()) { client.Connect(mailServer, port, ssl); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate(login, password); // The Inbox folder is always available on all IMAP servers... var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); var results = inbox.Search(SearchOptions.All, SearchQuery.Not(SearchQuery.Seen)); foreach (var uniqueId in results.UniqueIds) { var message = inbox.GetMessage(uniqueId); messages.Add(message.HtmlBody); //Mark message as read //inbox.AddFlags(uniqueId, MessageFlags.Seen, true); } client.Disconnect(true); } return messages; } public IEnumerable&lt;string&gt; GetAllMails() { var messages = new List&lt;string&gt;(); using (var client = new ImapClient()) { client.Connect(mailServer, port, ssl); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate(login, password); // The Inbox folder is always available on all IMAP servers... var inbox = client.Inbox; inbox.Open(FolderAccess.ReadOnly); var results = inbox.Search(SearchOptions.All, SearchQuery.NotSeen); foreach (var uniqueId in results.UniqueIds) { var message = inbox.GetMessage(uniqueId); messages.Add(message.HtmlBody); //Mark message as read //inbox.AddFlags(uniqueId, MessageFlags.Seen, true); } client.Disconnect(true); } return messages; } } </code></pre> <h2>Usage</h2> <pre><code>[Test] public void GetAllEmails() { var mailRepository = new MailRepository("imap.gmail.com", 993, true, "YOUREMAILHERE@gmail.com", "YOURPASSWORDHERE"); var allEmails = mailRepository.GetAllMails(); foreach(var email in allEmails) { Console.WriteLine(email); } Assert.IsTrue(allEmails.ToList().Any()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload