Debian Codegraph @

demo/src/main/java/sendfile.java

raw
1 /*
2 * Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0, which is available at
6 * http://www.eclipse.org/org/documents/edl-v10.php.
7 *
8 * SPDX-License-Identifier: BSD-3-Clause
9 */
10
11 import java.util.*;
12 import java.io.*;
13 import jakarta.mail.*;
14 import jakarta.mail.internet.*;
15 import jakarta.activation.*;
16
17 /**
18 * sendfile will create a multipart message with the second
19 * block of the message being the given file.<p>
20 *
21 * This demonstrates how to use the FileDataSource to send
22 * a file via mail.<p>
23 *
24 * usage: <code>java sendfile <i>to from smtp file true|false</i></code>
25 * where <i>to</i> and <i>from</i> are the destination and
26 * origin email addresses, respectively, and <i>smtp</i>
27 * is the hostname of the machine that has smtp server
28 * running. <i>file</i> is the file to send. The next parameter
29 * either turns on or turns off debugging during sending.
30 *
31 * @author Christopher Cotton
32 */
33 public class sendfile {
34
35 public static void main(String[] args) {
36 if (args.length != 5) {
37 System.out.println("usage: java sendfile <to> <from> <smtp> <file> true|false");
38 System.exit(1);
39 }
40
41 String to = args[0];
42 String from = args[1];
43 String host = args[2];
44 String filename = args[3];
45 boolean debug = Boolean.valueOf(args[4]).booleanValue();
46 String msgText1 = "Sending a file.\n";
47 String subject = "Sending a file";
48
49 // create some properties and get the default Session
50 Properties props = System.getProperties();
51 props.put("mail.smtp.host", host);
52
53 Session session = Session.getInstance(props, null);
54 session.setDebug(debug);
55
56 try {
57 // create a message
58 MimeMessage msg = new MimeMessage(session);
59 msg.setFrom(new InternetAddress(from));
60 InternetAddress[] address = {new InternetAddress(to)};
61 msg.setRecipients(Message.RecipientType.TO, address);
62 msg.setSubject(subject);
63
64 // create and fill the first message part
65 MimeBodyPart mbp1 = new MimeBodyPart();
66 mbp1.setText(msgText1);
67
68 // create the second message part
69 MimeBodyPart mbp2 = new MimeBodyPart();
70
71 // attach the file to the message
72 mbp2.attachFile(filename);
73
74 /*
75 * Use the following approach instead of the above line if
76 * you want to control the MIME type of the attached file.
77 * Normally you should never need to do this.
78 *
79 FileDataSource fds = new FileDataSource(filename) {
80 public String getContentType() {
81 return "application/octet-stream";
82 }
83 };
84 mbp2.setDataHandler(new DataHandler(fds));
85 mbp2.setFileName(fds.getName());
86 */
87
88 // create the Multipart and add its parts to it
89 Multipart mp = new MimeMultipart();
90 mp.addBodyPart(mbp1);
91 mp.addBodyPart(mbp2);
92
93 // add the Multipart to the message
94 msg.setContent(mp);
95
96 // set the Date: header
97 msg.setSentDate(new Date());
98
99 /*
100 * If you want to control the Content-Transfer-Encoding
101 * of the attached file, do the following. Normally you
102 * should never need to do this.
103 *
104 msg.saveChanges();
105 mbp2.setHeader("Content-Transfer-Encoding", "base64");
106 */
107
108 // send the message
109 Transport.send(msg);
110
111 } catch (MessagingException mex) {
112 mex.printStackTrace();
113 Exception ex = null;
114 if ((ex = mex.getNextException()) != null) {
115 ex.printStackTrace();
116 }
117 } catch (IOException ioex) {
118 ioex.printStackTrace();
119 }
120 }
121 }