1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.utils; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.util.Calendar; import java.util.GregorianCalendar; import net.jot.logger.JOTLogger; /** * This is a mutithreaded emailer program, * it uses raw connection (socket) to an smtp server to send mail. * It is mutlithread so that sending the email in the background.(which can take long if the mail * server is oveloaded) * *@author tcolar *@created May 6, 2003 */ public class JOTEmailSender { static String[] days = {"Sun", "Mon", "Tue", "Wen", "Thu", "Fri", "Sat"}; static String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; String domain = "localhost"; String host = null; int port = 25; String replyTo = "postmaster"; String bounceTo = "postmaster"; String from = "postmaster"; boolean enabled = true; /** *Constructor for the Mailer object * *@param prefs Description of Parameter */ public JOTEmailSender() { } /** *Sends an email using this emailer * *@param to Description of Parameter *@param subject Description of Parameter *@param text Description of Parameter */ public void send(String to, String subject, String text) { if (isEnabled() && host != null) { new MailSender(to, subject, text).start(); JOTLogger.log(JOTLogger.DEBUG_LEVEL, this, "sending email to: " + to); } } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } /** *Description of the Method * *@param i Description of Parameter *@return Description of the Returned Value */ public String twoDigits(int i) { String result = "" + i; if (result.length() == 1) { result = "0" + result; } return result; } /** * Individual MailSender thread, responsible to handle a single "send mail" request. * *@author tcolar *@created May 6, 2003 */ protected class MailSender extends Thread { StringBuffer text; BufferedReader smtpReader = null; BufferedWriter smtpWriter = null; String to = ""; String subject = ""; String msg = null; /** *Constructor for the MailSender object * *@param to Description of Parameter *@param subject Description of Parameter *@param message Description of Parameter */ public MailSender(String to, String subject, String message) { this.to = to; this.subject = subject; this.msg = format(message); } // sending the email /** *Main processing method for the Mailer object */ public void run() { try { sendIt(); } catch (Exception e) { JOTLogger.logException(JOTLogger.ERROR_LEVEL, this, "Failed connecting to mailserver: " + host + ":" + port, e); } } public void sendIt() throws Exception { // constructing the date // in a format that mail server understands. String date = ""; GregorianCalendar cal = new GregorianCalendar(); date += days[cal.get(Calendar.DAY_OF_WEEK) - 1] + ", "; date += twoDigits(cal.get(Calendar.DAY_OF_MONTH)) + " "; date += months[cal.get(Calendar.MONTH)] + " "; date += cal.get(Calendar.YEAR) + " "; date += twoDigits(cal.get(Calendar.HOUR_OF_DAY)) + ":"; date += twoDigits(cal.get(Calendar.MINUTE)) + ":"; date += twoDigits(cal.get(Calendar.SECOND)) + " "; date += "-0800"; Socket s = null; s = new Socket(host, port); // open mail server SMTP port smtpReader = new BufferedReader(new InputStreamReader(s.getInputStream())); smtpWriter = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); smtpReader.readLine(); smtpWriter.write("HELO " + domain); smtpWriter.newLine(); smtpWriter.flush(); smtpReader.readLine(); smtpWriter.write("MAIL FROM: " + bounceTo+"@"+domain); smtpWriter.newLine(); smtpWriter.flush(); smtpReader.readLine(); smtpWriter.write("RCPT TO: " + to); smtpWriter.newLine(); smtpWriter.flush(); smtpReader.readLine(); smtpWriter.write("DATA"); smtpWriter.newLine(); smtpWriter.flush(); smtpReader.readLine(); smtpWriter.write("Date: " + date); smtpWriter.write("\r\n"); smtpWriter.write("From: " + from + " <" + from + "@" + domain + ">"); smtpWriter.write("\r\n"); smtpWriter.write("Reply-To: " + replyTo+"@"+domain); smtpWriter.write("\r\n"); smtpWriter.write("To: " + to); smtpWriter.write("\r\n"); smtpWriter.write("Message-ID: " + new java.util.Date().getTime() + "@" + domain); smtpWriter.write("\r\n"); smtpWriter.write("Subject: " + subject); smtpWriter.write("\r\n\r\n"); smtpWriter.write("" + msg); smtpWriter.write("\r\n"); smtpWriter.write("."); smtpWriter.write("\r\n"); smtpWriter.flush(); String code = smtpReader.readLine(); if (code.charAt(0) != '2') { JOTLogger.log(JOTLogger.ERROR_LEVEL, this, "Error while sending email:" + code); } smtpWriter.write("QUIT"); smtpWriter.newLine(); smtpWriter.flush(); smtpReader.readLine(); s.close(); } /** *Description of the Method * *@param msgString Description of Parameter *@return Description of the Returned Value */ public String format(String msgString) { StringBuffer msg = new StringBuffer(msgString); for (int i = 0; i != msg.length(); i++) { if (msg.charAt(i) == '\n') { if (!(i != 0 && msg.charAt(i - 1) == '\r')) { msg.insert(i, '\r'); } } } return msg.toString(); } } public void setBounceTo(String bounceTo) { this.bounceTo = bounceTo; } public static void setDays(String[] days) { JOTEmailSender.days = days; } public void setDomain(String domain) { this.domain = domain; } public void setFrom(String from) { this.from = from; } public void setHost(String host) { this.host = host; } public static void setMonths(String[] months) { JOTEmailSender.months = months; } public void setPort(int port) { this.port = port; } public void setReplyTo(String replyTo) { this.replyTo = replyTo; } }