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!
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.
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.
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.
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.