Results 1 to 4 of 4
  1. #1
    anjali09s is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default 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);
                   
           
        }

  2. #2
    anjali09s is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default

    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);
                   
           
        }

  3. #3
    lowell108 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    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);

  4. #4
    lowell108 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Also, the html has to look like this: <img src="cid:logo1">

Similar Threads

  1. Need help sending an email
    By isshino in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-19-2009, 08:35 PM
  2. automatic email
    By pracheebapate in forum New To Java
    Replies: 2
    Last Post: 01-18-2009, 08:07 PM
  3. send email using apache commons email
    By jnamendi in forum JavaServer Faces (JSF)
    Replies: 0
    Last Post: 10-14-2008, 05:55 PM
  4. help in email developing
    By reached in forum New To Java
    Replies: 0
    Last Post: 12-09-2007, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •