There are 3 ways of sending images inside email message:
- Embedding image using multipart/related MIME object
- Referencing remote web server (requires image maintenance for a long time)
- 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.