Mail

Send EMail with Inline Image


Leave a reply                     

How to send HTML email with inline image in java?

Java email API example

Java email MimeMessage, MimeBodyPart, MimeMultipart, Multipart, BodyPart

Java send HTML email with inline image

MimeMessage MimeBodyPart MimeMultipart Multipart BodyPart

An Email with inner image needs image code at two places, add the image as an attachment and reference it inside an HTML body part. Continue reading

Send EMail with Attachment


Leave a reply                     

How to send HTML email with attachment in java?

Java email API example

Java email MimeMessage, MimeBodyPart, MimeMultipart, Multipart, BodyPart, FileDataSource, DataSource, DataHandler

Java send HTML email with attachment

MimeMessage MimeBodyPart MimeMultipart Multipart BodyPart FileDataSource DataSource DataHandler

package raghuwansh.java.email;

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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;

public class SendEMailWithAttachment {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Recipient email, for multiple recipient's separate by comma
		String to = "to email id";
		// Sender email
		String from = "your email id";
		// Sender host
		String host = "localhost";
		// Get system properties
		Properties properties = System.getProperties();
		// Mail server
		properties.setProperty("mail.smtp.host", host);
		// Get the default Session object.
		Session session = Session.getDefaultInstance(properties);
		try {
			// Create a default MimeMessage object.
			MimeMessage message = new MimeMessage(session);
			// From
			message.setFrom(new InternetAddress(from));
			// To
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			// Subject
			message.setSubject("Subject");
			// HTML message
			Multipart multipart = new MimeMultipart();
			BodyPart htmlPart = new MimeBodyPart();
			htmlPart
					.setContent(
							"<h1>Message on <strong>http://www.raghuwansh.com</strong></h1>",
							"text/html");
			htmlPart.setDisposition(BodyPart.INLINE);
			multipart.addBodyPart(htmlPart);
			// Attachment
			BodyPart attachment = new MimeBodyPart();
			DataSource source = new FileDataSource(
					"C:/raghuwansh/attachment.txt");
			attachment.setDataHandler(new DataHandler(source));
			attachment.setFileName("attachment.txt");
			multipart.addBodyPart(attachment);

			message.setContent(multipart);
			// Send message
			Transport.send(message);
			System.out.println("Sent successfully.");
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
	}
}