Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Sending Mail With Images in a HTML Table Through Java

Posted on 2012-03-28
2
378 Views
Last Modified: 2012-11-13
Hi

Please help me in generating an email with images along with the data using java mail api

Thanks
Pavan
0
Comment
Question by:Gangadhara Melukote
2 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 37776520
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37778020
You can sedn image(s) as attachements, then refer to them in your HTML contents of the message using cid:

Thsi code below shows how to use image to craete background for the HTML based email
I tried this code and it worked for me.
I'm sure you can do the same way with images embedded in the HTML table

Alternaively create the table with images on the server and then
send only link by email - i guess that is what most adverstisers do

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class SendEmailWithImage {

    public static void main(String[] args) {

                       try{
                 Properties props = new Properties();
        props.put("mail.smtp.host","mailserv.mycomp.com");
         Session mailSession = Session.getDefaultInstance(props, null);
        //mailSession.setDebug(true);
         MimeMessage message = new MimeMessage(mailSession);
       // Transport transport = mailSession.getTransport();
   MimeMultipart multipart = new MimeMultipart("related");
// first part  (the html)
        BodyPart messageBodyPart = new MimeBodyPart();

        message.setSubject("Emailer Test - HTML  mail with images - please confirm");
        message.setFrom(new InternetAddress("my.address@mycomp.com"));
        String htmltext = "<HTML><BODY border=\"3\" background=\"cid:bg\">" +
        "<TABLE border=3 background=cid:bg><p> running testing with embedded images, please confirm to the following email if you can see the images.</p>aman_kebreab@yahoo.com<br>" +
        		"<h1>This is test 1</h1><img src=\"cid:image\"> <br/> " +
        		"<h1>This is test 2</h1><img src=\"cid:image2\"></TABLE></BODY></HTML>";
        System.out.println("html text: \n" + htmltext);
           messageBodyPart.setContent(htmltext, "text/html");
      //<kendm@xilinx.com
      // add it
        multipart.addBodyPart(messageBodyPart);
          message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("my.address@mycomp.com"));
          //kendm@xilinx.com
          message.addRecipient(Message.RecipientType.BCC,
                  new InternetAddress("my.address@mycomp.com"));
// second part (the image)
        messageBodyPart = new MimeBodyPart();
        DataSource fds = new FileDataSource
          ("C:\\temp\\test\\Example_1.gif");
        messageBodyPart.setDataHandler(new DataHandler(fds));
        messageBodyPart.setHeader("Content-ID","<image>");
         // add it
        multipart.addBodyPart(messageBodyPart);

     // second image
        messageBodyPart = new MimeBodyPart();
        DataSource fds2 = new FileDataSource
          ("C:\\temp\\test\\Example_2.gif");
        messageBodyPart.setDataHandler(new DataHandler(fds2));
        messageBodyPart.setHeader("Content-ID","<image2>");
         // add it
        multipart.addBodyPart(messageBodyPart);
        DataSource fds3 = new FileDataSource
        ("C:\\temp\\test\\Example_3.gif");
      messageBodyPart.setDataHandler(new DataHandler(fds3));
      messageBodyPart.setHeader("Content-ID","<bg>");
       // add it
      multipart.addBodyPart(messageBodyPart);

        // put everything together
        message.setContent(multipart);
      //  transport.connect();
        System.out.println("message " + message.toString());
        Transport.send(message);
      //  transport.close();
              System.out.println("message sent");

    }catch(Exception ex){
     ex.printStackTrace();


}
}
}
                                            

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question