KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > msgmultisendsample


1 /*
2  * @(#)msgmultisendsample.java 1.14 03/04/22
3  *
4  * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.util.*;
40 import java.io.*;
41 import javax.mail.*;
42 import javax.mail.internet.*;
43 import javax.activation.*;
44
45 /**
46  * msgmultisendsample creates a simple multipart/mixed message and sends it.
47  * Both body parts are text/plain.
48  * <p>
49  * usage: <code>java msgmultisendsample <i>to from smtp true|false</i></code>
50  * where <i>to</i> and <i>from</i> are the destination and
51  * origin email addresses, respectively, and <i>smtp</i>
52  * is the hostname of the machine that has smtp server
53  * running. The last parameter either turns on or turns off
54  * debugging during sending.
55  *
56  * @author Max Spivak
57  */

58 public class msgmultisendsample {
59     static String JavaDoc msgText1 = "This is a message body.\nHere's line two.";
60     static String JavaDoc msgText2 = "This is the text in the message attachment.";
61
62     public static void main(String JavaDoc[] args) {
63     if (args.length != 4) {
64         System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false");
65         return;
66     }
67
68     String JavaDoc to = args[0];
69     String JavaDoc from = args[1];
70     String JavaDoc host = args[2];
71     boolean debug = Boolean.valueOf(args[3]).booleanValue();
72
73     // create some properties and get the default Session
74
Properties props = new Properties();
75     props.put("mail.smtp.host", host);
76
77     Session session = Session.getInstance(props, null);
78     session.setDebug(debug);
79     
80     try {
81         // create a message
82
MimeMessage msg = new MimeMessage(session);
83         msg.setFrom(new InternetAddress(from));
84         InternetAddress[] address = {new InternetAddress(to)};
85         msg.setRecipients(Message.RecipientType.TO, address);
86         msg.setSubject("JavaMail APIs Multipart Test");
87         msg.setSentDate(new Date());
88
89         // create and fill the first message part
90
MimeBodyPart mbp1 = new MimeBodyPart();
91         mbp1.setText(msgText1);
92
93         // create and fill the second message part
94
MimeBodyPart mbp2 = new MimeBodyPart();
95         // Use setText(text, charset), to show it off !
96
mbp2.setText(msgText2, "us-ascii");
97
98         // create the Multipart and its parts to it
99
Multipart mp = new MimeMultipart();
100         mp.addBodyPart(mbp1);
101         mp.addBodyPart(mbp2);
102
103         // add the Multipart to the message
104
msg.setContent(mp);
105         
106         // send the message
107
Transport.send(msg);
108     } catch (MessagingException mex) {
109         mex.printStackTrace();
110         Exception JavaDoc ex = null;
111         if ((ex = mex.getNextException()) != null) {
112         ex.printStackTrace();
113         }
114     }
115     }
116 }
117
Popular Tags