【问题标题】:How to read mail content through c# console application如何通过 c# 控制台应用程序读取邮件内容
【发布时间】:2016-07-21 05:51:55
【问题描述】:
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));
        }
    }
}

以上是我的代码,用于读取邮件内容。当我尝试使用 gmail 端口时,它工作正常,但在使用 yahoo 或其他的时候。它不允许我阅读邮件抛出异常。有没有其他来源。请指导我

【问题讨论】:

  • 它抛出的异常的细节是什么?
  • 有时未处理的异常,有时用户和密码不正确

标签: c# console-application imap yahoo-mail


【解决方案1】:

首先,检查您的凭据是否正确。

其次,在构造函数中放一个try catch,看看能不能得到更多关于未处理异常的信息:

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
  }
}

第三MailRepository 类的起源似乎来​​自 here,它使用了其他人抱怨的 Imap4Client 实现不适用于 Yahoo: Connecting to yahoo email with IMAP4 MailSystem.NET

接受的答案建议使用ImapX 2 - crossplatform IMAP library for .NET 处理GMail、Yahoo 等。

【讨论】:

  • 感谢@jeremy Thompson,我在我的代码中尝试了catch,这里只有我忽略了。我会试试这个参考
猜你喜欢
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-18
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多