1 20 21 package com.methodhead.mail; 22 23 import java.io.File ; 24 import java.util.Properties ; 25 import javax.mail.internet.InternetAddress ; 26 import javax.mail.internet.AddressException ; 27 import org.apache.commons.mail.SimpleEmail; 28 import org.apache.commons.mail.MultiPartEmail; 29 import org.apache.commons.mail.HtmlEmail; 30 import org.apache.commons.mail.EmailException; 31 import org.apache.commons.mail.EmailAttachment; 32 import com.methodhead.MhfException; 33 import org.apache.commons.lang.exception.ExceptionUtils; 34 import org.apache.commons.lang.builder.ToStringBuilder; 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 38 42 public class Mail { 43 44 46 48 50 52 68 public static void init( 69 Properties props ) { 70 71 if ( logger_.isDebugEnabled() ) { 72 logger_.debug( 73 "Initializing with properties " + 74 new ToStringBuilder( props ). 75 append( "mail.host", props.get( "mail.host" ) ). 76 append( "mail.from", props.get( "mail.from" ) ). 77 toString() ); 78 } 79 80 mailProperties_ = props; 81 } 82 83 86 public static SimpleEmail newSimpleEmail() { 87 88 SimpleEmail email = new SimpleEmail(); 89 email.setHostName( mailProperties_.getProperty( "mail.host" ) ); 90 return email; 91 } 92 93 97 public static MultiPartEmail newMultiPartEmail() { 98 99 MultiPartEmail email = new MultiPartEmail(); 100 email.setHostName( mailProperties_.getProperty( "mail.host" ) ); 101 return email; 102 } 103 104 107 public static HtmlEmail newHtmlEmail() { 108 109 HtmlEmail email = new HtmlEmail(); 110 email.setHostName( mailProperties_.getProperty( "mail.host" ) ); 111 return email; 112 } 113 114 117 protected static boolean isValidAddress( 118 String address ) { 119 120 if ( address == null ) 121 return false; 122 123 try { 124 InternetAddress ia = new InternetAddress ( address ); 125 ia.validate(); 126 } 127 catch ( AddressException e ) { 128 return false; 129 } 130 131 return true; 132 } 133 134 141 public static void send( 142 String [] to, 143 String from, 144 String subject, 145 String text ) { 146 147 if ( logger_.isDebugEnabled() ) { 148 logger_.debug( "Sending plain text email from " + from + " with subject \"" + subject + "\", " + text.length() + " characters in body." ); 149 } 150 151 try { 152 for ( int i = 0; i < to.length; i++ ) { 153 if ( isValidAddress( to[ i ] ) ) { 154 155 if ( logger_.isDebugEnabled() ) { 156 logger_.debug( "Sending message to " + to[ i ] ); 157 } 158 159 SimpleEmail email = newSimpleEmail(); 160 email.addTo( to[ i ] ); 161 email.setFrom( from ); 162 email.setSubject( subject ); 163 email.setMsg( text ); 164 email.send(); 165 } 166 else { 167 if ( logger_.isDebugEnabled() ) { 168 logger_.debug( "Skipping invalid email address " + to[ i ] ); 169 } 170 } 171 } 172 } 173 catch ( EmailException e ) { 174 String msg = 175 "Sending plain text email from " + from + " with subject \"" + subject + "\", " + text.length() + " characters in body.\n" + 176 ExceptionUtils.getStackTrace( e ); 177 logger_.error( msg ); 178 throw new MhfException( msg ); 179 } 180 } 181 182 187 public static void send( 188 String to, 189 String from, 190 String subject, 191 String text ) 192 throws 193 EmailException { 194 195 send( new String [] { to }, from, subject, text ); 196 } 197 198 205 public static void send( 206 String [] to, 207 String from, 208 String subject, 209 String text, 210 String html ) { 211 212 send( to, from, subject, text, html, new File [] {} ); 213 } 214 215 223 public static void send( 224 String [] to, 225 String from, 226 String subject, 227 String text, 228 File [] attachments ) { 229 230 send( to, from, subject, text, null, attachments ); 231 } 232 233 243 public static void send( 244 String [] to, 245 String from, 246 String subject, 247 String text, 248 String html, 249 File [] attachments ) { 250 251 if ( logger_.isDebugEnabled() ) { 252 String textInfo = "no text"; 253 if ( text != null ) { 254 textInfo = "" + text.length() + " characters in text body"; 255 } 256 257 String htmlInfo = "no html"; 258 if ( html != null ) { 259 htmlInfo = "" + html.length() + " characters in html body"; 260 } 261 262 logger_.debug( 263 "Sending html email from " + from + " with subject \"" + subject + "\", " + textInfo + ", " + 264 htmlInfo + ", and attachments " + StringUtils.join( attachments ) ); 265 } 266 267 try { 268 if ( attachments == null ) 269 throw new EmailException( "Attachments array is null." ); 270 271 EmailAttachment[] emailAttachments = 275 new EmailAttachment[ attachments.length ]; 276 277 for ( int i = 0; i < attachments.length; i++ ) { 278 279 if ( 283 ( attachments[ i ] == null ) || 284 !attachments[ i ].exists() || 285 !attachments[ i ].isFile() ) 286 287 throw new EmailException( 288 "Attachment \"" + attachments[ i ] + 289 "\" does not exist or is not a file." ); 290 291 emailAttachments[ i ] = new EmailAttachment(); 292 emailAttachments[ i ].setPath( attachments[ i ].getAbsolutePath() ); 293 } 294 295 for ( int i = 0; i < to.length; i++ ) { 296 297 if ( isValidAddress( to[ i ] ) ) { 298 299 if ( logger_.isDebugEnabled() ) { 300 logger_.debug( "Sending to " + to[ i ] ); 301 } 302 303 HtmlEmail email = newHtmlEmail(); 304 email.addTo( to[ i ] ); 305 email.setFrom( from ); 306 email.setSubject( subject ); 307 308 if ( text != null ) 309 email.setTextMsg( text ); 310 311 if ( html != null ) 312 email.setHtmlMsg( html ); 313 314 for ( int j = 0; j < emailAttachments.length; j++ ) 318 email.attach( emailAttachments[ j ] ); 319 320 email.send(); 321 } 322 else { 323 if ( logger_.isDebugEnabled() ) { 324 logger_.debug( "Skipping invalid email address " + to[ i ] ); 325 } 326 } 327 } 328 } 329 catch ( EmailException e ) { 330 String msg = 331 "Sending html email from " + from + " to " + to + 332 " with subject \"" + subject + "\", text \"" + text + "\", html \"" + 333 html + "\", and attachments " + attachments + "\n" + 334 ExceptionUtils.getStackTrace( e ); 335 logger_.error( msg ); 336 throw new MhfException( msg ); 337 } 338 } 339 340 342 343 345 protected static Properties mailProperties_ = new Properties (); 346 347 private static Logger logger_ = Logger.getLogger( Mail.class ); 348 } 349 | Popular Tags |