We’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
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.SetHtmlData(@"<html><body><img src=""cid:image1"" /></body></html>");
// SetHtmlData automatically extracts plaint text from html so,
// unless you want to specify plain text explicitly, you don't need to use this method:
builder.SetTextData("This is text version of th message.");
// 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";
IMail email = builder.Create();
// Send the message
using (Smtp smtp = new Smtp())
{
smtp.Connect("mail.host.com");
smtp.Ehlo(HeloType.EhloHelo, "yourname");
smtp.Login("user", "password");
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 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.SetHtmlData("<html><body><img src=""cid:image1"" /></body></html>")
' SetHtmlData automatically extracts plaint text from html so,
' unless you want to specify plain text explicitly, you don't need to use this method:
builder.SetTextData("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 = New ContentType(MimeType.Image, MimeSubtype.Jpeg)
visual.ContentId = "image1"
Dim email As IMail = builder.Create()
' Send the message
Using smtp As New Smtp()
smtp.Connect("mail.host.com")
smtp.Ehlo(HeloType.EhloHelo, "yourname")
smtp.Login("user", "password")
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.