.Net : Retrieve Email Attachments and Embedded objects using Mailkit & Mimekit Library

Here we are going introduce our new technical document on retrieve email attachments and embedded objects using Mailkit and Mimekit library in C# and .net development technology.

Retrieve Email Attachments and Embedded objects using Mailkit & Mimekit Library


We are going to download attachments and embedded objects using mailkit and mimekit library. First of all we should know that what is mailkit and mime kit library. following is small description of these libraries.
  • MimeKit is a cross-platform .NET MIME creation and parser library. Click here to download.
  • MailKit is a cross-platform .NET library for IMAP, POP3, and SMTP. Click here to download.
With the help of this libraries we can fetch the email using IMAP or POP3. so first of all we need to set following code.
string host="hostname"; int port = 993; bool ssl = true; string username = "username"; string password = "password";
List<MimeKit.MimeMessage> MsgQue = new List<MimeMessage>(); using (var client = new Pop3Client())
     {       
            {                 try                 {                     try                                   client.Connect(host, port, ssl);
ms.Remove("XOAUTH2");                         client.Authenticate(username
                        client.AuthenticationMechani s, password);                     }                     catch (Exception)                     {                        
count = client.GetMessageCount();                     for (int i = 0; i < cou
                            //log.Error(ex.Message, ex);                                          }                     int  nt; i++)                     {                         var message = client.GetMessage(i);                         MsgQue.Add(message);                     }
.Message, ex);                              
                    client.Disconnect(true);                 }                 catch (Exception ex)                 {                             //log.Error(e x        }
}
             


so now we have all emails in MsgQue list object so now we will iterate each of these message to retrieve attachments.

List<Byte[]> attachmentContents = new List<Byte[]>();
            foreach (MimeMessage message in MsgQue)
bodyparts which 
            {                 // each message have  contains body of the email and attachements.
ts = message.BodyParts.Where(a => a.FileName != null);                 foreach (MimePart attachment i
                IEnumerable<MimePart> attachme nn attachments)                 {                     System.IO.MemoryStream ms = new System.IO.MemoryStream();
                }             }
                    attachment.ContentObject.DecodeTo(ms);                     attachmentContents.Add(ms.ToArray())
;

now you have all attachments and embedded object contents in attachmentContents list object.
here we have checked the file name of the mime part to verify that mime part is not textpart, so now you can iterate through each object of Byte[] in attachmentContent and save it either on File System or in a database.
SHARE

Ethan Millar

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment