public AeEmailClient(IConfiguration configuration) { configuration = configuration.GetSubsection("EmailClient"); client = new ImapClient( configuration.GetValue("Host"), configuration.GetValue("Username"), configuration.GetValue("Password"), ImapClient.AuthMethods.Login, configuration.GetValue<int>("Port"), configuration.GetValue<bool>("Secure"), configuration.GetValue<bool>("SkipSslValidation")); var unreadMessages = client.SearchMessages(SearchCondition.Unseen()); foreach (var message in unreadMessages) { client.SetFlags(Flags.Seen, message.Value); } unread.AddRange(unreadMessages.Select(message => GetInputMailMessage(message.Value))); client.NewMessage += (sender, args) => { var message = client.GetMessage(args.MessageCount - 1, false, true); client.SetFlags(Flags.Seen, message); unread.Add(GetInputMailMessage(message)); if (null != MailMessageReceived) { MailMessageReceived(this, args); } }; }