Search code examples
c#visual-studiogmailimap

Reading Emails from Gmail C# - IMAP


I'm trying to read emails from specific Gmail account. I have found something here(Stackoverflow) but I can't manage reading the emails.

this is what I'm using:

 public static void logingmail()
    {
        // Connect to the IMAP server. The 'true' parameter specifies to use SSL
        // which is important (for Gmail at least)
        ImapClient ic = new ImapClient("imap.gmail.com", "[email protected]", "4521945219",AuthMethods.Login, 993, true);
        // Select a mailbox. Case-insensitive
        ic.SelectMailbox("Inbox");
       string countmessages = ic.GetMessageCount().ToString();
        // Get the first *11* messages. 0 is the first message;
        // and it also includes the 10th message, which is really the eleventh ;)
        // MailMessage represents, well, a message in your mailbox
        MailMessage[] mm = ic.GetMessages(0, 10);
        foreach (MailMessage m in mm)
        {

            var subject = m.Subject.ToString();
        }
        // Probably wiser to use a using statement
        ic.Dispose();

    }

The problem I'm experiencsing probably happening when I'm first creating the new ImapCliient class. for some reason it's opening a browse path to choose a file(?). I'll be happy for some assistance. Thanks


Solution

  • You should use the solution of the second post of the topic were you have found something, S22.Imap. (Here you can find a compiled version).

    After download, you can read the documentation. It is simple and readable.

    Actually, I use this library and that works well !