Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.util.*;
  33. import java.io.*;
  34. import javax.mail.*;
  35. import javax.mail.internet.*;
  36. import javax.activation.*;
  37.  
  38. /**
  39.  * msgmultisendsample creates a simple multipart/mixed message and sends it.
  40.  * Both body parts are text/plain.
  41.  * <p>
  42.  * usage: <code>java msgmultisendsample <i>to from smtp true|false</i></code>
  43.  * where <i>to</i> and <i>from</i> are the destination and
  44.  * origin email addresses, respectively, and <i>smtp</i>
  45.  * is the hostname of the machine that has smtp server
  46.  * running.  The last parameter either turns on or turns off
  47.  * debugging during sending.
  48.  *
  49.  * @author      Max Spivak
  50.  */
  51. public class msgmultisendsample {
  52.     static String msgText1 = "This is a message body.\nHere's line two.";
  53.     static String msgText2 = "This is the text in the message attachment.";
  54.  
  55.     public static void main(String[] args) {
  56.         if (args.length != 4) {
  57.             System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false");
  58.             return;
  59.         }
  60.  
  61.         String to = args[0];
  62.         String from = args[1];
  63.         String host = args[2];
  64.         boolean debug = Boolean.valueOf(args[3]).booleanValue();
  65.  
  66.         // create some properties and get the default Session
  67.         Properties props = new Properties();
  68.         props.put("mail.smtp.host", host);
  69.  
  70.         Session session = Session.getInstance(props, null);
  71.         session.setDebug(debug);
  72.        
  73.         try {
  74.             // create a message
  75.             MimeMessage msg = new MimeMessage(session);
  76.             msg.setFrom(new InternetAddress(from));
  77.             InternetAddress[] address = {new InternetAddress(to)};
  78.             msg.setRecipients(Message.RecipientType.TO, address);
  79.             msg.setSubject("JavaMail APIs Multipart Test");
  80.             msg.setSentDate(new Date());
  81.  
  82.             // create and fill the first message part
  83.             MimeBodyPart mbp1 = new MimeBodyPart();
  84.             mbp1.setText(msgText1);
  85.  
  86.             // create and fill the second message part
  87.             MimeBodyPart mbp2 = new MimeBodyPart();
  88.             // Use setText(text, charset), to show it off !
  89.             mbp2.setText(msgText2, "us-ascii");
  90.  
  91.             // create the Multipart and its parts to it
  92.             Multipart mp = new MimeMultipart();
  93.             mp.addBodyPart(mbp1);
  94.             mp.addBodyPart(mbp2);
  95.  
  96.             // add the Multipart to the message
  97.             msg.setContent(mp);
  98.            
  99.             // send the message
  100.             Transport.send(msg);
  101.         } catch (MessagingException mex) {
  102.             mex.printStackTrace();
  103.             Exception ex = null;
  104.             if ((ex = mex.getNextException()) != null) {
  105.                 ex.printStackTrace();
  106.             }
  107.         }
  108.     }
  109. }
  110.