001: package snow.mail;
002:
003: import snow.utils.storage.FileUtils;
004: import java.net.*;
005: import java.io.*;
006: import java.util.*;
007: import java.text.*;
008:
009: /** A very easy Quick SMTP send message mechanism.
010: ( this class don't resolve dns information, it directly send messages to the given host,
011: do please use it only if you explicitely know the server )
012: */
013: public class EasySendMail {
014: static final boolean debug = true;
015:
016: static final String EOL = "\r\n"; // network end of line
017:
018: protected BufferedReader reply = null;
019: protected PrintStream send = null;
020: protected Socket sock = null;
021:
022: protected List<String> logLines = new Vector<String>();
023:
024: /**
025: * @param hostid The host to connect to (mail.gmx.ch).
026: * @param port is normally 25
027: */
028: public EasySendMail(String hostid, int port) throws Exception {
029: sock = new Socket(hostid, port);
030: sock.setSoTimeout(1000 * 60 * 1); // 1 min timeout
031:
032: reply = new BufferedReader(new InputStreamReader(sock
033: .getInputStream()));
034: send = new PrintStream(sock.getOutputStream());
035:
036: // greetings
037: //
038: String rstr = readLine();
039: if (!rstr.startsWith("220")) {
040: throw new Exception("I want 220 I get: " + rstr);
041: }
042:
043: while (rstr.indexOf('-') == 3) // ### 4?
044: {
045: rstr = readLine();
046: if (!rstr.startsWith("220")) {
047: throw new Exception("I want 220 I get: " + rstr);
048: }
049: }
050: }
051:
052: /** Send the message content to the given server.
053: [Sept2005] may throw a GreyListException with the time to wait until next attempt.
054: */
055: public void sendmsg(String from_address, String to_address,
056: @edu.umd.cs.findbugs.annotations.CheckForNull
057: String fileName, // null if none
058: String content, String domainName) throws Exception,
059: Exception {
060:
061: sendLine("HELO " + domainName, true);
062: String rstr = readLine();
063: if (!rstr.startsWith("250"))
064: throw new Exception("I say HELO " + domainName
065: + ", server says " + rstr);
066:
067: sendLine("MAIL FROM:<" + from_address + ">", true);
068: rstr = readLine();
069: if (!rstr.startsWith("250"))
070: throw new Exception("I say Mail From:<" + from_address
071: + ">" + " server says " + rstr);
072:
073: sendLine("RCPT TO:<" + to_address + ">", true);
074: rstr = readLine();
075: if (rstr.startsWith("450")) {
076: throw new Exception(rstr); // Greylist !!
077: }
078:
079: if (!rstr.startsWith("250"))
080: throw new Exception("I say RCPT TO:<" + to_address + ">"
081: + " server says " + rstr);
082:
083: sendLine("DATA", true);
084: rstr = readLine();
085: if (!rstr.startsWith("354"))
086: throw new Exception("I say DATA server says " + rstr);
087:
088: // send
089: //
090:
091: if (fileName != null) {
092: try {
093: sendFileContent(send, fileName);
094: } catch (Exception e) {
095: send.write("### FILE READ ERROR ###".getBytes());
096: throw e;
097: }
098: }
099:
100: if (content != null) {
101: send.write(content.getBytes());
102: }
103:
104: // all data has been sent - wrap it up
105: sendLine("", true);
106: sendLine(".", true);
107:
108: rstr = readLine();
109: if (!rstr.startsWith("250")) {
110: throw new Exception("I say .(end of message) server says "
111: + rstr);
112: }
113:
114: close();
115: }
116:
117: private void close() {
118: try {
119: sendLine("QUIT", true);
120: sock.close();
121: } catch (Exception ioe) {
122: }
123: }
124:
125: /** all the traffic
126: */
127: public List<String> getTrafficLogLines() {
128: return logLines;
129: }
130:
131: private void sendLine(String line, boolean doLog) throws Exception {
132: if (doLog) {
133: logLines.add("S> " + line);
134: if (debug)
135: System.out.println("EasySendMail S> " + line);
136: }
137: send.print(line);
138: send.print(EOL);
139: send.flush();
140: }
141:
142: private String readLine() throws Exception {
143: String line = reply.readLine();
144: logLines.add("R> " + line);
145: if (debug)
146: System.out.println("EasySendMail R> " + line);
147: return line;
148: }
149:
150: public static void main(String[] a) {
151: testGMX();
152: //testLocalhost();
153: }
154:
155: public static void testGMX() {
156: try {
157: InetAddress local = null;
158: try {
159: local = InetAddress.getLocalHost();
160: } catch (UnknownHostException ioe) {
161: System.err
162: .println("No local IP address found - is your network up?");
163: throw ioe;
164: }
165: System.out.println("LOCAL: " + local.getHostAddress()
166: + ", " + local.getHostName());
167:
168: EasySendMail con = new EasySendMail("mx0.gmx.de", 25);
169: con
170: .sendmsg(
171: "akimo@www.snowraver.org",
172: "akimo@gmx.ch",
173: null,
174: "subject: you should log out\n"
175: + "from: akimo@www.snowraver.org\n\n"
176: + "You work time of 8:32 hours today is reached\r\nlog out now.",
177: //local.getHostAddress()
178: "www.snowraver.org");
179: //con.close();
180: } catch (Exception e) {
181: e.printStackTrace();
182: }
183: }
184:
185: /** get the content of the file and send it.
186: */
187: private void sendFileContent(OutputStream os, String fileName)
188: throws Exception {
189: InputStream fis = null;
190:
191: try {
192: String strCurrentLine;
193: // the file is chiphered. NonLocal received a file and stored it here
194: fis = new FileInputStream(fileName);
195: BufferedReader lineReader = new BufferedReader(
196: new InputStreamReader(fis));
197:
198: // send the file one line at a time
199: String line = lineReader.readLine();
200: while (line != null) {
201: os.write((line + "\r\n").getBytes());
202:
203: line = lineReader.readLine();
204: }
205: } catch (Exception e) {
206: throw e;
207: } finally {
208: FileUtils.closeIgnoringExceptions(fis);
209: }
210: }
211:
212: }
|