+1 vote

We will edit the mail with image, we need we can insert the image as microsoft word do and send it. We want the receiver can display it in mail but not in attached files. How can we?
That is, we want to Embed image in html mail.

by

1 Answer

0 votes
 
Best answer

There are 3 ways of sending images inside email message:

  1. Embedding image using multipart/related MIME object
  2. Referencing remote web server (requires image maintenance for a long time)
  3. Using BASE64 encoded inline images (not supported by many email clients)

In my opinion 1. is the best option, as email itself contains all required data to be rendered. It makes email a bit larger of course.

Creating email with embedded image is extremely easy:

MailBuilder builder = new MailBuilder();

// Set From, To
builder.From.Add(new MailBox("alice@example.com", "Alice"));
builder.To.Add(new MailBox("bob@example.com", "Bob"));
builder.Subject = "Embedded image";

// Set HTML content (Notice the src="cid:..." attribute)
builder.Html = @"<html><body><img src=""cid:image1"" /></body></html>";

// Add image and set its Content-Id
MimeData visual = builder.AddVisual(@"c:\image.jpg");
visual.ContentId = "image1";

IMail email = builder.Create();

That's it - email is ready to be sent:

using (Smtp smtp = new Smtp())
{
    smtp.Connect("server.example.com");    // or ConnectSSL for SSL
    smtp.UseBestLogin("user", "password"); // remove if authentication is not needed

    smtp.SendMessage(email);

    smtp.Close();
}

You can find all info here in sending email with embedded image article.

by (301k points)
...