1 19 20 package com.lutris.airsent.business.email; 21 22 import java.io.*; 23 import java.net.Socket ; 24 import java.util.Vector ; 25 26 32 public class Email { 33 private OutputStreamWriter out = null; 34 private BufferedReader in = null; 35 private Socket mailserver = null; 36 private InputStreamReader isr = null; 37 private String mailServerURL = "localhost"; 38 39 47 public Email(String mailServer) throws IOException { 48 this.mailServerURL = mailServer; 49 } 50 51 63 private String send(String text) throws IOException { 64 65 out.write(text, 0, text.length()); 66 out.flush(); 67 68 String retVal = in.readLine(); 70 String dump = "x"; 71 while ((dump = in.readLine()) == null) {} 72 if (retVal.startsWith("5")) { 73 74 throw new IOException(mailServerURL + " SMTP Error: " + retVal); 75 } 76 77 return retVal; 78 } 79 80 88 private void open() throws IOException { 89 mailserver = new Socket (mailServerURL, 25); 90 isr = new InputStreamReader(mailserver.getInputStream()); 91 out = new OutputStreamWriter(mailserver.getOutputStream()); 92 in = new BufferedReader(isr); 93 } 94 95 103 private void close() throws IOException { 104 out.close(); 105 in.close(); 106 } 107 108 123 public void sendMail(String domain, String fromAddress, 124 String toAddress, String subject, String type, 125 String text) throws Exception { 126 try { 127 open(); 128 } catch (Exception e) { 129 throw new Exception ("Error opening socket" + e); 130 } 131 132 if (fromAddress == null) { 135 throw new Exception ("We need a from-address that is not null to send email."); 136 } 137 138 fromAddress = fromAddress.trim(); 139 140 if (fromAddress.length() == 0) { 141 throw new Exception ("We need a from-address that is not empty to send email."); 142 } 143 144 if (fromAddress.indexOf("@") == -1) { 145 throw new Exception ("We need a legal from-address to send email, we do not consider \"" 146 + fromAddress + "\" to be legal."); 147 148 } 150 151 if (toAddress == null) { 152 throw new Exception ("We need to-addresses that are not null to send email."); 153 } 154 155 toAddress = toAddress.trim(); 156 157 if (toAddress.length() == 0) { 158 throw new Exception ("We need to-addresses that are not empty to send email."); 159 } 160 161 if (toAddress.indexOf("@") == -1) { 162 throw new Exception ("We need legal to-addresses to send email, we do not consider \"" 163 + toAddress + "\" to be legal."); 164 165 } 167 168 if (subject == null) { 169 throw new Exception ("We need a non-null subject to send email"); 170 } 171 172 subject = subject.trim(); 173 174 if (subject.length() == 0) { 175 throw new Exception ("We need a non-empty subject to send email"); 176 } 177 178 if (type == null) { 179 type = "test/plain"; 180 } 181 182 if (text == null) { 184 throw new Exception ("We need a non-null text body to send email"); 185 } 186 187 text = text.trim(); 188 189 if (text.length() == 0) { 190 throw new Exception ("We need a non-empty text body to send email"); 191 } 192 193 try { 194 send("EHLO " + domain + "\r\n"); 195 } catch (IOException e) { 196 throw new Exception ("SMTP Mail Initialization failure"); 197 } 198 199 try { 200 send("MAIL FROM: " + fromAddress + "\r\n"); 201 send("RCPT TO: " + toAddress + "\r\n"); 202 } catch (IOException e) { 203 throw new Exception ("SMTP Mail addressing failure"); 204 } 205 206 try { 207 208 String boundary = "AIRSENTBOUNDARY-1"; 210 StringBuffer tmp = new StringBuffer (); 211 tmp.append("DATA\r\n"); 212 tmp.append("MIME-Version: 1.0"); 213 tmp.append("\r\n"); 214 tmp.append("From: "); 215 tmp.append(fromAddress); 216 tmp.append("\r\n"); 217 tmp.append("To: "); 218 tmp.append(toAddress); 219 tmp.append("\r\n"); 220 tmp.append("Subject: "); 221 tmp.append(subject); 222 tmp.append("\r\n"); 223 tmp.append("Content-Type: multipart/alternative; boundary=\"" + boundary + "\""); 224 tmp.append("\r\n"); 225 tmp.append("--" + boundary); 226 tmp.append("\r\n"); 227 tmp.append("Content-Type: text/plain; charset=us-ascii;"); 228 tmp.append("\r\n"); 229 tmp.append("\r\n"); 230 tmp.append("AirSent delivery notification:"); 231 tmp.append("\r\n"); 232 tmp.append(text); 233 tmp.append("\r\n"); 234 tmp.append("\r\n"); 235 tmp.append("--" + boundary); 236 tmp.append("\r\n"); 237 tmp.append("Content-Type: text/html; charset=us-ascii;"); 238 tmp.append("\r\n"); 239 tmp.append("\r\n"); 240 tmp.append(text); 241 tmp.append("\r\n"); 242 tmp.append("\r\n"); 243 tmp.append("--" + boundary + "--"); 244 tmp.append("\r\n" + ".\r\n"); 245 247 send(tmp.toString()); 248 249 } catch (Exception e) { 250 throw new Exception ("SMTP Mail message body failure"); 251 } 252 253 try { 254 send("QUIT\r\n"); 255 close(); 256 } catch (IOException e) { 257 throw new Exception ("SMTP Mail signoff failure"); 258 } 259 } 260 261 } 262 263 264 265 266 | Popular Tags |