Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ron Ang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20962
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic