View RSS Feed

Java Mail

Send multipart mail using java mail

Rating: 2 votes, 1.00 average.
by , 04-25-2012 at 11:02 PM (1278 Views)
By the help of java mail how one could send multipart mails has been shown in this example. One might think a Multipart to be a container which contains more than one body parts. For sending the multipart mail, Multipart class object is made. After this BodyPart class object is created and text is set in the BodyPart class object which is then added to Multipart class object. Ultimately message is sent. In the given code, Class Multipart is abstract class. In this program, method of Multipart class is
void addBodyPart(BodyPart part);

For addition of parts to multipart, use this method.

Java Code:
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 = "test@localhost";
  String to = "komal@localhost";

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

  Session session = Session.getDefaultInstance(properties);

  Message msg = new MimeMessage(session);

  msg.setFrom(new InternetAddress(from));
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......");



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

Submit "Send multipart mail using java mail" to Facebook Submit "Send multipart mail using java mail" to Digg Submit "Send multipart mail using java mail" to Submit "Send multipart mail using java mail" to StumbleUpon Submit "Send multipart mail using java mail" to Google

Mail API