1 38 39 import java.util.*; 40 import java.io.*; 41 import javax.mail.*; 42 import javax.mail.internet.*; 43 import javax.activation.*; 44 45 58 public class msgmultisendsample { 59 static String msgText1 = "This is a message body.\nHere's line two."; 60 static String msgText2 = "This is the text in the message attachment."; 61 62 public static void main(String [] args) { 63 if (args.length != 4) { 64 System.out.println("usage: java msgmultisend <to> <from> <smtp> true|false"); 65 return; 66 } 67 68 String to = args[0]; 69 String from = args[1]; 70 String host = args[2]; 71 boolean debug = Boolean.valueOf(args[3]).booleanValue(); 72 73 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 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 MimeBodyPart mbp1 = new MimeBodyPart(); 91 mbp1.setText(msgText1); 92 93 MimeBodyPart mbp2 = new MimeBodyPart(); 95 mbp2.setText(msgText2, "us-ascii"); 97 98 Multipart mp = new MimeMultipart(); 100 mp.addBodyPart(mbp1); 101 mp.addBodyPart(mbp2); 102 103 msg.setContent(mp); 105 106 Transport.send(msg); 108 } catch (MessagingException mex) { 109 mex.printStackTrace(); 110 Exception ex = null; 111 if ((ex = mex.getNextException()) != null) { 112 ex.printStackTrace(); 113 } 114 } 115 } 116 } 117 | Popular Tags |