我一直在尝试阅读来自客户电子邮件的网络邮件版本的电子邮件。在互联网上搜索了很多。我正在使用我在网上找到的代码
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
using System;
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<Message> GetAllMails(string mailBox) {
return GetMails(mailBox, "ALL").Cast < Message > ();
}
public IEnumerable<Message> GetUnreadMails(string mailBox) {
return GetMails(mailBox, "UNSEEN").Cast < Message > ();
}
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;
}
public static void Main() {
var mailRepository = new MailRepository(
"outlook.office365.com",
143,
true,
"username",
"password"
);
var emailList = mailRepository.GetAllMails("inbox");
foreach(Message email in emailList)
{
Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0) {
foreach(MimePart attachment in email.Attachments)
{
Console.WriteLine("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
}
}
}
}
}
}
我在Client.ConnectSsl(邮件服务器,端口)中调整异常 例外情况是:无法建立连接,因为目标计算机主动拒绝了