001: // $Id: AbstractGetCommand.java 335 2006-10-16 06:10:05Z grro $
002:
003: /*
004: * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
021: * The latest copy of this software may be found on http://www.xsocket.org/
022: */
023: package org.xsocket.mail.smtp;
024:
025: import java.net.BindException;
026: import java.net.InetAddress;
027: import java.nio.ByteBuffer;
028: import java.util.logging.Logger;
029:
030: import org.xsocket.stream.BlockingConnection;
031: import org.xsocket.stream.IBlockingConnection;
032:
033: public final class MailSender {
034:
035: private static final Logger LOG = Logger.getLogger(MailSender.class
036: .getName());
037:
038: public void send(InetAddress address, int port) throws Exception {
039: String mail = "Date: Mon, 12 Jun 2006 09:38:31 +0200\r\n"
040: + "From: testi@test.org\r\n" + "To: buddy@test.org\r\n"
041: + "\r\n" + "plain text mail";
042: send(address, port, ByteBuffer.wrap(mail.getBytes()));
043: }
044:
045: public void send(InetAddress address, int port, ByteBuffer mail)
046: throws Exception {
047:
048: IBlockingConnection connection = null;
049: try {
050: connection = new BlockingConnection(address, port);
051: connection.setAutoflush(true);
052: connection.setDefaultEncoding("ASCII");
053:
054: String result = connection.readStringByDelimiter("\r\n",
055: Integer.MAX_VALUE);
056: LOG.fine("received " + result);
057: if (!result.startsWith("220")) {
058: throw new Exception("Wrong greeting " + result);
059: }
060:
061: connection.write("HELO you\r\n");
062: result = connection.readStringByDelimiter("\r\n",
063: Integer.MAX_VALUE);
064: LOG.fine("received " + result);
065: if (!result.startsWith("250")) {
066: throw new Exception("Wrong respnose for Helo " + result);
067: }
068:
069: connection.write("MAIL FROM:testi@example.com\r\n");
070: result = connection.readStringByDelimiter("\r\n",
071: Integer.MAX_VALUE);
072: LOG.fine("received " + result);
073: if (!result.startsWith("250 ")) {
074: throw new Exception("Wrong respnose for MAIL FROM "
075: + result);
076: }
077:
078: connection.write("RCPT TO:you@example\r\n");
079: result = connection.readStringByDelimiter("\r\n",
080: Integer.MAX_VALUE);
081: LOG.fine("received " + result);
082: if (!result.startsWith("250 ")) {
083: throw new Exception("Wrong respnose for Rcpt TO "
084: + result);
085: }
086:
087: connection.write("RCPT TO:admin@example\r\n");
088: result = connection.readStringByDelimiter("\r\n",
089: Integer.MAX_VALUE);
090: LOG.fine("received " + result);
091: if (!result.startsWith("250 ")) {
092: throw new Exception("Wrong respnose for Rcpt TO "
093: + result);
094: }
095:
096: connection.write("DATA\r\n");
097: result = connection.readStringByDelimiter("\r\n",
098: Integer.MAX_VALUE);
099: LOG.fine("received " + result);
100: if (!result.startsWith("354")) {
101: throw new Exception("Wrong respnose for Data " + result);
102: }
103:
104: connection.write(mail);
105: connection.write("\r\n.\r\n");
106: result = connection.readStringByDelimiter("\r\n",
107: Integer.MAX_VALUE);
108: LOG.fine("received " + result);
109: if (!result.startsWith("250 ")) {
110: throw new Exception("Message not accepted: " + result);
111: }
112:
113: connection.write("QUIT\r\n");
114: result = connection.readStringByDelimiter("\r\n",
115: Integer.MAX_VALUE);
116: LOG.fine("received " + result);
117: if (!result.startsWith("221")) {
118: throw new Exception("Wrong respnose for quit " + result);
119: }
120:
121: } catch (BindException be) {
122: System.out.print("b");
123: try {
124: Thread.sleep(500);
125: } catch (InterruptedException igonre) {
126: }
127:
128: } finally {
129: if (connection != null) {
130: try {
131: connection.close();
132: } catch (Exception ignore) {
133: }
134: }
135: }
136: }
137:
138: }
|