package NET.worlds.console; import NET.worlds.network.DNSLookup; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Enumeration; import java.util.Vector; public class MailMessage extends Thread { private String server; private String from; private String to; private Vector cc; private String subject; private Vector body; private boolean lock = false; public MailMessage(String server) { this(server, null, null, null, null); } public MailMessage(String server, String from, String to) { this(server, from, to, null, null); } public MailMessage(String server, String from, String to, String subject, String body) { this.server = server; this.from = from; this.to = to; this.cc = new Vector(); this.subject = subject; this.body = new Vector(); if (body != null) { this.body.addElement(body); } } public void setFrom(String f) { if (!this.locked()) { this.from = f; } } public void setTo(String t) { if (!this.locked()) { this.to = t; } } public void addCC(String c) { if (!this.locked()) { this.cc.addElement(c); } } public void setSubject(String s) { if (!this.locked()) { this.subject = s; } } public void setBody(String b) { if (!this.locked()) { this.body = new Vector(); this.body.addElement(b); } } public void appendBody(String b) { if (!this.locked()) { this.body.addElement(b); } } public void appendParagraphs(String text, String sepStr, int lineLen) { if (!this.locked()) { String b = ""; String white = ""; String word = ""; boolean startParagraph = false; int len = 0; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (word.length() > 0 && Character.isWhitespace(c)) { len += white.length() + word.length(); if (len > lineLen) { b = b + sepStr; len = word.length(); } else { b = b + white; } b = b + word; word = ""; white = ""; } if (c != '\n' && c != '\r') { startParagraph = false; if (Character.isWhitespace(c)) { white = white + c; } else { word = word + c; } } else if (!startParagraph) { len = 0; b = b + sepStr; b = b + sepStr; startParagraph = true; } } if (word.length() > 0) { b = b + white; b = b + word; } this.body.addElement(b); } } public void appendParagraphs(String text) { this.appendParagraphs(text, "\n", 70); } private synchronized void lockMessage() { this.lock = true; } private synchronized void unlockMessage() { this.lock = false; } private synchronized boolean locked() { return this.lock; } protected void finished(boolean wasError) { } public void send() { this.start(); } @Override public void run() { this.lockMessage(); this.from = Console.parseUnicode(this.from); this.to = Console.parseUnicode(this.to); this.cc = Console.parseUnicode(this.cc); this.subject = Console.parseUnicode(this.subject); this.body = Console.parseUnicode(this.body); boolean wasError = !sendNote(this.server, this.from, this.to, this.cc, this.subject, this.body); this.unlockMessage(); this.finished(wasError); } public static synchronized boolean sendNote(String server, String from, String to, Vector cc, String subject, Vector body) { int port = 25; int cindex = server.indexOf(58); if (cindex != -1) { port = Integer.parseInt(server.substring(cindex + 1)); server = server.substring(0, cindex); } Socket smtpSocket = null; label123: { try { smtpSocket = new Socket(DNSLookup.lookup(server), port); InputStream smtpIn = smtpSocket.getInputStream(); PrintWriter smtpOut = new PrintWriter(smtpSocket.getOutputStream()); if (getReply(smtpIn) && print(smtpOut, "HELO worlds.net\r\n") && getReply(smtpIn) && print(smtpOut, "MAIL FROM:<" + from + ">\r\n") && getReply(smtpIn) && print(smtpOut, "RCPT TO:<" + to + ">\r\n") && getReply(smtpIn) && printCCcmd(smtpOut, smtpIn, cc) && print(smtpOut, "DATA\r\n") && getReply(smtpIn) && print(smtpOut, "MIME-Version: 1.0\r\n") && print(smtpOut, "MContent-Type: text/plain; charset=\"us-ascii\"\r\n") && print(smtpOut, "From: " + from + "\r\n") && print(smtpOut, "To: " + to + "\r\n") && printCCline(smtpOut, cc) && print(smtpOut, "Subject: " + subject + "\r\n\r\n") && print(smtpOut, body) && print(smtpOut, "\r\n.\r\n")) { getReply(smtpIn); } break label123; } catch (IOException var19) { System.out.println("Error communicating with mail server: " + var19); } finally { try { if (smtpSocket != null) { smtpSocket.close(); } } catch (IOException var18) { } } return false; } System.out.println("MailMessage.sendNote: complete."); return true; } private static boolean print(PrintWriter out, String text) { out.print(text); return !out.checkError(); } private static boolean print(PrintWriter out, Vector text) { int size = text.size(); for (int i = 0; i < size; i++) { out.print(text.elementAt(i)); if (out.checkError()) { return false; } } return true; } private static boolean printCCcmd(PrintWriter out, InputStream in, Vector cc) { if (cc.size() > 0) { Enumeration en = cc.elements(); while (en.hasMoreElements()) { String rcp = en.nextElement(); if (!print(out, "RCPT TO:<" + rcp + ">\r\n")) { return false; } if (!getReply(in)) { return false; } } } return true; } private static boolean printCCline(PrintWriter out, Vector cc) { int num = cc.size(); if (num > 0) { if (!print(out, "cc: ")) { return false; } Enumeration en = cc.elements(); while (en.hasMoreElements()) { String rcp = en.nextElement(); if (num-- > 1) { rcp = rcp + ", "; } if (!print(out, rcp)) { return false; } } if (!print(out, "\r\n")) { return false; } } return true; } private static boolean getReply(InputStream in) { try { int result; while ((result = in.read()) >= 0 && result != 10) { } return true; } catch (IOException var2) { return false; } } public static void main(String[] args) { Vector body = new Vector(); body.addElement("Looks like it worked...\n"); sendNote("www.3dcd.com:25", "Gamma Mail Service", "thumper@alumni.caltech.edu", new Vector(), "this is a test", body); } }