1 10 package org.mmbase.applications.email; 11 12 import java.util.*; 13 14 import javax.mail.*; 15 import javax.mail.internet.*; 16 import javax.naming.*; 17 18 import org.mmbase.module.core.MMBase; 19 import org.mmbase.util.logging.*; 20 21 30 public class SendMail extends AbstractSendMail implements SendMailInterface { 31 private static final Logger log = Logging.getLoggerInstance(SendMail.class); 32 33 public static final String DEFAULT_MAIL_ENCODING = "ISO-8859-1"; 34 35 public static String mailEncoding = DEFAULT_MAIL_ENCODING; 36 37 public static long emailSent = 0; 38 public static long emailFailed = 0; 39 40 42 public boolean sendMultiPartMail(String from, String to, Map headers, MimeMultipart mmpart) { 43 try { 44 45 MimeMessage msg = constructMessage(from, to, headers); 46 47 msg.setContent(mmpart); 48 49 Transport.send(msg); 50 51 emailSent++; 52 log.debug("JMimeSendMail done."); 53 return true; 54 } catch (javax.mail.MessagingException e) { 55 emailFailed++; 56 log.error("JMimeSendMail failure: " + e.getMessage()); 57 log.debug(Logging.stackTrace(e)); 58 } 59 return false; 60 } 61 62 65 public String getModuleInfo() { 66 return ("Sends mail through J2EE/JavaMail, supporting MultiPart"); 67 } 68 69 70 protected Session session; 71 72 74 public void reload() { 75 init(); 76 } 77 78 81 public void init() { 82 try { 83 MMBase mmb = MMBase.getMMBase(); 84 mailEncoding = mmb.getEncoding(); 85 String encoding = getInitParameter("encoding"); 86 if (encoding != null && !encoding.equals("")) 87 mailEncoding = encoding; 88 89 String smtpHost = getInitParameter("mailhost"); 90 String context = getInitParameter("context"); 91 String dataSource = getInitParameter("datasource"); 92 session = null; 93 if (smtpHost == null) { 94 if (context == null) { 95 context = "java:comp/env"; 96 log.warn("The property 'context' is missing, taking default " + context); 97 } 98 if (dataSource == null) { 99 dataSource = "mail/Session"; 100 log.warn("The property 'datasource' is missing, taking default " + dataSource); 101 } 102 103 Context initCtx = new InitialContext(); 104 Context envCtx = (Context)initCtx.lookup(context); 105 Object o = envCtx.lookup(dataSource); 106 if (o instanceof Session) { 107 session = (javax.mail.Session ) o; 108 } else { 109 log.fatal("Configured dataSource '" + dataSource + "' of context '" + context + "' is not a Session but " + (o == null ? "NULL" : "a " + o.getClass().getName())); 110 return; 111 } 112 log.info("Module SendMail started (datasource = " + dataSource + " -> " + session.getProperties() + ")"); 113 } else { 114 if (context != null) { 115 log.error("It does not make sense to have both properties 'context' and 'mailhost' in email module"); 116 } 117 if (dataSource != null) { 118 log.error("It does not make sense to have both properties 'datasource' and 'mailhost' in email module"); 119 } 120 log.info( 121 "EMail module is configured using 'mailhost' property.\n" 122 + "Consider using J2EE compliant 'context' and 'datasource'\n" 123 + "Which means to put something like this in your web.xml:\n" 124 + " <resource-ref>\n" 125 + " <description>Email module mail resource</description>\n" 126 + " <res-ref-name>mail/MMBase</res-ref-name>\n" 127 + " <res-type>javax.mail.Session</res-type>\n" 128 + " <res-auth>Container</res-auth>\n" 129 + " </resource-ref>\n" 130 + " + some app-server specific configuration (e.g. in orion the 'mail-session' entry in the application XML)"); 131 132 Properties prop = System.getProperties(); 133 prop.put("mail.smtp.host", smtpHost); 134 session = Session.getInstance(prop, null); 135 log.info("Module SendMail started (smtphost = " + smtpHost + ")"); 136 } 137 138 } catch (javax.naming.NamingException e) { 139 log.fatal("SendMail failure: " + e.getMessage()); 140 log.debug(Logging.stackTrace(e)); 141 } 142 } 143 144 147 protected MimeMessage constructMessage(String from, String to, Map headers) throws MessagingException { 148 if (log.isServiceEnabled()) { 149 log.service("SendMail sending mail to " + to); 150 } 151 MimeMessage msg = new MimeMessage(session); 153 if (from != null && !from.equals("")) { 154 msg.addFrom(InternetAddress.parse(from)); 155 } 156 157 msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); 158 159 if (headers.get("CC") != null) { 160 msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse((String )headers.get("CC"))); 161 } 162 if (headers.get("BCC") != null) { 163 msg.addRecipients(Message.RecipientType.CC, InternetAddress.parse((String )headers.get("BCC"))); 164 } 165 166 if (headers.get("Reply-To") != null) { 167 msg.setReplyTo(InternetAddress.parse((String )headers.get("Reply-To"))); 168 } 169 170 msg.setSubject((String )headers.get("Subject")); 171 172 return msg; 173 } 174 175 178 public boolean sendMail(String from, String to, String data, Map headers) { 179 try { 180 MimeMessage msg = constructMessage(from, to, headers); 181 182 msg.setText(data, mailEncoding); 183 Transport.send(msg); 184 log.debug("SendMail done."); 185 return true; 186 } catch (MessagingException e) { 187 log.error("SendMail failure: " + e.getMessage() + "from: " + from + " to: " + to); 188 log.debug(Logging.stackTrace(e)); 189 } 190 return false; 191 } 192 } 193 | Popular Tags |