Sending email with embedded image
Monday, December 21st, 2009We’ll use Mail.dll email client.
- We need to create an email message
- Use src=”cid:imageID” as image source in HTML
- Add the image to Visuals collection using AddVisual method
- Apply image id by setting ContentId
- And finally send the message
// Use builder class to create an email
SimpleMailMessageBuilder builder = new SimpleMailMessageBuilder();
// Set From, To
builder.From.Add(new MailBox("alice@mail.com", "Alice"));
builder.To.Add(new MailBox("bob@mail.com", "Bob"));
builder.Subject = "Test";
builder.SetTextData("This is text version of th message.");
// Set HTML content (Notice the src="cid:..." attribute)
builder.SetHtmlData(@"<html><body><img src=""cid:image1"" /></body></html>");
// Add image and set its Content-Id
MimeData visual = builder.AddVisual(@"c:\image.jpg");
visual.ContentType = new ContentType(MimeType.Image, MimeSubtype.Jpeg);
visual.ContentId = "image1";
ISimpleMailMessage email = builder.Create();
// Send the message
using (Smtp smtp = new Smtp())
{
smtp.User = "lesnikowski";
smtp.Password = "password";
smtp.Connect("mail.host.com");
smtp.Ehlo(HeloType.EhloHelo, "yourname");
smtp.Login();
smtp.SendMessage(email);
smtp.Close(false);
}
And of course the VB.NET version of the same code:
' Use builder class to create an email
Dim builder As New SimpleMailMessageBuilder()
' Set From, To
builder.From.Add(New MailBox("alice@mail.com", "Alice"))
builder.To.Add(New MailBox("bob@mail.com", "Bob"))
builder.Subject = "Test"
builder.SetTextData("This is text version of th message.")
' Set HTML content (Notice the src="cid:..." attribute)
builder.SetHtmlData("<html><body><img src=""cid:image1"" /></body></html>")
' Add image and set its Content-Id
Dim visual As MimeData = builder.AddVisual("c:\image.jpg")
visual.ContentType = New ContentType(MimeType.Image, MimeSubtype.Jpeg)
visual.ContentId = "image1"
Dim email As ISimpleMailMessage = builder.Create()
' Send the message
Using smtp As New Smtp()
smtp.User = "lesnikowski"
smtp.Password = "password"
smtp.Connect("mail.host.com")
smtp.Ehlo(HeloType.EhloHelo, "yourname")
smtp.Login()
smtp.SendMessage(email)
smtp.Close(False)
End Using
Same code using fluent interface:
Mail.Html(@"<html><body><img src=""cid:image1"" /></body></html>")
.Text("This is text version of th message.")
.From(new MailBox("alice@mail.com", "Alice"))
.To(new MailBox("bob@mail.com", "Bob"))
.Subject("Test")
.AddVisual(@"c:\image.jpg")
.SetContentId("image1")
.SetContentType(
new ContentType(MimeType.Image, MimeSubtype.Jpeg))
.UsingNewSmtp()
.WithCredentials("lesnikowski","password")
.Server("mail.host.com")
.Send();
And of course the VB.NET version using fluent interface:
Mail.Html("<html><body><img src=""cid:image1"" /></body></html>")_
.Text("This is text version of th message.")_
.From(New MailBox("alice@mail.com", "Alice"))_
.To(New MailBox("bob@mail.com", "Bob"))_
.Subject("Test").AddVisual("c:\image.jpg")_
.SetContentId("image1")_
.SetContentType(_
New ContentType(MimeType.Image, MimeSubtype.Jpeg))_
.UsingNewSmtp()_
.WithCredentials("lesnikowski", "password")_
.Server("mail.host.com")_
.Send()
You can download Mail.dll email client here.
