1 package org.roller.util; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 6 import javax.mail.Message ; 7 import javax.mail.MessagingException ; 8 import javax.mail.SendFailedException ; 9 import javax.mail.Session ; 10 import javax.mail.Transport ; 11 import javax.mail.Address ; 12 import javax.mail.internet.InternetAddress ; 13 import javax.mail.internet.MimeMessage ; 14 15 16 public class MailUtil extends Object { 17 18 private static Log mLogger = 19 LogFactory.getFactory().getInstance(MailUtil.class); 20 21 23 34 public static void sendMessage 35 ( 36 Session session, 37 String from, 38 String [] to, 39 String [] cc, 40 String [] bcc, 41 String subject, 42 String content, 43 String mimeType 44 ) 45 throws MessagingException 46 { 47 Message message = new MimeMessage (session); 48 49 if (! StringUtils.isEmpty(from)) { 51 InternetAddress sentFrom = new InternetAddress (from); 52 message.setFrom(sentFrom); 53 if (mLogger.isDebugEnabled()) mLogger.debug("e-mail from: " + sentFrom); 54 } 55 56 if (to!=null) 57 { 58 InternetAddress [] sendTo = new InternetAddress [to.length]; 59 60 for (int i = 0; i < to.length; i++) 61 { 62 sendTo[i] = new InternetAddress (to[i]); 63 if (mLogger.isDebugEnabled()) mLogger.debug("sending e-mail to: " + to[i]); 64 } 65 message.setRecipients(Message.RecipientType.TO, sendTo); 66 } 67 68 if (cc != null) 69 { 70 InternetAddress [] copyTo = new InternetAddress [cc.length]; 71 72 for (int i = 0; i < cc.length; i++) 73 { 74 copyTo[i] = new InternetAddress (cc[i]); 75 if (mLogger.isDebugEnabled()) mLogger.debug("copying e-mail to: " + cc[i]); 76 } 77 message.setRecipients(Message.RecipientType.CC, copyTo); 78 } 79 80 if (bcc != null) 81 { 82 InternetAddress [] copyTo = new InternetAddress [bcc.length]; 83 84 for (int i = 0; i < bcc.length; i++) 85 { 86 copyTo[i] = new InternetAddress (bcc[i]); 87 if (mLogger.isDebugEnabled()) mLogger.debug("blind copying e-mail to: " + bcc[i]); 88 } 89 message.setRecipients(Message.RecipientType.BCC, copyTo); 90 } 91 message.setSubject((subject == null) ? "(no subject)" : subject); 92 message.setContent(content, mimeType); 93 94 Address [] remainingAddresses = message.getAllRecipients(); 96 int nAddresses = remainingAddresses.length; 97 boolean bFailedToSome = false; 98 99 SendFailedException sendex = new SendFailedException ("Unable to send message to some recipients"); 100 101 do 103 { 104 nAddresses = remainingAddresses.length; 106 107 try 108 { 109 Transport.send(message,remainingAddresses); 111 } 112 catch(SendFailedException ex) 113 { 114 bFailedToSome=true; 115 sendex.setNextException(ex); 116 117 remainingAddresses=ex.getValidUnsentAddresses(); 119 } 120 } while (remainingAddresses!=null && remainingAddresses.length>0 && remainingAddresses.length!=nAddresses); 121 122 if (bFailedToSome) throw sendex; 123 } 124 125 134 public static void sendTextMessage 135 ( 136 Session session, 137 String from, 138 String [] to, 139 String [] cc, 140 String [] bcc, 141 String subject, 142 String content 143 ) 144 throws MessagingException 145 { 146 sendMessage(session, from, to, cc, bcc, subject, content, "text/plain; charset=utf-8"); 147 } 148 149 159 public static void sendTextMessage 160 ( 161 Session session, 162 String from, 163 String to, 164 String [] cc, 165 String [] bcc, 166 String subject, 167 String content 168 ) 169 throws MessagingException 170 { 171 String [] recipient = null; 172 if (to!=null) recipient = new String [] {to}; 173 174 sendMessage(session, from, recipient, cc, bcc, subject, content, "text/plain; charset=utf-8"); 175 } 176 177 189 public static void sendTextMessage 190 ( 191 Session session, 192 String from, 193 String to, 194 String cc, 195 String bcc, 196 String subject, 197 String content 198 ) 199 throws MessagingException 200 { 201 String [] recipient = null; 202 String [] copy = null; 203 String [] bcopy = null; 204 205 if (to!=null) recipient = new String [] {to}; 206 if (cc!=null) copy = new String [] {cc}; 207 if (bcc!=null) bcopy = new String [] {bcc}; 208 209 sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/plain; charset=utf-8"); 210 } 211 212 221 public static void sendHTMLMessage 222 ( 223 Session session, 224 String from, 225 String [] to, 226 String [] cc, 227 String [] bcc, 228 String subject, 229 String content 230 ) 231 throws MessagingException 232 { 233 sendMessage(session, from, to, cc, bcc, subject, content, "text/html; charset=utf-8"); 234 } 235 236 246 public static void sendHTMLMessage 247 ( 248 Session session, 249 String from, 250 String to, 251 String cc, 252 String bcc, 253 String subject, 254 String content 255 ) 256 throws MessagingException 257 { 258 String [] recipient = null; 259 String [] copy = null; 260 String [] bcopy = null; 261 262 if (to!=null) recipient = new String [] {to}; 263 if (cc!=null) copy = new String [] {cc}; 264 if (bcc!=null) bcopy = new String [] {bcc}; 265 266 sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/html; charset=utf-8"); 267 } 268 269 280 public static void sendHTMLMessage 281 ( 282 Session session, 283 String from, 284 String to, 285 String [] cc, 286 String [] bcc, 287 String subject, 288 String content 289 ) 290 throws MessagingException 291 { 292 String [] recipient = null; 293 if (to!=null) recipient = new String [] {to}; 294 295 sendMessage(session, from, recipient, cc, bcc, subject, content, "text/html; charset=utf-8"); 296 } 297 } 298 299 | Popular Tags |