1 package com.dotmarketing.util; 2 3 4 8 import java.io.UnsupportedEncodingException ; 9 import java.util.Properties ; 10 import javax.activation.DataHandler ; 11 import javax.activation.FileDataSource ; 12 import javax.mail.Message ; 13 import javax.mail.MessagingException ; 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 27 public class Mailer { 28 MimeMultipart sendingAttachments; 29 MimeMultipart sendingText; 30 String bcc; 31 String cc; 32 String errorMessage; 33 String fromEmail; 34 String fromName; 35 String result; 36 String subject; 37 String toEmail; 38 String toName; 39 long recipientId; 40 41 public Mailer() { 42 result = null; 43 sendingAttachments = new MimeMultipart (); 44 sendingText = new MimeMultipart (); 45 errorMessage = null; 46 } 47 48 public void setBcc(String x) { 49 bcc = x; 50 } 51 52 53 57 public String getBcc() { 58 return bcc; 59 } 60 61 public void setCc(String x) { 62 cc = x; 63 } 64 65 69 public String getCc() { 70 return cc; 71 } 72 73 77 public void setErrorMessage(String errorMessage) { 78 if(errorMessage != null) 79 80 this.errorMessage = errorMessage.trim(); 81 } 82 83 87 public String getErrorMessage() { 88 return errorMessage; 89 } 90 91 95 public void setFromEmail(String fromEmail) { 96 this.fromEmail = fromEmail; 97 } 98 99 103 public String getFromEmail() { 104 return fromEmail; 105 } 106 107 public void setFromName(String x) { 108 fromName = x; 109 } 110 111 115 public String getFromName() { 116 return fromName; 117 } 118 119 public void setHTMLAndTextBody(String HTMLBody) { 120 try { 121 MimeBodyPart bp = new MimeBodyPart (); 122 bp.setContent(HTMLBody, "text/html"); 123 sendingText.addBodyPart(bp); 124 bp = new MimeBodyPart (); 125 126 String x = HTMLBody.replaceAll("<[^>]*>", " "); 127 128 while (x.indexOf(" ") > -1) { 129 x = x.replaceAll(" ", " "); 130 } 131 132 setTextBody(x); 133 } catch (Exception f) { 134 Logger.error(this, "failed to setHTMLAndTextBody:" + f, f); 135 } 136 } 137 138 public void setHTMLBody(String HTMLBody) { 139 try { 140 MimeBodyPart bp = new MimeBodyPart (); 141 bp.setContent(HTMLBody, "text/html"); 142 sendingText.addBodyPart(bp); 143 } catch (Exception f) { 144 Logger.error(this, "failed to setHTMLBody:" + f, f); 145 } 146 } 147 148 152 public void setRecipientId(long recipientId) { 153 this.recipientId = recipientId; 154 } 155 156 160 public long getRecipientId() { 161 return recipientId; 162 } 163 164 168 public String getResult() { 169 return result; 170 } 171 172 public void setSubject(String x) { 173 subject = x; 174 } 175 176 180 public String getSubject() { 181 return subject; 182 } 183 184 public void setTextBody(String TextBody) { 185 try { 186 MimeBodyPart bp = new MimeBodyPart (); 187 bp.setContent(TextBody, "text/plain"); 188 sendingText.addBodyPart(bp, 0); 189 } catch (Exception f) { 190 Logger.error(this, "failed to setTextBody:" + f, f); 191 } 192 } 193 194 198 public void setToEmail(String toEmail) { 199 this.toEmail = toEmail; 200 } 201 202 206 public String getToEmail() { 207 return toEmail; 208 } 209 210 public void setToName(String x) { 211 toName = x; 212 } 213 214 218 public String getToName() { 219 return toName; 220 } 221 222 public void addAttachment(java.io.File file) { 223 if ((file != null) && file.exists() && !file.isDirectory()) { 224 try { 225 MimeBodyPart mbp = new MimeBodyPart (); 226 FileDataSource fds = new FileDataSource (file.getAbsolutePath()); 227 mbp.setDataHandler(new DataHandler (fds)); 228 mbp.setFileName(file.getName()); 229 sendingAttachments.addBodyPart(mbp); 230 } catch (Exception f) { 231 Logger.error(this, "failed to addAttachment:" + f, f); 232 } 233 } 234 } 235 236 239 public boolean sendMessage() { 240 MimeMultipart mp = new MimeMultipart (); 241 242 try { 243 if (sendingText.getCount() > 1) { 245 sendingText.setSubType("alternative"); 246 } 247 248 if (sendingAttachments.getCount() > 0) { 250 MimeBodyPart bp = new MimeBodyPart (); 251 bp.setContent(sendingAttachments); 252 mp.addBodyPart(bp, 0); 253 bp = new MimeBodyPart (); 254 bp.setContent(sendingText); 255 mp.addBodyPart(bp, 0); 256 } else { 257 mp = sendingText; 258 } 259 260 261 Properties props = System.getProperties(); 263 264 String smtpServer = Config.getStringProperty("SMTP_SERVER"); 266 props.put("mail.smtp.host", smtpServer); 267 268 Logger.debug(this, "Delivering mail using: " + smtpServer + " as server."); 269 270 Session session = Session.getDefaultInstance(props, null); 272 MimeMessage message = new MimeMessage (session); 273 274 if ((fromEmail != null) && (fromName != null)) { 275 message.setFrom(new InternetAddress (fromEmail, fromName)); 276 } else if (fromEmail != null) { 277 message.setFrom(new InternetAddress (fromEmail)); 278 } 279 280 if (toName != null) { 281 String [] recipients = toEmail.split("[;,]"); 282 for (String recipient : recipients) { 283 message.addRecipient(Message.RecipientType.TO, new InternetAddress (recipient, toName)); 284 } 285 } else { 286 String [] recipients = toEmail.split("[;,]"); 287 for (String recipient : recipients) { 288 message.addRecipient(Message.RecipientType.TO, new InternetAddress (recipient)); 289 } 290 } 291 if (UtilMethods.isSet(cc)) { 292 String [] recipients = cc.split("[;,]"); 293 for (String recipient : recipients) { 294 message.addRecipient(Message.RecipientType.CC, new InternetAddress (recipient)); 295 } 296 } 297 if (UtilMethods.isSet(bcc)) { 298 String [] recipients = bcc.split("[;,]"); 299 for (String recipient : recipients) { 300 message.addRecipient(Message.RecipientType.BCC, new InternetAddress (recipient)); 301 } 302 } 303 304 message.setSubject(subject); 305 message.setContent(mp); 306 Transport.send(message); 307 result = "Send Ok"; 308 return true; 309 } catch (javax.mail.SendFailedException f) { 310 String error = String.valueOf(f); 311 errorMessage = error.substring(error.lastIndexOf("javax.mail.SendFailedException:") + "javax.mail.SendFailedException:".length(), (error.length()-1)); 312 result = "Failed:" + error; 313 Logger.error(Mailer.class, f.toString(), f); 314 return false; 315 } catch (MessagingException f) { 316 String error = String.valueOf(f); 317 errorMessage = error.substring(error.lastIndexOf("javax.mail.MessagingException:")+ "javax.mail.MessagingException:".length(), (error.length()-1)); 318 result = "Failed:" + error; 319 Logger.error(Mailer.class, f.toString(), f); 320 return false; 321 } 322 catch (UnsupportedEncodingException f) { 323 String error = String.valueOf(f); 324 errorMessage = error.substring(error.lastIndexOf("java.io.UnsupportedEncodingException:") + "java.io.UnsupportedEncodingException:".length(), (error.length()-1)); 325 result = "Failed:" + error ; 326 Logger.error(Mailer.class, f.toString(), f); 327 return false; 328 } 329 330 331 332 333 334 335 } 336 } 337 | Popular Tags |