diff options
Diffstat (limited to 'NET/worlds/console/MailMessage.java')
| -rw-r--r-- | NET/worlds/console/MailMessage.java | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/NET/worlds/console/MailMessage.java b/NET/worlds/console/MailMessage.java new file mode 100644 index 0000000..fbc5828 --- /dev/null +++ b/NET/worlds/console/MailMessage.java @@ -0,0 +1,297 @@ +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<String> cc; + private String subject; + private Vector<String> 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<String>(); + this.subject = subject; + this.body = new Vector<String>(); + 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<String>(); + 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<String> cc, String subject, Vector<String> 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<String> 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<String> cc) { + if (cc.size() > 0) { + Enumeration<String> 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<String> cc) { + int num = cc.size(); + if (num > 0) { + if (!print(out, "cc: ")) { + return false; + } + + Enumeration<String> 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<String> body = new Vector<String>(); + body.addElement("Looks like it worked...\n"); + sendNote("www.3dcd.com:25", "Gamma Mail Service", "[email protected]", new Vector<String>(), "this is a test", body); + } +} |