Can I send files via email using MailKit?

As a header, is MailKit supported for sending a file?
If so, how can I do this?

+29
c # email-attachments mailkit mimekit
source share
2 answers

Yes. This is explained in the documentation as well as in the FAQ .

From the FAQ:

How to create a message with attachments?

To create a message with attachments, the first thing you need to do is create a multipart/mixed Container to which you want to add a message body. After you have added the body, you can then add the MIME parts to it that contain the contents of the files you want to attach, be sure to set the value of the Content-Disposition header for the attachment. You might also want to set the filename parameter in the Content-Disposition header, as well as the name parameter in the Content-Type header. The most convenient way to do this is simply to use the MimePart.FileName property , which will set both parameters for you, and also set the value of the Content-Disposition header to attachment if it has not already been set to something else.

 var message = new MimeMessage (); message.From.Add (new MailboxAddress ("Joey", " joey@friends.com ")); message.To.Add (new MailboxAddress ("Alice", " alice@wonderland.com ")); message.Subject = "How you doin?"; // create our message text, just like before (except don't set it as the message.Body) var body = new TextPart ("plain") { Text = @"Hey Alice, What are you up to this weekend? Monica is throwing one of her parties on Saturday and I was hoping you could make it. Will you be my +1? -- Joey " }; // create an image attachment for the file located at path var attachment = new MimePart ("image", "gif") { Content = new MimeContent (File.OpenRead (path)), ContentDisposition = new ContentDisposition (ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Base64, FileName = Path.GetFileName (path) }; // now create the multipart/mixed container to hold the message text and the // image attachment var multipart = new Multipart ("mixed"); multipart.Add (body); multipart.Add (attachment); // now set the multipart/mixed as the message body message.Body = multipart; 

An easier way to create messages with attachments is to use the BodyBuilder class.

 var message = new MimeMessage (); message.From.Add (new MailboxAddress ("Joey", " joey@friends.com ")); message.To.Add (new MailboxAddress ("Alice", " alice@wonderland.com ")); message.Subject = "How you doin?"; var builder = new BodyBuilder (); // Set the plain-text version of the message text builder.TextBody = @"Hey Alice, What are you up to this weekend? Monica is throwing one of her parties on Saturday and I was hoping you could make it. Will you be my +1? -- Joey "; // We may also want to attach a calendar event for Monica party... builder.Attachments.Add (@"C:\Users\Joey\Documents\party.ics"); // Now we just need to set the message body and we're done message.Body = builder.ToMessageBody (); 

For more information, see Creating Messages .

+53
source share

@jstedfast brought a pretty cool solution, here are some more examples of simple ways to simply send the file as an attachment (in this case, a pdf document, but it can be applied to any type of file).

 var message = new MimeMessage(); // add from, to, subject and other needed properties to your message var builder = new BodyBuilder(); builder.HtmlBody = htmlContent; builder.TextBody = textContent; // you can either create MimeEntity object(s) // this might get handy in case you want to pass multiple attachments from somewhere else byte[] myFileAsByteArray = LoadMyFileAsByteArray(); var attachments = new List<MimeEntity> { // from file MimeEntity.Load("myFile.pdf"), // file from stream MimeEntity.Load(new MemoryStream(myFileAsByteArray)), // from stream with a content type defined MimeEntity.Load(new ContentType("application", "pdf"), new MemoryStream(myFileAsByteArray)) } // or add file directly - there are a few more overloads to this builder.Attachments.Add("myFile.pdf"); builder.Attachments.Add("myFile.pdf", myFileAsByteArray); builder.Attachments.Add("myFile.pdf", myFileAsByteArray , new ContentType("application", "pdf")); // append previously created attachments foreach (var attachment in attachments) { builder.Attachments.Add(attachment); } message.Body = builder.ToMessageBody(); 

Hope this helps.

0
source share

All Articles