'How to list incoming emails from a special account in ASP.NET Core
I am using MailKit to receive emails.
Here is my code:
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);
}
}
}
The problem is that the above code gets all emails in a loop and then searches for the email messages received from an special account. Is there any way to list the email messages received from the account without getting all email messages in the for loop?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
