Sending email with embedded image
There are 3 ways of embedding 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 this article we’ll show how to use the first method. This method makes message bigger, but images don’t need to be stored on your web server. Furthermore this method is widely supported by email clients.
How this works
When MIME tree is build we specify that images added are in relation to HTML content of the message. We also specify content disposition as inline. This means that those images are not actual attachments, but rather should be displayed to user when he/she opens the message.
In Mail.dll it is quite simple as we just need to add the image to MailBuilder.Visualscollection using AddVisual method. Remember that you need to set the image’s Content-ID, as it is required in the next step.
HTML body of the message references those images by using special "cid:"
protocol. It specifies Content-ID of the image that should be used:
This is our <strong>brand new</strong> logo: <br /> <img src="cid:logo@example.com" />
Create HTML message using MailBuilder
// Use builder class to create an email MailBuilder builder = new MailBuilder(); // Set From, To builder.From.Add(new MailBox("alice@mail.com", "Alice")); builder.To.Add(new MailBox("bob@mail.com", "Bob")); builder.Subject = "Test"; // Set HTML content (Notice the src="cid:..." attribute) builder.Html = @"<html><body><img src=""cid:logo@example.com"" /></body></html>"; // Html automatically extracts plaint text from html so, // unless you want to specify plain text explicitly, you don't need to use this line: builder.Text = "This is text version of the message."; // Add image and set its Content-Id MimeData visual = builder.AddVisual(@"c:\image.jpg"); visual.ContentId = "logo@example.com"; IMail email = builder.Create();
And of course the VB.NET version of the same code:
' Use builder class to create an email Dim builder As New MailBuilder() ' Set From, To builder.From.Add(New MailBox("alice@mail.com", "Alice")) builder.To.Add(New MailBox("bob@mail.com", "Bob")) builder.Subject = "Test" ' Set HTML content (Notice the src="cid:..." attribute) builder.Html = "<html><body><img src=""cid:logo@example.com"" /></body></html>" ' Html automatically extracts plaint text from html so, ' unless you want to specify plain text explicitly, you don't need to use this line: builder.Text = "This is text version of the message." ' Add image and set its Content-Id Dim visual As MimeData = builder.AddVisual("c:\image.jpg") visual.ContentId = "logo@example.com" Dim email As IMail = builder.Create()
HTML message using fluent interface
// C# using Fluent = Limilabs.Mail.Fluent; IMail email = Fluent.Mail.Html(@"<html><body><img src=""cid:logo@example.com"" /></body></html>") .From(new MailBox("alice@mail.com", "Alice")) .To(new MailBox("bob@mail.com", "Bob")) .Subject("Test") .AddVisual(@"c:\image.jpg") .SetContentId("logo@example.com") .Create();
And of course the VB.NET version using fluent interface:
' VB.NET IMail email = Mail.Html("<html><body><img src=""cid:logo@example.com"" /></body></html>") _ .From(New MailBox("alice@mail.com", "Alice")) _ .To(New MailBox("bob@mail.com", "Bob")) _ .Subject("Test").AddVisual("c:\image.jpg") _ .SetContentId("logo@example.com") _ .Create()
Sending email using SMTP
Now we can connect to SMTP server and send the HTML email with embedded image we recently created:
using (Smtp client = new Smtp()) { client.Connect("smtp.example.com"); // or ConnectSSL client.UseBestLogin("user", "password"); client.SendMessage(email); client.Close(); }
You can download Mail.dll email client here.