View RSS Feed

Java Mail

Send multipart mail using java mail

Rating: 2 votes, 1.00 average.
by , 04-25-2012 at 10:02 PM (1128 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 = "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....");
  }
}

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 del.icio.us Submit "Send multipart mail using java mail" to StumbleUpon Submit "Send multipart mail using java mail" to Google

Categories
Mail API

Comments