1 package com.protomatter.syslog; 2 3 52 53 import java.io.*; 54 import java.net.*; 55 import java.util.*; 56 import java.text.*; 57 import com.protomatter.util.*; 58 59 68 class SMTPMailTransport 69 { 70 private String hostname = null; 71 private int port = 25; 72 private String transportAgent = "protomatter-syslog-agent"; 73 private String CRLF = "\r\n"; 74 private SimpleDateFormat dateFormat = new SimpleDateFormat ("EEE, d MMM yyyy HH:mm:ss"); 75 76 80 public SMTPMailTransport(String hostname) 81 { 82 this(hostname, 25); 83 this.hostname = hostname; 84 } 85 86 90 public SMTPMailTransport(String hostname, int port) 91 { 92 this.hostname = hostname; 93 this.port = port; 94 } 95 96 99 public String getTransportAgentName() 100 { 101 return this.transportAgent; 102 } 103 104 107 public void setTransportAgentName(String transportAgent) 108 { 109 this.transportAgent = transportAgent; 110 } 111 112 115 public void sendMessage(MailMessage message) 116 throws MailException, IOException 117 { 118 try 119 { 120 Socket s = new Socket(hostname, port); 121 122 PrintWriter writer = new PrintWriter(s.getOutputStream()); 123 BufferedReader reader = new BufferedReader( 124 new InputStreamReader(s.getInputStream())); 125 126 String UR = Syslog.getResourceString(MessageConstants.MAILLOG_UNEXPECTED_RESPONSE_MESSAGE); 127 128 String line = reader.readLine(); 129 if (!line.startsWith("2")) 130 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 131 132 writer.print("HELO "); 133 writer.print(transportAgent); 134 writer.print(CRLF); 135 writer.flush(); 136 137 line = reader.readLine(); 138 if (!line.startsWith("2")) 139 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 140 141 writer.print("MAIL FROM: "); 142 writer.print(message.getFromAddress()); 143 writer.print(CRLF); 144 writer.flush(); 145 146 line = reader.readLine(); 147 if (!line.startsWith("2")) 148 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 149 150 151 Enumeration e = message.getTo().elements(); 153 while (e.hasMoreElements()) 154 { 155 writer.print("RCPT TO: "); 156 writer.print((String )e.nextElement()); 157 writer.print(CRLF); 158 writer.flush(); 159 160 line = reader.readLine(); 161 if (!line.startsWith("2")) 162 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 163 } 164 165 e = message.getCC().elements(); 167 while (e.hasMoreElements()) 168 { 169 writer.print("RCPT TO: "); 170 writer.print((String )e.nextElement()); 171 writer.print(CRLF); 172 writer.flush(); 173 174 line = reader.readLine(); 175 if (!line.startsWith("2")) 176 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 177 } 178 179 e = message.getBCC().elements(); 181 while (e.hasMoreElements()) 182 { 183 writer.print("RCPT TO: "); 184 writer.print((String )e.nextElement()); 185 writer.print(CRLF); 186 writer.flush(); 187 188 line = reader.readLine(); 189 if (!line.startsWith("2")) 190 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 191 } 192 193 Object body = message.getBody(); 194 195 writer.print("DATA"); 196 writer.print(CRLF); 197 writer.flush(); 198 line = reader.readLine(); 199 if (!line.startsWith("3")) 200 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 201 202 writer.print("To: "); 203 e = message.getTo().elements(); 204 while (e.hasMoreElements()) 205 { 206 writer.print((String )e.nextElement()); 207 if (e.hasMoreElements()) 208 writer.print(", "); 209 } 210 writer.print(CRLF); 211 212 writer.print("CC: "); 213 e = message.getCC().elements(); 214 if (e.hasMoreElements()) 215 { 216 while (e.hasMoreElements()) 217 { 218 writer.print((String )e.nextElement()); 219 if (e.hasMoreElements()) 220 writer.print(", "); 221 } 222 } 223 writer.print(CRLF); 224 225 String fromName = message.getFromName(); 226 if (fromName == null) 227 fromName = ""; 228 writer.print("From: "); 229 writer.print(fromName); 230 writer.print(" <"); 231 writer.print(message.getFromAddress()); 232 writer.print(">"); 233 writer.print(CRLF); 234 235 writer.print("Subject: "); 236 writer.print(message.getSubject()); 237 writer.print(CRLF); 238 239 writer.print("Date: "); 240 writer.print(dateFormat.format(new Date())); 241 writer.print(CRLF); 242 243 if (body instanceof MIMEMessage) 244 { 245 MIMEMessage mBody = (MIMEMessage)body; 246 writer.print("Content-type: multipart/alternative; boundary=\""); 247 writer.print(mBody.getBoundary()); 248 writer.print("\""); 249 writer.print(CRLF); 250 } 251 252 writer.print(CRLF); 253 writer.print(convertText(message.getBody().toString())); 254 writer.print(CRLF); 255 writer.print(CRLF); 256 writer.print("."); 257 writer.print(CRLF); 258 writer.flush(); 259 260 line = reader.readLine(); 261 if (!line.startsWith("2")) 262 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 263 264 writer.print("QUIT"); 265 writer.print(CRLF); 266 writer.flush(); 267 268 line = reader.readLine(); 269 if (!line.startsWith("2")) 270 throw new MailException(MessageFormat.format(UR, new Object [] { line })); 271 } 272 catch (Exception x) 273 { 274 if (x instanceof IOException) 275 throw (IOException)x; 276 throw new MailException(Syslog.getResourceString(MessageConstants.MAILLOG_TRANSPORT_EXCEPTION_MESSAGE), x); 277 } 278 } 279 280 private String convertText(String text) 281 { 282 285 String endOfLine = System.getProperty("line.separator"); 286 if (CRLF.equals(endOfLine)) 287 { 288 return text; 290 } 291 292 StringBuffer newText = new StringBuffer (text.length() + 512); 293 if (endOfLine.length() == 1) 294 { 295 char eol = endOfLine.charAt(0); 296 char c; 297 for (int i=0; i<text.length(); i++) 298 { 299 c = text.charAt(i); 300 if (c == eol) 301 { 302 newText.append(CRLF); 303 } 304 else 305 { 306 newText.append(c); 307 } 308 } 309 return newText.toString(); 310 } 311 else 312 { 313 316 return text; 320 } 321 } 322 } 323 | Popular Tags |