views:

35

answers:

3

I'm writing a j2ee application, that generates an html and sends it as email. In my html, a have an image, but it is not displayed when email is received. The html code is something like:

<img src="myimage.gif"></img>

where "myimage.gif" is sent as attached file in the email. I tried to change it to

<img src="cid:myimage.gif"></img>

but still no result. Any ideas? It should be without a link to the image.

+3  A: 

You should upload your image to you server and reference that as a hard coded url in the src

e.g. upload to myserver.com/images/myimage.gif the in your html

<img src="http://myserver.com/images/myimage.gif" />
corroded
yes, that is a way, but I want it to be server-independent and not to be with a link. When my server is down, the image still must be displayed.
brain_damage
+1  A: 

If the image is small enough, you could use my HTML Table pixel format :)

see my blog for details: HTML Table Pixel Format

This is just plain valid HTML, however it renders as an image.

/end of shameless plug

Darknight
wow! Great! My image is small, but with a lot of details and colors, and I do not know whether the result will be good-looking. Which is the smallest pixel size? 1px or it can be smaller?
brain_damage
It will be identical to the original image, pixel for pixel, best of all, because its a table, it auto scales..
Darknight
+1  A: 

Take a look at Commons Email. It's build on top of the Java Mail API but simplifies it.

They have an example for sending html mails with inline images http://commons.apache.org/email/userguide.html

import org.apache.commons.mail.HtmlEmail;
...

// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName("mail.myserver.com");
email.addTo("[email protected]", "John Doe");
email.setFrom("[email protected]", "Me");
email.setSubject("Test email with inline image");

// embed the image and get the content id
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
String cid = email.embed(url, "Apache logo");

// set the html message
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");

// send the email
email.send();
Soundlink