As the title, is MailKit supported to send file?
If yes, how can I do it?
3 Answers
Yes. This is explained in the documentation as well as the FAQ.
From the FAQ:
How do I create a message with attachments?
To construct a message with attachments, the first thing you'll need to do is create a multipart/mixed
container which you'll then want to add the message body to first. Once you've added the body, you can then add MIME parts to it that contain the content of the files you'd like to attach, being sure to set the Content-Disposition
header value to the attachment. You'll probably also want to set the filename
parameter on the Content-Disposition
header as well as the name
parameter on the Content-Type
header. The most convenient way to do this is to simply use the MimePart.FileName property which
will set both parameters for you as well as setting the Content-Disposition
header value 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. 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;
A simpler way to construct messages with attachments is to take advantage of 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. I was hoping you could make it.
Will you be my +1?
-- Joey
";
// We may also want to attach a calendar event for Monica's 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.
-
Thank you, File has worked perfectly. But image is not as expected for the second example – KlynkC Jun 17 '16 at 01:43
-
1I don't understand what that means. – jstedfast Jun 17 '16 at 12:42
-
I have sent file, but file that type is image is not been sent – KlynkC Jun 18 '16 at 07:11
-
if you are adding it like in the example, I guarantee it's being sent. – jstedfast Jun 18 '16 at 11:22
-
the link is not valid anymore ? – kuldeep Jun 21 '17 at 12:27
-
In Mailkit, is it possible to send calendar data (the ics content) as one of the multiparts but not as attachment ? – kuldeep Jun 21 '17 at 14:19
-
2Yes - just set the content disposition to inline – jstedfast Jun 21 '17 at 14:34
-
I tried as following .. var attachment = new MimePart("text/calendar; method=REQUEST", "") { ContentObject = new ContentObject(GenerateStreamFromString(calString), ContentEncoding.Default), ContentDisposition = new ContentDisposition(ContentDisposition.Inline), ContentTransferEncoding = ContentEncoding.Base64 }; but in outlook email client i see that ics is still coming as an attachment instead of a meeting request. I am using outlook 2016 – kuldeep Jun 22 '17 at 14:06
-
Find an example message that works with Outlook and then construct your message in the same way as that message :-) MimeKit can be used to construct any message layout and set any attribute, so if it's possible to achieve what you want with MIME, it's possible to do it with MimeKit – jstedfast Jun 22 '17 at 14:09
@jstedfast brought pretty cool solution, here are a few more examples of simple ways to just send a file as an attachment (pdf document in this case, but can be applied to any file type).
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 it helps.

- 2,478
- 1
- 23
- 31
You can also send multiple files using this approach directly. **Note: files used here is IEnumerable files **
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(emailService.FromFullName, emailService.FromEmail));
message.To.AddRange(emailsToSend.Select(x => new MailboxAddress(x)));
message.Subject = subject;
var builder = new BodyBuilder();
builder.HtmlBody = body;
foreach (var attachment in files)
{
if (attachment.Length > 0)
{
string fileName = Path.GetFileName(attachment.FileName);
builder.Attachments.Add(fileName, attachment.OpenReadStream());
}
}
message.Body = builder.ToMessageBody();
}

- 108
- 2
- 8