Embedding resources in an HTML email message

Top  Previous  Next

Using the HtmlEmailMessage class you can embed resources within your message that may be referenced by your HTML code.  This is particularly useful if your HTML message contains images that you wish to include in the message itself rather than having the client download these images from a remote web server.

 

To embed a resource you must perform the following:

 

1.  Embed the resource using one of the HtmlEmailMessage#embed methods.

 

2.  Update your HTML to reference the embedded resource using the unique id used in the previous step.

 

3.  Set the HTML contents of the email message using one of the HtmlEmailMessage#setHtmlBody methods.

 

 

Example

 

// HTML text referencing an embedded resource

String html = "An embedded image <img src=\"cid:12345\">";

 

// plain text displayed to non-HTML mail clients

String text = "Plain text part displayed to non-HTML mail clients";

 

// create new HtmlEmailMessage instance

HtmlEmailMessage message = new HtmlEmailMessage();

 

// embed resource with unique content ID of 12345

message.embed(new File("c:/tmp/image.gif"),"12345");

 

// set HTML text

message.setHtmlBody(html);

 

// set text displayed for non-HTML clients.

message.setTextBody(text);

 

 

As shown in the HTML text above, when referencing an embedded resource you must prefix the source with cid:  This will instruct the mail client to set the resource to be the contents of the embedded resource with the matching unique content ID.

 

See also

 

EmailMessage class overview

Smtp class overview

 

 

 





Home | Company | Products | Solutions | Purchase | Support | Services | Blog

© 2021 JSCAPE LLC