Ответ 1
Да. Это объясняется в документации, а также в FAQ.
Из FAQ:
Как создать сообщение с вложениями?
Чтобы создать сообщение с вложениями, первое, что вам нужно сделать, это создать multipart/mixed
Контейнер, к которому вы хотите добавить тело сообщения. После того, как вы добавили тело, вы можете
затем добавьте в него части MIME, которые содержат содержимое файлов, которые вы хотите прикрепить, обязательно установив
значение заголовка Content-Disposition
для вложения. Возможно, вы также захотите установить filename
параметр в заголовке Content-Disposition
, а также параметр name
в Content-Type
заголовок. Самый удобный способ сделать это - просто использовать
свойство MimePart.FileName, которое
установит для вас оба параметра, а также установит значение заголовка Content-Disposition
на attachment
если он еще не был установлен на что-то другое.
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "[email protected]"));
message.To.Add (new MailboxAddress ("Alice", "[email protected]"));
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;
Более простой способ создания сообщений с вложениями - воспользоваться BodyBuilder класс.
var message = new MimeMessage ();
message.From.Add (new MailboxAddress ("Joey", "[email protected]"));
message.To.Add (new MailboxAddress ("Alice", "[email protected]"));
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 ();
Для получения дополнительной информации см. Создание сообщений.