Images not going in email
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Images not going in email

  1. #1
    Join Date
    Aug 2009
    Posts
    2

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

  2. #2
    Join Date
    Aug 2009
    Posts
    2

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

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Images not going in email

    Google is your friend - try Javamail With Images.

    Plan to throw one away; you will anyhow...
    F. Brooks
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center