1 43 package net.jforum.util.mail; 44 45 import java.io.StringWriter ; 46 import java.util.Date ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 import java.util.Properties ; 50 51 import javax.mail.Address ; 52 import javax.mail.Message ; 53 import javax.mail.MessagingException ; 54 import javax.mail.Session ; 55 import javax.mail.Transport ; 56 import javax.mail.internet.InternetAddress ; 57 import javax.mail.internet.MimeMessage ; 58 59 import net.jforum.JForumExecutionContext; 60 import net.jforum.util.preferences.ConfigKeys; 61 import net.jforum.util.preferences.SystemGlobals; 62 63 import org.apache.log4j.Logger; 64 65 import freemarker.template.SimpleHash; 66 import freemarker.template.Template; 67 68 75 public class Spammer 76 { 77 private static final Logger logger = Logger.getLogger(Spammer.class); 78 79 private static int MESSAGE_HTML = 0; 80 private static int MESSAGE_TEXT = 1; 81 82 private Properties mailProps = new Properties (); 83 private static int messageFormat; 84 private static Session session; 85 private static String username; 86 private static String password; 87 private MimeMessage message; 88 private String messageText; 89 90 protected Spammer() throws EmailException 91 { 92 String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST); 93 94 if (host != null) { 95 int colon = host.indexOf(':'); 96 97 if (colon > 0) { 98 mailProps.put("mail.smtp.host", host.substring(0, colon)); 99 mailProps.put("mail.smtp.port", String.valueOf(host.substring(colon + 1))); 100 } 101 else { 102 mailProps.put("mail.smtp.host", SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST)); 103 } 104 105 String port = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PORT); 106 107 if (port != null) { 108 mailProps.put("mail.smtp.port", port); 109 } 110 111 String localhost = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_LOCALHOST); 112 113 if (localhost != null && localhost.trim().length() > 0) { 114 mailProps.put("mail.smtp.localhost", localhost); 115 } 116 } 117 118 mailProps.put("mail.mime.address.strict", "false"); 119 mailProps.put("mail.mime.charset", SystemGlobals.getValue(ConfigKeys.MAIL_CHARSET)); 120 mailProps.put("mail.smtp.auth", SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_AUTH)); 121 122 username = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_USERNAME); 123 password = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PASSWORD); 124 125 messageFormat = SystemGlobals.getValue(ConfigKeys.MAIL_MESSSAGE_FORMAT).equals("html") 126 ? MESSAGE_HTML 127 : MESSAGE_TEXT; 128 129 session = Session.getDefaultInstance(mailProps); 130 } 131 132 public static Session getSession() 133 { 134 return session; 135 } 136 137 public final Message getMesssage() 138 { 139 return this.message; 140 } 141 142 public boolean dispatchMessages() throws Exception 143 { 144 if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_AUTH)) { 145 if (username != null && !username.equals("") && password != null && !password.equals("")) { 146 Transport transport = Spammer.getSession().getTransport("smtp"); 147 148 try { 149 String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST); 150 if (host != null) { 151 int colon = host.indexOf(':'); 152 if (colon > 0) { 153 transport.connect(host.substring(0, colon), Integer.parseInt(host.substring(colon + 1)), 154 username, password); 155 } 156 else { 157 transport.connect(host, username, password); 158 } 159 } 160 } 161 catch (MessagingException e) { 162 throw new EmailException("Could not connect to the mail server", e); 163 } 164 165 if (transport.isConnected()) { 166 Address [] addresses = message.getAllRecipients(); 167 for (int i = 0; i < addresses.length; i++) { 168 message.setRecipient(Message.RecipientType.TO, addresses[i]); 170 transport.sendMessage(message, new Address [] { addresses[i] }); 171 } 172 } 173 174 transport.close(); 175 } 176 } 177 else { 178 Address [] addresses = message.getAllRecipients(); 179 for (int i = 0; i < addresses.length; i++) { 180 message.setRecipient(Message.RecipientType.TO, addresses[i]); 181 Transport.send(message, new Address [] { addresses[i] }); 182 } 183 } 184 185 return true; 186 } 187 188 protected final void prepareMessage(List addresses, SimpleHash params, String subject, String messageFile) 189 throws EmailException 190 { 191 this.message = new MimeMessage (session); 192 193 params.put("forumName", SystemGlobals.getValue(ConfigKeys.FORUM_NAME)); 194 195 try { 196 InternetAddress [] recipients = new InternetAddress [addresses.size()]; 197 198 String charset = SystemGlobals.getValue(ConfigKeys.MAIL_CHARSET); 199 200 this.message.setSentDate(new Date ()); 201 this.message.setFrom(new InternetAddress (SystemGlobals.getValue(ConfigKeys.MAIL_SENDER))); 202 this.message.setSubject(subject, charset); 203 204 this.messageText = this.getMessageText(params, messageFile); 205 206 if (messageFormat == MESSAGE_HTML) { 207 this.message.setContent(this.messageText, "text/html; charset=" + charset); 208 } 209 else { 210 this.message.setText(this.messageText, charset); 211 } 212 213 int i = 0; 214 for (Iterator iter = addresses.iterator(); iter.hasNext(); i++) { 215 recipients[i] = new InternetAddress ((String ) iter.next()); 216 } 217 218 this.message.setRecipients(Message.RecipientType.TO, recipients); 219 } 220 catch (Exception e) { 221 logger.warn(e); 222 throw new EmailException(e); 223 } 224 } 225 226 234 protected String getMessageText(SimpleHash params, String messageFile) throws Exception 235 { 236 String templateEncoding = SystemGlobals.getValue(ConfigKeys.MAIL_TEMPLATE_ENCODING); 237 238 StringWriter sWriter = new StringWriter (); 239 240 Template template = null; 241 242 if (templateEncoding == null || "".equals(templateEncoding.trim())) { 243 template = JForumExecutionContext.templateConfig().getTemplate(messageFile); 244 } 245 else { 246 template = JForumExecutionContext.templateConfig().getTemplate(messageFile, templateEncoding); 247 } 248 249 template.process(params, sWriter); 250 251 return sWriter.toString(); 252 } 253 254 259 public String getMessageBody() 260 { 261 return this.messageText; 262 } 263 } 264 | Popular Tags |