Results 1 to 4 of 4
Thread: Images not going in email
- 08-02-2009, 02:07 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
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
Java 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); }
- 08-02-2009, 02:08 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
I also tried this but no good
Java 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); }
- 12-30-2009, 10:33 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
The image you reference must be available on the web for an <img src=""> to work. If you want to read in the image from the server and send it you have to do this:
BodyPart embedImage1=new MimeBodyPart();
DataSource ds1=new FileDataSource(new File(logoFile));
embedImage1.setDataHandler(new DataHandler(ds1));
embedImage1.setHeader("Content-ID","<logo1>");
/*This cid (content-ID) is passed to the jsp page in the
*<img src> tag for embedding */
mp.addBodyPart(embedImage1);
- 12-30-2009, 10:34 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Need help sending an email
By isshino in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 03-19-2009, 08:35 PM -
automatic email
By pracheebapate in forum New To JavaReplies: 2Last Post: 01-18-2009, 08:07 PM -
send email using apache commons email
By jnamendi in forum JavaServer Faces (JSF)Replies: 0Last Post: 10-14-2008, 05:55 PM -
help in email developing
By reached in forum New To JavaReplies: 0Last Post: 12-09-2007, 07:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks