Debian Codegraph @

demo/src/main/java/msgmultisendsample.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 * msgmultisendsample creates a simple multipart/mixed message and sends it.
19 * Both body parts are text/plain.
20 * <p>
21 * usage: <code>java msgmultisendsample <i>to from smtp true|false</i></code>
22 * where <i>to</i> and <i>from</i> are the destination and
23 * origin email addresses, respectively, and <i>smtp</i>
24 * is the hostname of the machine that has smtp server
25 * running. The last parameter either turns on or turns off
26 * debugging during sending.
27 *
28 * @author Max Spivak
29 */
30 public class msgmultisendsample {
31 static String msgText1 = "This is a message body.\nHere's line two.";
32 static String msgText2 = "This is the text in the message attachment.";
33
34 public static void main(String[] args) {
35 if (args.length != 4) {
36 System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false");
37 return;
38 }
39
40 String to = args[0];
41 String from = args[1];
42 String host = args[2];
43 boolean debug = Boolean.valueOf(args[3]).booleanValue();
44
45 // create some properties and get the default Session
46 Properties props = new Properties();
47 props.put("mail.smtp.host", host);
48
49 Session session = Session.getInstance(props, null);
50 session.setDebug(debug);
51
52 try {
53 // create a message
54 MimeMessage msg = new MimeMessage(session);
55 msg.setFrom(new InternetAddress(from));
56 InternetAddress[] address = {new InternetAddress(to)};
57 msg.setRecipients(Message.RecipientType.TO, address);
58 msg.setSubject("Jakarta Mail APIs Multipart Test");
59 msg.setSentDate(new Date());
60
61 // create and fill the first message part
62 MimeBodyPart mbp1 = new MimeBodyPart();
63 mbp1.setText(msgText1);
64
65 // create and fill the second message part
66 MimeBodyPart mbp2 = new MimeBodyPart();
67 // Use setText(text, charset), to show it off !
68 mbp2.setText(msgText2, "us-ascii");
69
70 // create the Multipart and its parts to it
71 Multipart mp = new MimeMultipart();
72 mp.addBodyPart(mbp1);
73 mp.addBodyPart(mbp2);
74
75 // add the Multipart to the message
76 msg.setContent(mp);
77
78 // send the message
79 Transport.send(msg);
80 } catch (MessagingException mex) {
81 mex.printStackTrace();
82 Exception ex = null;
83 if ((ex = mex.getNextException()) != null) {
84 ex.printStackTrace();
85 }
86 }
87 }
88 }