Home Javamail Send multipart mail using java mail
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Send multipart mail using java mail

Advertisement
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.

SendMultipartMail.java


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

public class SendMultipartMail {

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

  String host = "192.168.10.205";
  String from = "test@localhost";
  String to = "komal@localhost";

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

  Session session = Session.getDefaultInstance(properties);

  Message msg = new MimeMessage(session);

  msg.setFrom(new InternetAddress(from));
  msg.addRecipient(Message.RecipientType.TO, 
new 
InternetAddress(to));
  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......");

  multipart.addBodyPart(part1);
  multipart.addBodyPart(part2);

  msg.setContent(multipart);

  Transport.send(msg);
  System.out.println("Message send....");
  }
}

Output:

Message send....

Download code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 15, 2008

Related Tutorials

Ask Questions?    Discuss: Send multipart mail using java mail  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
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("mail.smtp.host", 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","javax.net.ssl.SSLSocketFactory"); } 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(SMTPTransport.java:676) //AND ALSO MAKE ME AN ERROR IN THIS LINE t.sendMessage(message, message.getAllRecipients()); CAN ANYONE HELP ME? THANKS
DMCA.com