Showing posts with label sending email. Show all posts
Showing posts with label sending email. Show all posts

Sending email using embedded Image in Java

In our earlier tutorial we have how to send email using Java Code. As same way in this tutorial lets see how to send email using embedded image. By using javax.mail API we can achieve this by creating 2 MimeBodyPart instance for attaching image and to add HTML body text. In this below example lets see step by step to set SMTP server details and to message body with embedded image.

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 SendMail {

 public void emailWithEmbeddedImage() {

  try {

   final String from = "";
   final String password = "senderpassword";
   String toAddress = "";
   String ccAddress ="";
   String bccAddress = "";
   String name = "Job";
   // JavaMail session object
   Session session;

   // The JavaMail message object
   Message mesg;

   //SMTP server properties 
   Properties properties = new Properties();
   properties.put("", "");
   properties.put("mail.smtp.port", 587);
   properties.put("mail.smtp.auth", "true");
   properties.put("mail.smtp.starttls.enable", "true");

   // authenticate sender username and password 
   Authenticator auth = new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(from, password);

   // initialize session object
   session = Session.getInstance(properties, auth);

   // initialize message object
   mesg = new MimeMessage(session);

   // from Address
   mesg.setFrom(new InternetAddress(from));

   // Email Addresses
   InternetAddress toAdd = new InternetAddress(toAddress);
   InternetAddress ccAdd = new InternetAddress(ccAddress);
   InternetAddress bccAdd = new InternetAddress(bccAddress);

   mesg.addRecipient(Message.RecipientType.TO, toAdd);
   mesg.addRecipient(Message.RecipientType.CC, ccAdd);
   mesg.addRecipient(Message.RecipientType.BCC, bccAdd);

   // email Subject
   mesg.setSubject("Visitor Pass");

   // message body.
   Multipart mp = new MimeMultipart("related");

   String cid = "qr";

   MimeBodyPart pixPart = new MimeBodyPart();
   pixPart.setContentID("<" + cid + ">");

   MimeBodyPart textPart = new MimeBodyPart();
   textPart.setText("<html>" + "Hello " + name + ", <br> "
     + "Please find your visiting QR code <br> "
     + "<div><img src=\"cid:" + cid
     + "\" /></div></html>" + "Thanks & Regards, <br> "
     + "Bill</html>", "US-ASCII", "html");

   // Attach text and image to message body

   // Setting message content

   // Send mail

  } catch (MessagingException e) {
  } catch (IOException e) {
 public static void main(String[] args) {
  new SendMail().emailWithEmbeddedImage();


Sending Email using embedded Image in Java

Here QR code image embedded with email and shown sample screen shot as output.