11161N/A/*
11161N/A * Copyright (c) 1996-2010 Oracle and/or its affiliates. All rights reserved.
13760N/A *
11161N/A * Redistribution and use in source and binary forms, with or without
11161N/A * modification, are permitted provided that the following conditions
11161N/A * are met:
11161N/A *
11161N/A * - Redistributions of source code must retain the above copyright
11904N/A * notice, this list of conditions and the following disclaimer.
11161N/A *
11161N/A * - Redistributions in binary form must reproduce the above copyright
11161N/A * notice, this list of conditions and the following disclaimer in the
11161N/A * documentation and/or other materials provided with the distribution.
11161N/A *
11161N/A * - Neither the name of Oracle nor the names of its
11904N/A * contributors may be used to endorse or promote products derived
11161N/A * from this software without specific prior written permission.
11161N/A *
11161N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11161N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11161N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11161N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11161N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11161N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11161N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11161N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11161N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11904N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11161N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11161N/A */
11904N/A
11161N/Aimport java.util.*;
11161N/Aimport java.io.*;
11904N/Aimport javax.mail.*;
11161N/Aimport javax.mail.internet.*;
11161N/Aimport javax.activation.*;
11161N/A
11161N/A/**
11161N/A * msgmultisendsample creates a simple multipart/mixed message and sends it.
11161N/A * Both body parts are text/plain.
11905N/A * <p>
11904N/A * usage: <code>java msgmultisendsample <i>to from smtp true|false</i></code>
11904N/A * where <i>to</i> and <i>from</i> are the destination and
11904N/A * origin email addresses, respectively, and <i>smtp</i>
11161N/A * is the hostname of the machine that has smtp server
11161N/A * running. The last parameter either turns on or turns off
11904N/A * debugging during sending.
11161N/A *
11161N/A * @author Max Spivak
11161N/A */
11161N/Apublic class msgmultisendsample {
11161N/A static String msgText1 = "This is a message body.\nHere's line two.";
11161N/A static String msgText2 = "This is the text in the message attachment.";
11161N/A
11161N/A public static void main(String[] args) {
11161N/A if (args.length != 4) {
11161N/A System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false");
11161N/A return;
11161N/A }
11161N/A
18745N/A String to = args[0];
11161N/A String from = args[1];
11161N/A String host = args[2];
11161N/A boolean debug = Boolean.valueOf(args[3]).booleanValue();
11161N/A
11161N/A // create some properties and get the default Session
11161N/A Properties props = new Properties();
11161N/A props.put("mail.smtp.host", host);
11161N/A
11161N/A Session session = Session.getInstance(props, null);
11161N/A session.setDebug(debug);
11161N/A
11904N/A try {
11904N/A // create a message
11904N/A MimeMessage msg = new MimeMessage(session);
11904N/A msg.setFrom(new InternetAddress(from));
11904N/A InternetAddress[] address = {new InternetAddress(to)};
11904N/A msg.setRecipients(Message.RecipientType.TO, address);
11161N/A msg.setSubject("JavaMail APIs Multipart Test");
11161N/A msg.setSentDate(new Date());
11904N/A
11904N/A // create and fill the first message part
11904N/A MimeBodyPart mbp1 = new MimeBodyPart();
11161N/A mbp1.setText(msgText1);
// create and fill the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// Use setText(text, charset), to show it off !
mbp2.setText(msgText2, "us-ascii");
// 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();
}
}
}
}