Discuss this help topic in SecureBlackbox Forum

MIME: Create HTML message with embedded images

If an HTML text refers to one or more images, it's suitable to include those images to the same e-mail message. Such messages have the following structure:

  • multipart/related
    • text/html
    • image/jpeg or other MIME type
    • and so on for each image referenced in the HTML part

The HTML text refers to the embedded images using a special notation in "src" parameters: <img src="cid:imageId">. The following sample refers to the embedded image with Content-ID header field set to "imageId":

C#:


// create objects
TElMessage message = new TElMessage(new TElMultiPartList(), "My Cool E-mail Client");

// setup message parameters
message.From.AddAddress("Secretary", "sender@company.com");
message.To_.AddAddress("Addressee", "address@domain.com");
message.SetSubject("Test Message");

// specify that the included parts are related to each other
message.MainPart.SetContentSubtype("related");

// create and setup the HTML part
TElPlainTextPart htmlPart = new TElPlainTextPart(message, message.MainPart);
message.MainPart.AddPart(htmlPart);
htmlPart.SetContentSubtype("html");
htmlPart.SetText(@"<html><head></head><body><cite>Just a test message</cite><p><img src='cid:myImage'></body></html>");

// create and setup the image part
TElMessagePart image = new TElMessagePart();
message.MainPart.AddPart(image);
image.SetContentType("image");
// set an appropriate value corresponding to the image type, i.e. png, gif, etc.
image.SetContentSubtype("jpeg");
// specify the image id as it referenced to in the HTML part
image.SetContentID("myImage");
// specify the image is NOT an attachment
image.SetContentDisposition("inline");
// embed the image data
image.Data = File.ReadAllBytes(@"D:\image.jpg");

// assemble and save the message
using (FileStream f = new FileStream(@"D:\test.eml", FileMode.Create, FileAccess.Write, FileShare.Read))
{
    message.AssembleMessage(f);
    f.Close();
}

How To articles about MIME

Discuss this help topic in SecureBlackbox Forum