Solved

Sending Mail With Images in a HTML Table Through Java

Posted on 2012-03-28
2
368 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

778 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