Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Java: send a multipart/mixed message

Java: send a multipart/mixed message
{filelink=1018}

import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 JavaMail_Mime
 {
  static String message1 = "Hello, from here";
  static String message2 = "Hello, from there";
  public static void main(String[] args)
  	{
    Properties prop = new Properties();
    prop.put("mail.smtp.host", "myMailServer");
    prop.put("mail.from", "me@example.com");
    String to = "contact@exemple.com";
    Session session = Session.getInstance(prop, null);
    try {
      // create message
      MimeMessage msg = new MimeMessage(session);
      // set message header
      msg.setFrom();
      msg.setRecipients(Message.RecipientType.TO, to);
      msg.setSubject("multipart/mixed message example");
      msg.setSentDate(new Date());
      // Create body part for 1st and 2nd messages
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText(message1);
      MimeBodyPart mbp2 = new MimeBodyPart();
      mbp2.setText(message2);
      // create the Multipart and its parts to it
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp1);
      mp.addBodyPart(mbp2);
      //  add the Multipart to the message
      msg.setContent(mp);
      // send the message
      Transport.send(msg);
    } catch (MessagingException mex) {
      mex.printStackTrace();
      Exception ex = null;
      if ((ex = mex.getNextException()) != null) {
        ex.printStackTrace();
      }
    }
  }
}

Users who found this page were searching for:

  • Perl Binary Mode
  • python smtplib multiple receivers
  • how to use comma in java
  • returning empty arrays in perl
  • java set read only file attribute
  • python working directory
  • java subtract method
  • database programming microsoft sql server with java
  • python string to number

No comments yet.

Leave a Comment