Solved

Sending Mail With Images in a HTML Table Through Java

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now