http://www.rgagnon.com/javadetails/java-0504.html

It's easy to send HTML mail with JavaMail. Simply set the content type to "text/html".

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;

class SimpleMail {
    public static void main(String[] args) throws Exception{
        System.out.println("Sending mail...");
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.mymailserver.com");
        props.setProperty("mail.user", "myuser");
        props.setProperty("mail.password", "mypwd");

        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("HTML  mail with p_w_picpaths");
        message.setFrom(new InternetAddress("[email protected]"));
        message.setContent("

Hello world

", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }

One approach to include p_w_picpaths in the mail body is to use the IMG tag and make the p_w_picpaths available on a server.

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;

class SimpleMail1 {
    public static void main(String[] args) throws Exception{
        System.out.println("Sending mail...");
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.mymailserver.com");
        props.setProperty("mail.user", "myuser");
        props.setProperty("mail.password", "mypwd");

        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("HTML  mail with p_w_picpaths");
        message.setFrom(new InternetAddress("[email protected]"));
        message.setContent
          ("

This is a test

" + "", "text/html"); message.addRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]")); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }

The browser accesses these p_w_picpaths just as if it were displaying an p_w_picpath in a Web page. Unfortunately, spammers have used this mechanism as a sneaky way to record who visits their site (and mark your email as valid). To protect your privacy, many Web-based (and other) email clients don't display p_w_picpaths in HTML emails.

An alternative to placing absolute URLs to p_w_picpaths in your HTML is to include the p_w_picpaths as p_w_uploads to the email. The HTML can reference the p_w_picpath in an p_w_upload by using the protocol prefix cid: plus the content-id of the p_w_upload.

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

import java.util.Properties;

class SimpleMail2 {
    public static void main(String[] args) throws Exception{
        System.out.println("Sending mail...");
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.mymailserver.com");
        props.setProperty("mail.user", "myuser");
        props.setProperty("mail.password", "mypwd");

        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject("HTML  mail with p_w_picpaths");
        message.setFrom(new InternetAddress("[email protected]"));
        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress("[email protected]"));

        //
        // This HTML mail have to 2 part, the BODY and the embedded p_w_picpath
        //
        MimeMultipart multipart = new MimeMultipart("related");

        // first part  (the html)
        BodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "

Hello

"; messageBodyPart.setContent(htmlText, "text/html"); // add it multipart.addBodyPart(messageBodyPart); // second part (the p_w_picpath) messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource ("C:\\p_w_picpaths\\jht.gif"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID",""); // add it multipart.addBodyPart(messageBodyPart); // put everything together message.setContent(multipart); transport.connect(); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } }