Images not going in email
I am sending an email in HTML format using Java mail.
In this I have included an image using
<img src='c:/clip_image002.jpg'/>
the mail is going fine usng java mail api
If I view this email from my system, I am able to view the image (it seems to be picking the local path). but when I open this email on some other system, email does not display.
Can someone please let me know how to embed the image so that it opens on other system as well.
Here is my code
Code :
public static void main(String[] args) {
String to = "ee@3l.in";
String from = "r22@rr.com";
String host = "127.0.0.1";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
Transport bus = session.getTransport("smtp");
bus.connect();
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("firmation");
msg.setSentDate(new Date());
Multipart mp = new MimeMultipart();
setHTMLContent(msg, mp);
msg.saveChanges();
bus.sendMessage(msg, address);
bus.close();
}
catch (MessagingException mex) {
mex.printStackTrace();
while (mex.getNextException() != null) {
Exception ex = mex.getNextException();
ex.printStackTrace();
if (!(ex instanceof MessagingException)) break;
else mex = (MessagingException)ex;
}
}
}
public static void setHTMLContent(Message msg, Multipart mp) throws MessagingException {
String html = "<HTML><head><style><!-- @font-face{font-family:Latha;panose-1:2 11 6 4 2 2 2 2 2 4;mso-font-charset:0;mso-generic-font-family:swiss;--></style></head>";
html = html + "<BODY><p class=MsoNormal><span style='font-size:10.0pt;font-family:\"Verdana\",\"sans-serif\";mso-fareast-font-family:\"Times New Roman\"'>Dear Mr ABC,</span>";
html=html+"<img src='c:/clip_image002.jpg'/><br/>";
html = html + "<br/>Regards<br/>tha<br/><br/></p></font></BODY></HTML>";
MimeBodyPart p1 = new MimeBodyPart();
msg.setContent(mp);
p1.setContent(html, "text/html");
mp.addBodyPart(p1);
}
Re: Images not going in email
I also tried this but no good
Code :
public static void setHTMLContent(Message msg, Multipart mp) throws MessagingException {
MimeBodyPart p1 = new MimeBodyPart();
/*****/
DataSource fds = new FileDataSource("C:\\images\\clip_image002.jpg");
p1.setDataHandler(new DataHandler(fds));
p1.setHeader("Content-ID","<image>");
/********/
String html = "<HTML><head><style><!-- @font-face{font-family:Latha;panose-1:2 11 6 4 2 2 2 2 2 4;mso-font-charset:0;mso-generic-font-family:swiss;--></style></head>";
html = html + "<BODY><p class=MsoNormal><span style='font-size:10.0pt;font-family:\"Verdana\",\"sans-serif\";mso-fareast-font-family:\"Times New Roman\"'>Dear Mr ABC,</span>";
html=html+"<img src=\"cid:image\"><br/>";
html = html + "<br/>Regards<br/>tha<br/><br/></p></font></BODY></HTML>";
MimeBodyPart p1 = new MimeBodyPart();
msg.setContent(mp);
p1.setContent(html, "text/html");
mp.addBodyPart(p1);
}
Re: Images not going in email
Hello anjali09s. Welcome to the Java Programming Forums :D
As far as im aware, the images need to be in the root directory of the application. They cannot be in a folder or on any other drive.
Please try this and let me know if it works...
Re: Images not going in email
Thanks for replying.
That might be true for a web application. This is a standalone Java class.