1 17 18 19 20 package org.apache.lenya.net; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.DataInputStream ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.OutputStreamWriter ; 28 import java.io.PrintWriter ; 29 import java.net.ConnectException ; 30 import java.net.Socket ; 31 import java.util.StringTokenizer ; 32 33 import org.apache.log4j.Category; 34 35 36 40 public class SMTP { 41 static Category log = Category.getInstance(SMTP.class); 42 String host = null; 43 int port; 44 String domain = null; 45 Socket socket = null; 46 PrintWriter out = null; 47 DataInputStream in = null; 48 String errlog = null; 49 String from = null; 50 String to = null; 51 String reply_to = null; 52 String cc = null; 53 String [] ccs = null; 54 String bcc = null; 55 String [] bccs = null; 56 String subject = null; 57 String data = null; 58 59 62 public SMTP() { 63 Configuration conf = new Configuration(); 64 host = conf.smtpHost; 65 port = new Integer (conf.smtpPort).intValue(); 66 domain = conf.smtpDomain; 67 log.debug(host + ":" + port + " (" + domain + ")"); 68 } 69 70 71 81 public void send(String from, String to, String cc, String bcc, String subject, String body) { 82 From(from); 83 Reply_To(from); 84 To(to); 85 Cc(cc); 86 Bcc(bcc); 87 Subject(subject); 88 DATA(body); 89 send(); 90 } 91 92 95 public void send() { 96 errlog = ""; 97 98 try { 99 socket = new Socket (host, port); 100 out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket.getOutputStream(), "UTF-8")), true); 101 in = new DataInputStream (socket.getInputStream()); 102 103 errlog = errlog + getResponse(220); 104 105 errlog = errlog + "HELO " + domain + "\n"; 106 out.println("HELO " + domain); 107 errlog = errlog + getResponse(250); 108 109 errlog = errlog + "MAIL FROM:<" + from + ">\n"; 110 out.println("MAIL FROM:<" + from + ">"); 111 errlog = errlog + getResponse(250); 112 113 errlog = errlog + "RCPT TO:<" + to + ">\n"; 114 out.println("RCPT TO:<" + to + ">"); 115 errlog = errlog + getResponse(250); 116 117 for (int i = 0; i < ccs.length; i++) { 118 errlog = errlog + "RCPT TO:<" + ccs[i] + ">\n"; 119 out.println("RCPT TO:<" + ccs[i] + ">"); 120 errlog = errlog + getResponse(250); 121 } 122 123 for (int i = 0; i < bccs.length; i++) { 124 errlog = errlog + "RCPT TO:<" + bccs[i] + ">\n"; 125 out.println("RCPT TO:<" + bccs[i] + ">"); 126 errlog = errlog + getResponse(250); 127 } 128 129 errlog = errlog + "DATA\n"; 130 out.println("DATA"); 131 errlog = errlog + getResponse(354); 132 133 errlog = errlog + "From: " + from + "\n"; 134 out.println("From: " + from); 135 errlog = errlog + "To: " + to + "\n"; 136 out.println("To: " + to); 137 errlog = errlog + "Reply-To: " + reply_to + "\n"; 138 out.println("Reply-To: " + reply_to); 139 140 if (cc != null) { 141 errlog = errlog + "Cc: " + cc + "\n"; 142 out.println("Cc: " + cc); 143 } 144 145 if (bcc != null) { 146 errlog = errlog + "Bcc: " + bcc + "\n"; 147 out.println("Bcc: " + bcc); 148 } 149 150 errlog = errlog + "Subject: " + subject + "\n"; 151 out.println("Subject: " + subject); 152 153 errlog = errlog + "MIME-Version: 1.0\n"; 154 out.println("MIME-Version: 1.0"); 155 156 errlog = errlog + "Content-Type: text/plain; charset=UTF-8; format=flowed\n"; 157 out.println("Content-Type: text/plain; charset=UTF-8; format=flowed"); 158 159 errlog = errlog + "Content-Transfer-Encoding: 8bit\n"; 160 out.println("Content-Transfer-Encoding: 8bit"); 161 162 errlog = errlog + data + "\n.\n"; 163 out.println(data + "\n."); 164 errlog = errlog + getResponse(250); 165 166 errlog = errlog + "QUIT\n"; 167 out.println("QUIT"); 168 errlog = errlog + getResponse(221); 169 log.debug(errlog); 170 } catch (ConnectException e) { 171 log.error(".send(): " + e + " (sendmail is probably not running)"); 172 173 return; 174 } catch (Exception e) { 175 log.error(".send(): " + e); 176 } 177 178 try { 179 in.close(); 180 out.close(); 181 socket.close(); 182 } catch (IOException e) { 183 log.error(this.getClass().getName() + ".send(): " + e); 184 } 185 } 186 187 private String getResponse(int value) throws IOException { 188 try { 189 Thread.sleep(200); 190 } catch (InterruptedException e) { 191 } 192 193 return readLine(in); 194 } 195 196 private String readLine(DataInputStream in) throws IOException { 197 StringBuffer line = new StringBuffer (""); 198 199 while (in.available() > 0) { 200 char character = (char) in.read(); 201 line.append(character); 202 } 203 204 String response = new String (line); 205 206 return response; 207 } 208 209 214 public void DATA(String data) { 215 this.data = data; 216 } 217 218 223 public void From(String from) { 224 this.from = from; 225 } 226 227 232 public void To(String to) { 233 this.to = to; 234 } 235 236 241 public void Reply_To(String reply_to) { 242 this.reply_to = reply_to; 243 } 244 245 250 public void Cc(String cc) { 251 if (cc == null) { 252 ccs = new String [0]; 253 254 return; 255 } 256 257 this.cc = cc; 258 259 StringTokenizer st = new StringTokenizer (cc, ","); 260 ccs = new String [st.countTokens()]; 261 262 for (int i = 0; i < ccs.length; i++) { 263 ccs[i] = st.nextToken(); 264 } 265 } 266 267 272 public void Bcc(String bcc) { 273 if (bcc == null) { 274 bccs = new String [0]; 275 276 return; 277 } 278 279 this.bcc = bcc; 280 281 StringTokenizer st = new StringTokenizer (bcc, ","); 282 bccs = new String [st.countTokens()]; 283 284 for (int i = 0; i < bccs.length; i++) { 285 bccs[i] = st.nextToken(); 286 } 287 } 288 289 294 public void Subject(String subject) { 295 this.subject = subject; 296 } 297 298 305 public int numberOfLines(String filename) { 306 String string = ""; 307 int nlines = 0; 308 309 try { 310 BufferedReader reader = new BufferedReader (new FileReader (filename)); 311 312 while (in.available() != 0) { 313 string = reader.readLine(); 314 nlines++; 315 } 316 317 in.close(); 318 } catch (Exception e) { 319 System.out.println(e); 320 } 321 322 return nlines; 323 } 324 325 332 public String [] loadLines(String filename) { 333 String [] string = new String [numberOfLines(filename)]; 334 int nlines = 0; 335 336 try { 337 BufferedReader reader = new BufferedReader (new FileReader (filename)); 338 339 while (in.available() != 0) { 340 string[nlines] = reader.readLine(); 341 nlines++; 342 } 343 344 in.close(); 345 } catch (Exception e) { 346 System.out.println(e); 347 } 348 349 return string; 350 } 351 } 352 | Popular Tags |