Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Send multipart mail using java mail

This Example shows you how to send multipart mail using java mail. Multipart is like a container that holds one or more body parts.


Send multipart mail using java mail


This Example shows you how to send multipart mail using java mail. Multipart is like a container that holds one or more body parts. When u send a multipart mail firstly create a Multipart class object and then create BodyPart class object, set text in BodyPart class object and add all BodyPart class object in Multipart class object and send the message. Class Multipart that we will use in this code is an abstract class. Method used in this program of Multipart class is.....

void addBodyPart(BodyPart part);

This method is used to add parts in multipart.

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMultipartMail {

  public static void main(String args[]) throws Exception {

  String host = "";
  String from = "[email protected]";
  String to = "[email protected]";

  Properties properties = System.getProperties();
  properties.setProperty("", host);

  Session session = Session.getDefaultInstance(properties);

  Message msg = new MimeMessage(session);

  msg.setFrom(new InternetAddress(from));
  msg.setSubject("MultiPart Mail");

  Multipart multipart = new MimeMultipart();

  BodyPart part1 = new MimeBodyPart();
 part1.setText("This is multipart mail and u read part1......");

 BodyPart part2 = new MimeBodyPart();
 part2.setText("This is multipart mail and u read part2......");



  System.out.println("Message send....");


Message send....

Download code


Share on Google+Share on Google+

Send multipart mail using java mail

Posted on: December 15, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Send multipart mail using java mail  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Raul perez
September 13, 2013
question? send an email with an image

Hi, sorry I have a question ... I'm trying to send an email with an image, meaning through JavaMail, I have installed the JDK 6.9 Here the code: try { Session session = null; Properties props = new Properties(); this.para=mail; props.setProperty("", this.servidor); props.setProperty("mail.smtp.port", this.puerto); props.setProperty("mail.smtp.starttls.enable", "true"); if (this.ssl){ props.put("mail.smtp.socketFactory.port", this.puerto); props.put("mail.smtp.socketFactory.class",""); } if( this.usuario != null && this.password != null && ! this.usuario.equals("") && ! this.password.equals("")){ props.put("mail.smtp.auth", "true"); props.put("mail.smtp.user", this.usuario ); session = Session.getInstance(props,new Authenticator(){ @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(USU ,PASS); } }); }else{ session = Session.getInstance(props, null); } MimeMultipart multiParte = new MimeMultipart(); BodyPart messageBodyPart = null; messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent("<html><h1>Hola Mundo</h1> <img src='cid:tdc_picture'></html>", "text/html"); multiParte.addBodyPart(messageBodyPart); /*EVERYTHING IS OK WITHOUT THIS, AND YES MAIL IS SENT messageBodyPart = new MimeBodyPart(); DataSource fds = new FileDataSource("C:\\minion.jpg"); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader("Content-ID","<tdc_picture>"); multiParte.addBodyPart(messageBodyPart); */ System.out.println("Todo correcto 0..."); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(this.usuario)); message.setSubject(this.asunto); message.setContent(multiParte); message.setSentDate(new Date()); // Obtener multiples correos destinatarios if (!this.para.equals("")){ String[] arr = this.para.split(";"); for (int i = 0; i < arr.length; i++) { if (!arr[i].trim().equals("")){ message.addRecipient(Message.RecipientType.TO,new InternetAddress(arr[i])); } } } System.out.println("Todo correcto 1..."); // Enviar correo Transport t = session.getTransport("smtp"); t.connect(this.servidor,this.usuario,this.password); t.sendMessage(message, message.getAllRecipients()); t.close(); System.out.println("MAIL SENT"); //Uncommenting CODE WHERE IS THE PICTURE, MAKE THE FOLLOWING ERROR: com.sun.mail.smtp.SMTPTransport.sendMessage( //AND ALSO MAKE ME AN ERROR IN THIS LINE t.sendMessage(message, message.getAllRecipients()); CAN ANYONE HELP ME? THANKS