+1 vote

How can you send multiple email attachments via smtp? I generate the email using MailBuilder.

Thank you for the quick answer

by (650 points)

2 Answers

0 votes
 
Best answer

It's quite easy - during email creation, you need to invoke MailBuilder's AddAttachment method multiple times.

Attachments are integral part of an email message.

MailBuilder builder = new MailBuilder();
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "Test";
builder.Text = "This is a plain text message.";

/* Read attachment from disk, add it to Attachments collection */

MimeData attachment1 = builder.AddAttachment(@"../image1.jpg");
MimeData attachment2 = builder.AddAttachment(@"../image2.jpg");

IMail email = builder.Create();

Now you can send the email:

using (Smtp smtp = new Smtp())
{
    smtp.ConnectSSL("server.example.com");
    smtp.UseBestLogin("user", "password");

    smtp.SendMessage(email);

    smtp.Close();
 }

There is also an AddAttachment overload that allow you to create an attachment from a byte array.

You can use retuned MimeData object to set/change attachment's name or content type, or any other MIME property.

by (272k points)
selected by
0 votes

Many thanks for the quick response

by (650 points)
...