1 16 package org.apache.commons.net.smtp; 17 18 import java.io.BufferedReader ; 19 import java.io.BufferedWriter ; 20 import java.io.IOException ; 21 import java.io.InputStreamReader ; 22 import java.io.OutputStreamWriter ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 import org.apache.commons.net.MalformedServerReplyException; 26 import org.apache.commons.net.ProtocolCommandListener; 27 import org.apache.commons.net.ProtocolCommandSupport; 28 import org.apache.commons.net.SocketClient; 29 30 83 84 public class SMTP extends SocketClient 85 { 86 87 public static final int DEFAULT_PORT = 25; 88 89 private static final String __DEFAULT_ENCODING = "ISO-8859-1"; 93 94 private StringBuffer __commandBuffer; 95 96 BufferedReader _reader; 97 BufferedWriter _writer; 98 int _replyCode; 99 Vector _replyLines; 100 boolean _newReplyString; 101 String _replyString; 102 103 107 protected ProtocolCommandSupport _commandSupport_; 108 109 114 public SMTP() 115 { 116 setDefaultPort(DEFAULT_PORT); 117 __commandBuffer = new StringBuffer (); 118 _replyLines = new Vector (); 119 _newReplyString = false; 120 _replyString = null; 121 _commandSupport_ = new ProtocolCommandSupport(this); 122 } 123 124 private int __sendCommand(String command, String args, boolean includeSpace) 125 throws IOException 126 { 127 String message; 128 129 __commandBuffer.setLength(0); 130 __commandBuffer.append(command); 131 132 if (args != null) 133 { 134 if (includeSpace) 135 __commandBuffer.append(' '); 136 __commandBuffer.append(args); 137 } 138 139 __commandBuffer.append(SocketClient.NETASCII_EOL); 140 141 _writer.write(message = __commandBuffer.toString()); 142 _writer.flush(); 143 144 if (_commandSupport_.getListenerCount() > 0) 145 _commandSupport_.fireCommandSent(command, message); 146 147 __getReply(); 148 return _replyCode; 149 } 150 151 private int __sendCommand(int command, String args, boolean includeSpace) 152 throws IOException 153 { 154 return __sendCommand(SMTPCommand._commands[command], args, includeSpace); 155 } 156 157 private void __getReply() throws IOException 158 { 159 int length; 160 161 _newReplyString = true; 162 _replyLines.setSize(0); 163 164 String line = _reader.readLine(); 165 166 if (line == null) 167 throw new SMTPConnectionClosedException( 168 "Connection closed without indication."); 169 170 length = line.length(); 173 if (length < 3) 174 throw new MalformedServerReplyException( 175 "Truncated server reply: " + line); 176 177 try 178 { 179 String code = line.substring(0, 3); 180 _replyCode = Integer.parseInt(code); 181 } 182 catch (NumberFormatException e) 183 { 184 throw new MalformedServerReplyException( 185 "Could not parse response code.\nServer Reply: " + line); 186 } 187 188 _replyLines.addElement(line); 189 190 if (length > 3 && line.charAt(3) == '-') 192 { 193 do 194 { 195 line = _reader.readLine(); 196 197 if (line == null) 198 throw new SMTPConnectionClosedException( 199 "Connection closed without indication."); 200 201 _replyLines.addElement(line); 202 203 } 207 while (!(line.length() >= 4 && line.charAt(3) != '-' && 208 Character.isDigit(line.charAt(0)))); 209 } 213 214 if (_commandSupport_.getListenerCount() > 0) 215 _commandSupport_.fireReplyReceived(_replyCode, getReplyString()); 216 217 if (_replyCode == SMTPReply.SERVICE_NOT_AVAILABLE) 218 throw new SMTPConnectionClosedException( 219 "SMTP response 421 received. Server closed connection."); 220 } 221 222 223 protected void _connectAction_() throws IOException 224 { 225 super._connectAction_(); 226 _reader = 227 new BufferedReader (new InputStreamReader (_input_, 228 __DEFAULT_ENCODING)); 229 _writer = 230 new BufferedWriter (new OutputStreamWriter (_output_, 231 __DEFAULT_ENCODING)); 232 __getReply(); 233 } 234 235 236 242 public void addProtocolCommandListener(ProtocolCommandListener listener) 243 { 244 _commandSupport_.addProtocolCommandListener(listener); 245 } 246 247 253 public void removeProtocolCommandistener(ProtocolCommandListener listener) 254 { 255 _commandSupport_.removeProtocolCommandListener(listener); 256 } 257 258 259 267 public void disconnect() throws IOException 268 { 269 super.disconnect(); 270 _reader = null; 271 _writer = null; 272 _replyString = null; 273 _replyLines.setSize(0); 274 _newReplyString = false; 275 } 276 277 278 298 public int sendCommand(String command, String args) throws IOException 299 { 300 return __sendCommand(command, args, true); 301 } 302 303 304 325 public int sendCommand(int command, String args) throws IOException 326 { 327 return sendCommand(SMTPCommand._commands[command], args); 328 } 329 330 331 349 public int sendCommand(String command) throws IOException 350 { 351 return sendCommand(command, null); 352 } 353 354 355 374 public int sendCommand(int command) throws IOException 375 { 376 return sendCommand(command, null); 377 } 378 379 380 388 public int getReplyCode() 389 { 390 return _replyCode; 391 } 392 393 410 public int getReply() throws IOException 411 { 412 __getReply(); 413 return _replyCode; 414 } 415 416 417 424 public String [] getReplyStrings() 425 { 426 String [] lines; 427 lines = new String [_replyLines.size()]; 428 _replyLines.copyInto(lines); 429 return lines; 430 } 431 432 439 public String getReplyString() 440 { 441 Enumeration en; 442 StringBuffer buffer; 443 444 if (!_newReplyString) 445 return _replyString; 446 447 buffer = new StringBuffer (256); 448 en = _replyLines.elements(); 449 while (en.hasMoreElements()) 450 { 451 buffer.append((String )en.nextElement()); 452 buffer.append(SocketClient.NETASCII_EOL); 453 } 454 455 _newReplyString = false; 456 457 return (_replyString = buffer.toString()); 458 } 459 460 461 475 public int helo(String hostname) throws IOException 476 { 477 return sendCommand(SMTPCommand.HELO, hostname); 478 } 479 480 481 495 public int mail(String reversePath) throws IOException 496 { 497 return __sendCommand(SMTPCommand.MAIL, reversePath, false); 498 } 499 500 501 515 public int rcpt(String forwardPath) throws IOException 516 { 517 return __sendCommand(SMTPCommand.RCPT, forwardPath, false); 518 } 519 520 521 534 public int data() throws IOException 535 { 536 return sendCommand(SMTPCommand.DATA); 537 } 538 539 540 554 public int send(String reversePath) throws IOException 555 { 556 return sendCommand(SMTPCommand.SEND, reversePath); 557 } 558 559 560 574 public int soml(String reversePath) throws IOException 575 { 576 return sendCommand(SMTPCommand.SOML, reversePath); 577 } 578 579 580 594 public int saml(String reversePath) throws IOException 595 { 596 return sendCommand(SMTPCommand.SAML, reversePath); 597 } 598 599 600 613 public int rset() throws IOException 614 { 615 return sendCommand(SMTPCommand.RSET); 616 } 617 618 619 633 public int vrfy(String user) throws IOException 634 { 635 return sendCommand(SMTPCommand.VRFY, user); 636 } 637 638 639 653 public int expn(String name) throws IOException 654 { 655 return sendCommand(SMTPCommand.EXPN, name); 656 } 657 658 671 public int help() throws IOException 672 { 673 return sendCommand(SMTPCommand.HELP); 674 } 675 676 690 public int help(String command) throws IOException 691 { 692 return sendCommand(SMTPCommand.HELP, command); 693 } 694 695 708 public int noop() throws IOException 709 { 710 return sendCommand(SMTPCommand.NOOP); 711 } 712 713 714 727 public int turn() throws IOException 728 { 729 return sendCommand(SMTPCommand.TURN); 730 } 731 732 733 746 public int quit() throws IOException 747 { 748 return sendCommand(SMTPCommand.QUIT); 749 } 750 751 } 752 753 760 | Popular Tags |