KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sendfile


1 /*
2  * @(#)sendfile.java 1.11 03/06/19
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  * sendfile will create a multipart message with the second
47  * block of the message being the given file.<p>
48  *
49  * This demonstrates how to use the FileDataSource to send
50  * a file via mail.<p>
51  *
52  * usage: <code>java sendfile <i>to from smtp file true|false</i></code>
53  * where <i>to</i> and <i>from</i> are the destination and
54  * origin email addresses, respectively, and <i>smtp</i>
55  * is the hostname of the machine that has smtp server
56  * running. <i>file</i> is the file to send. The next parameter
57  * either turns on or turns off debugging during sending.
58  *
59  * @author Christopher Cotton
60  */

61 public class sendfile {
62
63     public static void main(String JavaDoc[] args) {
64     if (args.length != 5) {
65         System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
66         System.exit(1);
67     }
68
69     String JavaDoc to = args[0];
70     String JavaDoc from = args[1];
71     String JavaDoc host = args[2];
72     String JavaDoc filename = args[3];
73     boolean debug = Boolean.valueOf(args[4]).booleanValue();
74     String JavaDoc msgText1 = "Sending a file.\n";
75     String JavaDoc subject = "Sending a file";
76     
77     // create some properties and get the default Session
78
Properties props = System.getProperties();
79     props.put("mail.smtp.host", host);
80     
81     Session session = Session.getInstance(props, null);
82     session.setDebug(debug);
83     
84     try {
85         // create a message
86
MimeMessage msg = new MimeMessage(session);
87         msg.setFrom(new InternetAddress(from));
88         InternetAddress[] address = {new InternetAddress(to)};
89         msg.setRecipients(Message.RecipientType.TO, address);
90         msg.setSubject(subject);
91
92         // create and fill the first message part
93
MimeBodyPart mbp1 = new MimeBodyPart();
94         mbp1.setText(msgText1);
95
96         // create the second message part
97
MimeBodyPart mbp2 = new MimeBodyPart();
98
99             // attach the file to the message
100
FileDataSource fds = new FileDataSource(filename);
101         mbp2.setDataHandler(new DataHandler(fds));
102         mbp2.setFileName(fds.getName());
103
104         // create the Multipart and add its parts to it
105
Multipart mp = new MimeMultipart();
106         mp.addBodyPart(mbp1);
107         mp.addBodyPart(mbp2);
108
109         // add the Multipart to the message
110
msg.setContent(mp);
111
112         // set the Date: header
113
msg.setSentDate(new Date());
114         
115         // send the message
116
Transport.send(msg);
117         
118     } catch (MessagingException mex) {
119         mex.printStackTrace();
120         Exception JavaDoc ex = null;
121         if ((ex = mex.getNextException()) != null) {
122         ex.printStackTrace();
123         }
124     }
125     }
126 }
127
Popular Tags