1 13 package org.jahia.services.mail; 14 15 import java.io.PrintWriter ; 16 import java.io.StringWriter ; 17 import java.util.Date ; 18 import java.util.Properties ; 19 20 import javax.mail.Message ; 21 import javax.mail.Multipart ; 22 import javax.mail.Session ; 23 import javax.mail.Transport ; 24 import javax.mail.internet.InternetAddress ; 25 import javax.mail.internet.MimeMessage ; 26 27 import org.jahia.bin.JahiaInterface; 28 import org.jahia.exceptions.JahiaException; 29 import org.jahia.services.JahiaInitializableService; 30 import org.jahia.settings.SettingsBean; 31 32 43 public class MailService extends JahiaInitializableService { 44 45 53 public static synchronized MailService getInstance() 54 throws JahiaException { 55 if (singletonInstance == null) { 56 singletonInstance = new MailService(); 57 } 58 return singletonInstance; 59 } 60 61 66 public void init(SettingsBean jSettings) { 67 logger.debug("Start Mail Service"); 68 this.to = jSettings.mail_administrator; 69 if (this.to == null) { 70 this.to = null; 71 } 72 this.from = jSettings.mail_from; 73 if (this.from == null) { 74 this.from = null; 75 } 76 this.mailhost = jSettings.mail_server; 77 if (this.mailhost.equals("") || to.equals("") || from.equals("")) { 78 logger.warn("Mail settings not valid, ignoring..."); 79 } 80 if (this.mailhost == null || to == null || from == null) { 81 logger.warn("Mail settings not valid, ignoring..."); 82 } 83 logger.debug("Using default settings mailhost=[" + this.mailhost + 84 "] to=[" + to + "] from=[" + from + "]"); 85 } 86 87 94 public boolean sendMessage(String message) { 95 return sendMessage(this.from, this.to, null, null, 96 this.subject, this.mailhost, message, false); 97 } 98 99 106 public boolean sendMessage(String to, String message) { 107 return sendMessage(this.from, to, null, null, 108 this.subject, this.mailhost, message, false); 109 } 110 111 119 public boolean sendMessage(String from, String to, String message) { 120 return sendMessage(from, to, null, null, 121 this.subject, this.mailhost, message, false); 122 } 123 124 136 public boolean sendMessage(String from, String to, String cc, String bcc, 137 String subject, String message) { 138 return sendMessage(from, to, cc, bcc, 139 subject, this.mailhost, message, false); 140 } 141 142 155 public boolean sendMessage(String from, String to, String cc, String bcc, 156 String subject, String message, boolean htmlFormat) { 157 return sendMessage(from, to, cc, bcc, 158 subject, this.mailhost, message, htmlFormat); 159 } 160 161 175 public boolean sendMessage(String from, String to, String cc, String bcc, 176 String subject, String mailhost, String message, boolean htmlFormat) { 177 try { 178 if (to == null) { 179 to = defaultRecipient(); 180 } 181 if (from == null) { 182 from = defaultSender(); 183 } 184 185 if (mailhost == null || to == null || from == null) { 186 logger.debug("Mail settings not valid, ignoring..."); 187 return false; 188 } 189 if (mailhost.equals("") || to.equals("") || from.equals("")) { 190 logger.debug("Mail settings not valid, ignoring..."); 191 return false; 192 } 193 logger.debug("Send mail using settings mailhost=[" + mailhost + 194 "] to=[" + to + "] from=[" + from + "]"); 195 Properties props = System.getProperties(); 196 props.put("mail.smtp.host", mailhost); 197 Session session = Session.getDefaultInstance(props, null); 199 Message msg = new MimeMessage (session); 201 if (from != null) { 202 msg.setFrom(new InternetAddress (from)); 203 } else { 204 msg.setFrom(); 205 } 206 msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); 207 if (cc != null) { 208 msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); 209 } 210 if (bcc != null) { 211 msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); 212 } 213 if (subject == null || "".equals(subject)) { 214 subject = "[JAHIA] Jahia Message"; 215 } 216 msg.setSubject(subject); 217 218 StringWriter msgBodyWriter = new StringWriter (); 219 PrintWriter strOut = new PrintWriter (msgBodyWriter); 220 strOut.println(message); 221 222 if (!htmlFormat) 223 msg.setText(msgBodyWriter.toString()); 224 else 225 msg.setContent(msgBodyWriter.toString(), "text/html"); 226 227 msg.setHeader("X-Mailer", mailer); 228 msg.setSentDate(new Date ()); 229 logger.debug("Mailing to " + to + " via " + mailhost + "..."); 230 231 sendMessage(msg); 233 logger.debug("Mail was sent successfully."); 234 } catch (Throwable th) { 235 logger.debug("Error while sending mail : " + th.getMessage(), th); 236 return false; 237 } 238 return true; 239 } 240 241 247 public boolean sendMessage(Message message) { 248 try { 249 Transport.send(message); 250 } catch (Throwable th) { 251 logger.debug("Error while sending mail : " + th.getMessage(), th); 252 return false; 253 } 254 return true; 255 } 256 257 269 public boolean sendMessage( 270 String from, 271 String to, 272 String subject, 273 Multipart multipart) { 274 try { 275 if (to == null) { 276 to = defaultRecipient(); 277 } 278 if (from == null) { 279 from = defaultSender(); 280 } 281 282 if (mailhost == null || to == null || from == null) { 283 logger.debug("*** sendMessage : Mail settings not valid, ignoring..."); 284 return false; 285 } 286 if (mailhost.equals("") || to.equals("") || from.equals("")) { 287 logger.debug("*** sendMessage : Mail settings not valid, ignoring..."); 288 return false; 289 } 290 logger.debug( 291 "Send mail using settings mailhost=[" 292 + mailhost 293 + "] to=[" 294 + to 295 + "] from=[" 296 + from 297 + "]"); 298 Properties props = System.getProperties(); 299 props.put("mail.smtp.host", mailhost); 300 Session session = Session.getDefaultInstance(props, null); 302 MimeMessage msg = new MimeMessage (session); 304 305 306 if (from != null) { 307 msg.setFrom(new InternetAddress (from)); 308 } else { 309 msg.setFrom(); 310 } 311 msg.setRecipients( 312 Message.RecipientType.TO, 313 InternetAddress.parse(to, false)); 314 315 if (subject == null || "".equals(subject)) { 316 subject = "[JAHIA] Jahia Message"; 317 } 318 319 msg.setSubject(subject, "ISO-8859-1"); 320 msg.setHeader("X-Mailer", mailer); 321 msg.setSentDate(new Date ()); 322 323 if (multipart != null) { 324 msg.setContent(multipart); 325 } 326 327 Transport.send(msg); 328 329 logger.debug("*** sendMessage : Mailing to " + to + " via " + mailhost + "..."); 331 332 logger.debug("*** sendMessage : Mail was sent successfully."); 335 336 } catch (Throwable th) { 337 logger.error("*** sendMessage : Error while sending mail : " + th.getMessage(), th); 338 return false; 339 } 340 return true; 341 } 342 343 public String defaultRecipient() { 344 return this.to; 345 } 346 347 public String defaultSender() { 348 return this.from; 349 } 350 351 private String to; 353 private String subject; 354 private String from; 355 private String mailhost; 356 private String mailer = "Jahia Server v." + JahiaInterface.RELEASE_NUMBER + 357 "." + JahiaInterface.SERVICE_PACK_NUMBER + 358 " build " + JahiaInterface.BUILD_NUMBER; 359 360 static private MailService singletonInstance = null; 361 362 private static org.apache.log4j.Logger logger = 363 org.apache.log4j.Logger.getLogger(MailService.class); 364 365 } | Popular Tags |