简体   繁体   中英

How to read mail content through c# console application

MailRepository rep = new MailRepository("imap.mail.yahoo.com", 993, true, @"xxxxx@yahoo.com", "*******");
foreach (Message email in rep.GetUnreadMails("Inbox"))
{
    //Console.WriteLine(string.Format("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text));
    Console.WriteLine(email.From);
    Console.WriteLine(email.Subject);
    Console.WriteLine(email.BodyHtml.Text);
    if (email.Attachments.Count > 0)
    {
        foreach (MimePart attachment in email.Attachments)
        {
            Console.WriteLine(string.Format("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType));
        }
    }
}

Above is my code, which is used to read mail content. It's working fine when i tryed for gmail port, but while going for yahoo or some other. It's not allowing me to read the mail throwing exception. Is there any other source . Please guide me

First , check your credentials are correct.

Second , put a try catch in the constructor to see if you can get more info about the unhandled exception:

public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
  try {
    if (ssl) {
      Client.ConnectSsl(mailServer, port);
    }
    else {
      Client.Connect(mailServer, port);
    }
  Client.Login(login, password);
  }
  catch(Exception ex)
  {
     //Check the exception details here
  }
}

Third , the origin of the MailRepository class appears to be from here that uses the Imap4Client implementation which others have complained doesn't work with Yahoo: Connecting to yahoo email with IMAP4 MailSystem.NET

The accepted answer recommends using ImapX 2 - crossplatform IMAP library for .NET to handle GMail, Yahoo, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM