Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Images not going in email

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    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
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images not going in email

    I also tried this but no good
    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
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Images not going in email

    Hello anjali09s. Welcome to the Java Programming Forums

    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...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images not going in email

    Thanks for replying.

    That might be true for a web application. This is a standalone Java class.

Similar Threads

  1. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  2. Replies: 2
    Last Post: June 26th, 2009, 12:08 PM
  3. sending an email with pdf file everyday
    By pradeepptp in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: June 21st, 2009, 11:49 AM
  4. [SOLVED] Need to display multiple images from database on a webpage
    By raghuprasad in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: May 13th, 2009, 04:15 AM
  5. What are the best way of placing images in GUI?
    By Ciwan in forum AWT / Java Swing
    Replies: 5
    Last Post: February 26th, 2009, 05:19 PM