KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > msgsendsample


1 /*
2  * @(#)msgsendsample.java 1.19 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  * msgsendsample creates a very simple text/plain message and sends it.
47  * <p>
48  * usage: <code>java msgsendsample <i>to from smtphost true|false</i></code>
49  * where <i>to</i> and <i>from</i> are the destination and
50  * origin email addresses, respectively, and <i>smtphost</i>
51  * is the hostname of the machine that has the smtp server
52  * running. The last parameter either turns on or turns off
53  * debugging during sending.
54  *
55  * @author Max Spivak
56  */

57 public class msgsendsample {
58     static String JavaDoc msgText = "This is a message body.\nHere's the second line.";
59
60     public static void main(String JavaDoc[] args) {
61     if (args.length != 4) {
62         usage();
63         System.exit(1);
64     }
65
66     System.out.println();
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     if (debug) props.put("mail.debug", args[3]);
77
78     Session session = Session.getInstance(props, null);
79     session.setDebug(debug);
80     
81     try {
82         // create a message
83
Message msg = new MimeMessage(session);
84         msg.setFrom(new InternetAddress(from));
85         InternetAddress[] address = {new InternetAddress(args[0])};
86         msg.setRecipients(Message.RecipientType.TO, address);
87         msg.setSubject("JavaMail APIs Test");
88         msg.setSentDate(new Date());
89         // If the desired charset is known, you can use
90
// setText(text, charset)
91
msg.setText(msgText);
92         
93         Transport.send(msg);
94     } catch (MessagingException mex) {
95         System.out.println("\n--Exception handling in msgsendsample.java");
96
97         mex.printStackTrace();
98         System.out.println();
99         Exception JavaDoc ex = mex;
100         do {
101         if (ex instanceof SendFailedException) {
102             SendFailedException sfex = (SendFailedException)ex;
103             Address[] invalid = sfex.getInvalidAddresses();
104             if (invalid != null) {
105             System.out.println(" ** Invalid Addresses");
106             if (invalid != null) {
107                 for (int i = 0; i < invalid.length; i++)
108                 System.out.println(" " + invalid[i]);
109             }
110             }
111             Address[] validUnsent = sfex.getValidUnsentAddresses();
112             if (validUnsent != null) {
113             System.out.println(" ** ValidUnsent Addresses");
114             if (validUnsent != null) {
115                 for (int i = 0; i < validUnsent.length; i++)
116                 System.out.println(" "+validUnsent[i]);
117             }
118             }
119             Address[] validSent = sfex.getValidSentAddresses();
120             if (validSent != null) {
121             System.out.println(" ** ValidSent Addresses");
122             if (validSent != null) {
123                 for (int i = 0; i < validSent.length; i++)
124                 System.out.println(" "+validSent[i]);
125             }
126             }
127         }
128         System.out.println();
129         if (ex instanceof MessagingException)
130             ex = ((MessagingException)ex).getNextException();
131         else
132             ex = null;
133         } while (ex != null);
134     }
135     }
136
137     private static void usage() {
138     System.out.println("usage: java msgsendsample <to> <from> <smtp> true|false");
139     }
140 }
141
Popular Tags