如何在 ASP.NET Core 中列出来自特殊帐户的传入电子邮件
How to list incoming emails from a special account in ASP.NET Core
我正在使用 MailKit 接收电子邮件。
这是我的代码:
using (var emailClient = new Pop3Client())
{
emailClient.Connect(settings.HostMail, 110);
emailClient.Authenticate(settings.UsernameEmail, settings.PasswordEmail);
List<EmailMessageDTO> emails = new List<EmailMessageDTO>();
for (int i = 0; i < emailClient.Count; i++)
{
var message = emailClient.GetMessage(i);
var emailMessage = new EmailMessageDTO();
emailMessage.MessageHtml = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody;
emailMessage.Subject = message.Subject;
emailMessage.To = new List<DTO.Address>();
emailMessage.To.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new DTO.Address { Email = x.Address, Name = x.Name }));
var sender = message.From.Select(x => (MailboxAddress)x).Select(x => new DTO.Address { Email = x.Address, Name = x.Name }).FirstOrDefault();
emailMessage.From = new DTO.Address { Email = sender.Email, Name = sender.Name };
if(emailMessage.From.Name == "Ali")
{
emails.Add(emailMessage);
}
}
}
问题是上面的代码循环获取所有邮件,然后搜索从一个特殊帐户收到的电子邮件。有什么方法可以列出从帐户收到的电子邮件,而无需在 for
循环中获取所有电子邮件?
POP3协议非常基础,不支持任何方式的搜索。 IMAP 协议对于你想做的事情来说是一个更好的协议,但如果你没有那个选项,你可以(可能)通过只下载 headers 来提高你的应用程序的网络性能,以便做你的过滤,如果匹配,则下载完整消息。
例如:
for (int i = 0; i < emailClient.Count; i++)
{
var headers = emailClient.GetMessageHeaders(i);
if (!headers.TryGetValue (HeaderId.From, out var header))
continue;
if (!InternetAddressList.TryParse (header.RawValue, out var addrList))
continue;
var sender = addrList.Mailboxes.FirstOrDefault ();
if (sender == null || sender.Name != "Ali")
continue;
var message = emailClient.GetMessage(i);
var emailMessage = new EmailMessageDTO();
emailMessage.MessageHtml = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody;
emailMessage.Subject = message.Subject;
emailMessage.To = new List<DTO.Address>();
emailMessage.To.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new DTO.Address { Email = x.Address, Name = x.Name }));
emailMessage.From = new DTO.Address { Email = sender.Address, Name = sender.Name };
emails.Add(emailMessage);
}
我正在使用 MailKit 接收电子邮件。
这是我的代码:
using (var emailClient = new Pop3Client())
{
emailClient.Connect(settings.HostMail, 110);
emailClient.Authenticate(settings.UsernameEmail, settings.PasswordEmail);
List<EmailMessageDTO> emails = new List<EmailMessageDTO>();
for (int i = 0; i < emailClient.Count; i++)
{
var message = emailClient.GetMessage(i);
var emailMessage = new EmailMessageDTO();
emailMessage.MessageHtml = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody;
emailMessage.Subject = message.Subject;
emailMessage.To = new List<DTO.Address>();
emailMessage.To.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new DTO.Address { Email = x.Address, Name = x.Name }));
var sender = message.From.Select(x => (MailboxAddress)x).Select(x => new DTO.Address { Email = x.Address, Name = x.Name }).FirstOrDefault();
emailMessage.From = new DTO.Address { Email = sender.Email, Name = sender.Name };
if(emailMessage.From.Name == "Ali")
{
emails.Add(emailMessage);
}
}
}
问题是上面的代码循环获取所有邮件,然后搜索从一个特殊帐户收到的电子邮件。有什么方法可以列出从帐户收到的电子邮件,而无需在 for
循环中获取所有电子邮件?
POP3协议非常基础,不支持任何方式的搜索。 IMAP 协议对于你想做的事情来说是一个更好的协议,但如果你没有那个选项,你可以(可能)通过只下载 headers 来提高你的应用程序的网络性能,以便做你的过滤,如果匹配,则下载完整消息。
例如:
for (int i = 0; i < emailClient.Count; i++)
{
var headers = emailClient.GetMessageHeaders(i);
if (!headers.TryGetValue (HeaderId.From, out var header))
continue;
if (!InternetAddressList.TryParse (header.RawValue, out var addrList))
continue;
var sender = addrList.Mailboxes.FirstOrDefault ();
if (sender == null || sender.Name != "Ali")
continue;
var message = emailClient.GetMessage(i);
var emailMessage = new EmailMessageDTO();
emailMessage.MessageHtml = !string.IsNullOrEmpty(message.HtmlBody) ? message.HtmlBody : message.TextBody;
emailMessage.Subject = message.Subject;
emailMessage.To = new List<DTO.Address>();
emailMessage.To.AddRange(message.To.Select(x => (MailboxAddress)x).Select(x => new DTO.Address { Email = x.Address, Name = x.Name }));
emailMessage.From = new DTO.Address { Email = sender.Address, Name = sender.Name };
emails.Add(emailMessage);
}