1 3 package jodd.mail; 4 5 import java.util.Date ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Properties ; 9 10 import javax.mail.Authenticator ; 11 import javax.mail.Message ; 12 import javax.mail.MessagingException ; 13 import javax.mail.Multipart ; 14 import javax.mail.Session ; 15 import javax.mail.Transport ; 16 import javax.mail.internet.InternetAddress ; 17 import javax.mail.internet.MimeBodyPart ; 18 import javax.mail.internet.MimeMessage ; 19 import javax.mail.internet.MimeMultipart ; 20 21 26 public class SendMail { 27 28 private static SmtpServer defaultSmtpServer = null; 29 34 public static void setDefaultSmtpServer(SmtpServer server) { 35 defaultSmtpServer = server; 36 } 37 42 public static SmtpServer getDefaultSmtpServer() { 43 return defaultSmtpServer; 44 } 45 46 47 49 56 public static void send(Email email) throws MessagingException { 57 send(email, null); 58 } 59 60 68 public static void send(Email email, SmtpServer smtpServer) throws MessagingException { 69 if (smtpServer == null) { 70 smtpServer = defaultSmtpServer; 71 } 72 73 Properties props = new Properties (); 74 props.put("mail.smtp.host", smtpServer.getHost()); 75 Authenticator auth = null; 76 if (smtpServer.getUsername() != null) { 77 props.put("mail.smtp.auth", "true"); 78 auth = new SmtpSimpleAuthenticator(smtpServer.getUsername(), smtpServer.getPassword()); 79 } 80 Session session = Session.getInstance(props, auth); 81 82 Message msg = new MimeMessage (session); 83 msg.setFrom(new InternetAddress (email.getFrom())); 84 85 int totalTo = email.getTo().length; 86 InternetAddress [] address = new InternetAddress [totalTo]; 87 for (int i = 0; i < totalTo; i++) { 88 address[i] = new InternetAddress (email.getTo()[i]); 89 } 90 msg.setRecipients(Message.RecipientType.TO, address); 91 92 if (email.getCc() != null) { 93 int totalCc = email.getCc().length; 94 address = new InternetAddress [totalCc]; 95 for (int i = 0; i < totalCc; i++) { 96 address[i] = new InternetAddress (email.getCc()[i]); 97 } 98 msg.setRecipients(Message.RecipientType.CC, address); 99 } 100 if (email.getBcc() != null) { 101 int totalBcc = email.getBcc().length; 102 address = new InternetAddress [totalBcc]; 103 for (int i = 0; i < totalBcc; i++) { 104 address[i] = new InternetAddress (email.getBcc()[i]); 105 } 106 msg.setRecipients(Message.RecipientType.BCC, address); 107 } 108 109 msg.setSubject(email.getSubject()); 110 Date date = email.getSentDate(); 111 if (date == null) { 112 date = new Date (); 113 } 114 msg.setSentDate(date); 115 HashMap headers = email.getHeaders(); 116 Iterator it = headers.keySet().iterator(); 117 while (it.hasNext()) { 118 String name = (String ) it.next(); 119 String value = (String ) headers.get(name); 120 msg.setHeader(name, value); 121 } 122 123 124 MimeBodyPart messageData = new MimeBodyPart (); 125 126 if (email.isHtmlMessage()) { 127 messageData.setContent(email.getMessage(), "text/html"); 128 } else { 129 messageData.setText(email.getMessage()); 132 } 133 134 Multipart multipart = new MimeMultipart (); 135 multipart.addBodyPart(messageData); 136 137 int totalAttachments = email.getTotalAttachments(); 138 for (int i = 0; i < totalAttachments; i++) { 139 multipart.addBodyPart(email.getAttachmentBodyPart(i)); 140 } 141 142 msg.setContent(multipart); 143 Transport.send(msg); 144 } 145 146 } 147 | Popular Tags |