} // End Sub TestPop3 public static void TestImap(string username, string password) { using (MailKit.MailStore client = new MailKit.Net.Imap.ImapClient()) { // For demo-purposes, accept all SSL certificates client.ServerCertificateValidationCallback = (s, c, h, e) => true; // Non-Encrypted: 143 // SSL/TLS: 993 client.Connect(RedmineMailService.Trash.UserData.IMAP, 993, true); // Note: since we don't have an OAuth2 token, disable // the XOAUTH2 authentication mechanism. client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate(username, password); // The Inbox folder is always available on all IMAP servers... MailKit.IMailFolder inbox = client.Inbox; inbox.Open(MailKit.FolderAccess.ReadOnly); System.Console.WriteLine("Total messages: {0}", inbox.Count); System.Console.WriteLine("Recent messages: {0}", inbox.Recent); for (int i = 0; i < inbox.Count; i++) { MimeKit.MimeMessage message = inbox.GetMessage(i); System.Console.WriteLine("Subject: {0}", message.Subject); } // Next i client.Disconnect(true); } // End Using client } // End Sub TestImap
// Typically, SMTP uses port 25. However, an alternative SMTP "submission" port has been reserved on port 587. // For Exchange 2007 and 2010, installation will create a "Default" module listening on port 25 as well as a // "Client" module listening on port 587. // https://stackoverflow.com/questions/38747822/accessing-exchange-shared-folders-using-mailkit public static void ExchangeShared() { string userName = "******"; // The email address that has permissions to the shared mailbox string sharedMailboxAlias = "aliasName"; // This is the alias name as setup in Exchange string password = ""; using (MailKit.MailStore client = new MailKit.Net.Imap.ImapClient()) { client.Connect("outlook.office365.com", 993, true); client.AuthenticationMechanisms.Remove("XOAUTH2"); client.Authenticate(userName + @"\" + sharedMailboxAlias, password); MailKit.IMailFolder inbox = client.Inbox; inbox.Open(MailKit.FolderAccess.ReadOnly); System.Console.WriteLine("Total messages: {0}", inbox.Count); System.Console.WriteLine("Recent messages: {0}", inbox.Recent); client.Disconnect(true); } }
public void PickupEmails() { // Reference to a directory string directory = "C:\\DATA\\home\\AnnytabDoxStandards\\Open\\"; // Create and use an imap client using (var client = new MailKit.Net.Imap.ImapClient()) { // Add credentials client.Connect(this.options.Host, 993, true); client.Authenticate(this.options.Pickup, this.options.Password); // Get inbox folder MailKit.IMailFolder inbox = client.Inbox; inbox.Open(MailKit.FolderAccess.ReadWrite); // Write information about the inbox Console.WriteLine("Total messages: {0}", inbox.Count); Console.WriteLine("Recent messages: {0}", inbox.Recent); // Create a query MailKit.Search.SearchQuery query = MailKit.Search.SearchQuery.NotDeleted.And(MailKit.Search.SearchQuery.NotSeen); // Loop messages foreach(MailKit.UniqueId uid in inbox.Search(query)) { // Get the message var message = inbox.GetMessage(uid); // Print subject Console.WriteLine("Subject: {0}", message.Subject); // Get attachments foreach (var attachment in message.Attachments) { // Get information about the attachment var file_name = attachment.ContentDisposition?.FileName; file_name = string.IsNullOrEmpty(file_name) ? Guid.NewGuid().ToString() + ".noname" : Guid.NewGuid().ToString() + Tools.GetExtensions(file_name); var content_type = attachment.ContentType; // Only accept files that ends with dox.zip if(file_name.EndsWith(".dox.zip") == true) { // Save the attachment to disk using (FileStream stream = new FileStream(directory + file_name, FileMode.Create)) { if (attachment is MimeKit.MessagePart) { var part = (MimeKit.MessagePart)attachment; part.Message.WriteTo(stream); } else { var part = (MimeKit.MimePart)attachment; part.Content.DecodeTo(stream); } } } } // Flag email as seen inbox.AddFlags(uid, MailKit.MessageFlags.Seen, true); //inbox.AddFlags(uid, MailKit.MessageFlags.Deleted, true); } // Disconnect client client.Disconnect(true); } } // End of the PickupEmails method