Sending email with embedded image

We’ll use Mail.dll email client.

  1. We need to create an email message
  2. Use src=”cid:imageID” as image source in HTML
  3. Add the image to Visuals collection using AddVisual method
  4. Apply image id by setting ContentId
  5. And finally send the message
// 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:image1"" /></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 th message.";

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

IMail email = builder.Create();

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

    smtp.SendMessage(email);
    smtp.Close();
}

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:image1"" /></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 th message."

' Add image and set its Content-Id
Dim visual As MimeData = builder.AddVisual("c:image.jpg")
visual.ContentType = ContentType.ImageJpeg
visual.ContentId = "image1"

Dim email As IMail = builder.Create()

' Send the message
Using smtp As New Smtp()
        smtp.Connect("server.example.com")    ' or ConnectSSL for SSL
        smtp.UseBestLogin("user", "password")   ' remove if not needed

	smtp.SendMessage(email)

	smtp.Close(False)
End Using

Same code using fluent interface:

Mail.Html(@"<html><body><img src=""cid:image1"" /></body></html>")
        .From(new MailBox("alice@mail.com", "Alice"))
        .To(new MailBox("bob@mail.com", "Bob"))
        .Subject("Test")
        .AddVisual(@"c:image.jpg")
        .SetContentId("image1")
        .SetContentType(ContentType.ImageJpeg)
        .UsingNewSmtp()
        .WithCredentials("user","password")
        .Server("mail.example.com")
        .Send();

And of course the VB.NET version using fluent interface:

Mail.Html("<html><body><img src=""cid:image1"" /></body></html>") _
	.From(New MailBox("alice@mail.com", "Alice")) _
	.To(New MailBox("bob@mail.com", "Bob")) _
	.Subject("Test").AddVisual("c:image.jpg") _
	.SetContentId("image1") _
	.SetContentType(ContentType.ImageJpeg) _
	.UsingNewSmtp() _
	.WithCredentials("user", "password") _
	.Server("mail.example.com") _
	.Send()

You can download Mail.dll email client here.

Tags: , , ,

2 Responses to “Sending email with embedded image”

  1. Farooq Says:

    Hi,

    I have done the major part of reading emails, manipulating emails and so on. I wanted to forward the email to another address. That works fine for Gmail but when I try to deploy it to my server it is giving me 5.5.4 Invalid Address error. Do you have any idea, how can I correct it?

    Regards,
    Farooq

  2. Pawel Lesnikowski Says:

    What is the stack-trace of the error?

    However unlikely your smtp server might block your connection because you provided incorrect EHLO/HELO domain parameter (check out the Exchange article: http://support.microsoft.com/kb/291828).

    Please try providing correct domain or use parameterless Ehlo method that uses your machine’s IP address.

    Edit:
    Mail.dll now issues correct EHLO/HELO automatically.

Leave a Reply