1 18 package org.apache.roller.util; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import javax.mail.Message ; 24 import javax.mail.MessagingException ; 25 import javax.mail.SendFailedException ; 26 import javax.mail.Session ; 27 import javax.mail.Transport ; 28 import javax.mail.Address ; 29 import javax.mail.internet.InternetAddress ; 30 import javax.mail.internet.MimeMessage ; 31 import org.apache.commons.lang.StringUtils; 32 33 public class MailUtil extends Object { 34 35 private static Log mLogger = 36 LogFactory.getFactory().getInstance(MailUtil.class); 37 38 40 51 public static void sendMessage 52 ( 53 Session session, 54 String from, 55 String [] to, 56 String [] cc, 57 String [] bcc, 58 String subject, 59 String content, 60 String mimeType 61 ) 62 throws MessagingException 63 { 64 Message message = new MimeMessage (session); 65 66 if (! StringUtils.isEmpty(from)) { 68 InternetAddress sentFrom = new InternetAddress (from); 69 message.setFrom(sentFrom); 70 if (mLogger.isDebugEnabled()) mLogger.debug("e-mail from: " + sentFrom); 71 } 72 73 if (to!=null) 74 { 75 InternetAddress [] sendTo = new InternetAddress [to.length]; 76 77 for (int i = 0; i < to.length; i++) 78 { 79 sendTo[i] = new InternetAddress (to[i]); 80 if (mLogger.isDebugEnabled()) mLogger.debug("sending e-mail to: " + to[i]); 81 } 82 message.setRecipients(Message.RecipientType.TO, sendTo); 83 } 84 85 if (cc != null) 86 { 87 InternetAddress [] copyTo = new InternetAddress [cc.length]; 88 89 for (int i = 0; i < cc.length; i++) 90 { 91 copyTo[i] = new InternetAddress (cc[i]); 92 if (mLogger.isDebugEnabled()) mLogger.debug("copying e-mail to: " + cc[i]); 93 } 94 message.setRecipients(Message.RecipientType.CC, copyTo); 95 } 96 97 if (bcc != null) 98 { 99 InternetAddress [] copyTo = new InternetAddress [bcc.length]; 100 101 for (int i = 0; i < bcc.length; i++) 102 { 103 copyTo[i] = new InternetAddress (bcc[i]); 104 if (mLogger.isDebugEnabled()) mLogger.debug("blind copying e-mail to: " + bcc[i]); 105 } 106 message.setRecipients(Message.RecipientType.BCC, copyTo); 107 } 108 message.setSubject((subject == null) ? "(no subject)" : subject); 109 message.setContent(content, mimeType); 110 message.setSentDate(new java.util.Date ()); 111 112 Address [] remainingAddresses = message.getAllRecipients(); 114 int nAddresses = remainingAddresses.length; 115 boolean bFailedToSome = false; 116 117 SendFailedException sendex = new SendFailedException ("Unable to send message to some recipients"); 118 119 do 121 { 122 nAddresses = remainingAddresses.length; 124 125 try 126 { 127 Transport.send(message,remainingAddresses); 129 } 130 catch(SendFailedException ex) 131 { 132 bFailedToSome=true; 133 sendex.setNextException(ex); 134 135 remainingAddresses=ex.getValidUnsentAddresses(); 137 } 138 } while (remainingAddresses!=null && remainingAddresses.length>0 && remainingAddresses.length!=nAddresses); 139 140 if (bFailedToSome) throw sendex; 141 } 142 143 152 public static void sendTextMessage 153 ( 154 Session session, 155 String from, 156 String [] to, 157 String [] cc, 158 String [] bcc, 159 String subject, 160 String content 161 ) 162 throws MessagingException 163 { 164 sendMessage(session, from, to, cc, bcc, subject, content, "text/plain; charset=utf-8"); 165 } 166 167 177 public static void sendTextMessage 178 ( 179 Session session, 180 String from, 181 String to, 182 String [] cc, 183 String [] bcc, 184 String subject, 185 String content 186 ) 187 throws MessagingException 188 { 189 String [] recipient = null; 190 if (to!=null) recipient = new String [] {to}; 191 192 sendMessage(session, from, recipient, cc, bcc, subject, content, "text/plain; charset=utf-8"); 193 } 194 195 207 public static void sendTextMessage 208 ( 209 Session session, 210 String from, 211 String to, 212 String cc, 213 String bcc, 214 String subject, 215 String content 216 ) 217 throws MessagingException 218 { 219 String [] recipient = null; 220 String [] copy = null; 221 String [] bcopy = null; 222 223 if (to!=null) recipient = new String [] {to}; 224 if (cc!=null) copy = new String [] {cc}; 225 if (bcc!=null) bcopy = new String [] {bcc}; 226 227 sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/plain; charset=utf-8"); 228 } 229 230 239 public static void sendHTMLMessage 240 ( 241 Session session, 242 String from, 243 String [] to, 244 String [] cc, 245 String [] bcc, 246 String subject, 247 String content 248 ) 249 throws MessagingException 250 { 251 sendMessage(session, from, to, cc, bcc, subject, content, "text/html; charset=utf-8"); 252 } 253 254 264 public static void sendHTMLMessage 265 ( 266 Session session, 267 String from, 268 String to, 269 String cc, 270 String bcc, 271 String subject, 272 String content 273 ) 274 throws MessagingException 275 { 276 String [] recipient = null; 277 String [] copy = null; 278 String [] bcopy = null; 279 280 if (to!=null) recipient = new String [] {to}; 281 if (cc!=null) copy = new String [] {cc}; 282 if (bcc!=null) bcopy = new String [] {bcc}; 283 284 sendMessage(session, from, recipient, copy, bcopy, subject, content, "text/html; charset=utf-8"); 285 } 286 287 298 public static void sendHTMLMessage 299 ( 300 Session session, 301 String from, 302 String to, 303 String [] cc, 304 String [] bcc, 305 String subject, 306 String content 307 ) 308 throws MessagingException 309 { 310 String [] recipient = null; 311 if (to!=null) recipient = new String [] {to}; 312 313 sendMessage(session, from, recipient, cc, bcc, subject, content, "text/html; charset=utf-8"); 314 } 315 } 316 317 | Popular Tags |