aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes How to embed image (relative path) in an HTML e-mail? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How to embed image (relative path) in an HTML e-mail?" Watch "How to embed image (relative path) in an HTML e-mail?" New topic
Author

How to embed image (relative path) in an HTML e-mail?

Ron Ang
Greenhorn

Joined: Jan 14, 2010
Posts: 4
I am trying to send an e-mail with an embedded image. I used the approach below rather than using the <img> tag so that the image will immediately appear when email is viewed. When using the <img> tag, the recipient would have only the option to download the image which is not what I need. So, as you can observed, the approach used DataSource fds = new FileDataSource("C:\\images\\jht.gif"); , which actually works but I'm working at a web app. So, we I deploy this to other environments, this would not work. I think I need a relative path but I don't know how. Or other approach that would allow me to do this. Thanks!


credit to http://www.rgagnon.com/javadetails/java-0504.html for the above approach.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

So your question appears to amount to "How does code in my web application find the full path of a file?"

Or am I misunderstanding your post? I don't see why the fact that your code is part of a web application makes any difference; just provide the FileDataSource with the full path to the image you want to include in the e-mail.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Adding images to emails is easy. Embedding them isn't. I suggest you forget all about JavaMail, and instead use Apache Commons Email. It has class HtmlEmail which has several embed methods. The class Javadoc even includes an example of how to embed images into the email.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Actually Ron's code for the multipart-related message does work quite well (and the source from which it came is a good one). So I wouldn't discard it just yet, I think we need to find out what the actual problem is.
Ron Ang
Greenhorn

Joined: Jan 14, 2010
Posts: 4
Hi All, my actual problem was I needed to hard code the path of the image in order to embed it to the e-mail. The problem will occur when I deploy it to other environment and will not be able to access the hard coded path.

However, I already found a solution. I just specified the servlet context path then added the image's location. So it will somewhat look like this.



Thanks all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to embed image (relative path) in an HTML e-mail?
 
Similar Threads
problems adding attachements to Javamail with Outlook
problems in background image !!!
Sending Email using Java gives error message
Sending MAIL by Java
how to send mail using gmail server