1 12 13 package com.openedit.modules.email; 14 15 import java.io.UnsupportedEncodingException ; 16 import java.util.Date ; 17 import java.util.Properties ; 18 19 import javax.mail.BodyPart ; 20 import javax.mail.Message ; 21 import javax.mail.MessagingException ; 22 import javax.mail.Multipart ; 23 import javax.mail.PasswordAuthentication ; 24 import javax.mail.Session ; 25 import javax.mail.Transport ; 26 import javax.mail.internet.InternetAddress ; 27 import javax.mail.internet.MimeBodyPart ; 28 import javax.mail.internet.MimeMessage ; 29 import javax.mail.internet.MimeMultipart ; 30 31 import com.openedit.OpenEditRuntimeException; 32 33 34 public class PostMail 35 { 36 protected String fieldSmtpUsername; 37 protected String fieldSmtpPassword; 38 protected String fieldSmtpServer="localhost"; 39 protected int fieldPort = 25; 40 protected boolean fieldSmtpSecured=false; 41 42 public String getSmtpPassword() { 43 return fieldSmtpPassword; 44 } 45 46 public void setSmtpPassword(String inSmtpPassword) { 47 this.fieldSmtpPassword = inSmtpPassword; 48 } 49 50 public boolean isSmtpSecured() { 51 return fieldSmtpSecured; 52 } 53 54 public void setSmtpSecured(boolean inSmtpSecured) { 55 this.fieldSmtpSecured = inSmtpSecured; 56 } 57 58 public String getSmtpUsername() { 59 return fieldSmtpUsername; 60 } 61 62 public void setSmtpUsername(String inSmtpUsername) { 63 this.fieldSmtpUsername = inSmtpUsername; 64 } 65 66 public void postMail(String recipient, String subject, String message, String from) 67 throws MessagingException 68 { 69 postMail(new String [] { recipient }, subject, message, null, from); 70 } 71 72 public TemplateWebEmail getTemplateWebEmail(){ 74 TemplateWebEmail email = new TemplateWebEmail(); 75 email.setPostMail(this); 76 return email; 77 } 78 79 public void postMail( 80 String [] recipients, String subject, String inHtml, String inText, String from) 81 throws MessagingException 82 { 83 Properties props = new Properties (); 85 props.put("mail.smtp.host",fieldSmtpServer); 87 props.put("mail.smtp.port", String.valueOf(fieldPort) ); 88 props.put("mail.smtp.auth",new Boolean (fieldSmtpSecured).toString()); 89 Session session; 91 if(fieldSmtpSecured){ 93 94 SmtpAuthenticator auth = new SmtpAuthenticator(); 95 session = Session.getDefaultInstance(props,auth); 96 } 97 else{ 98 session = Session.getDefaultInstance(props); 99 } 100 102 Message msg = new MimeMessage (session); 104 105 if ( inText != null && inHtml != null) 107 { 108 Multipart mp = new MimeMultipart ("alternative"); 110 111 BodyPart messageBodyPart = new MimeBodyPart (); 112 messageBodyPart.setContent(inText, "text/plain"); 113 mp.addBodyPart(messageBodyPart); 114 115 messageBodyPart = new MimeBodyPart (); 116 messageBodyPart.setContent(inHtml, "text/html"); 117 mp.addBodyPart(messageBodyPart); 118 119 msg.setContent(mp); 120 } 121 else if ( inHtml != null) 122 { 123 msg.setContent(inHtml,"text/html"); 124 } 125 else 126 { 127 msg.setContent(inText,"text/plain"); 128 } 129 InternetAddress addressFrom = new InternetAddress (from); 131 msg.setFrom(addressFrom); 132 msg.setSentDate(new Date ()); 133 InternetAddress [] addressTo = new InternetAddress [recipients.length]; 134 135 for (int i = 0; i < recipients.length; i++) 136 { 137 String rec = recipients[i]; 138 addressTo[i] = new InternetAddress (rec); 139 String personal = addressTo[i].getPersonal(); 140 if ( personal != null && personal.indexOf("\"") == -1) 141 { 142 if ( personal.indexOf(",") > -1 || personal.indexOf(".") > -1 ) 144 { 145 personal = "\"" + personal + "\""; 146 try{ 147 addressTo[i].setPersonal(personal); 148 } catch ( UnsupportedEncodingException ex) 149 { 150 throw new OpenEditRuntimeException(ex); 151 } 152 } 153 } 154 } 155 156 msg.setRecipients(Message.RecipientType.TO, addressTo); 157 158 msg.setSubject(subject); 162 163 170 Transport.send(msg); 171 } 172 173 public int getPort() { 174 return fieldPort; 175 } 176 177 public void setPort(int inPort) { 178 this.fieldPort = inPort; 179 } 180 181 public String getSmtpServer() { 182 return fieldSmtpServer; 183 } 184 185 public void setSmtpServer(String inSmtpServer) { 186 this.fieldSmtpServer = inSmtpServer; 187 } 188 public class SmtpAuthenticator extends javax.mail.Authenticator { 189 public PasswordAuthentication getPasswordAuthentication() { 190 return new PasswordAuthentication (fieldSmtpUsername, fieldSmtpPassword); 191 } 192 } 193 } 194 | Popular Tags |