import java.net.InetAddress; import java.net.Socket; import java.io.IOException; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.*; import java.net.*; public class Mailer { public static void main(String[] args) { // TODO code application logic here String firstName = "Johua"; InetAddress remote = null; String mail_recipient = args[1]; String subject = args[2]; InetAddress myself = null; Socket conn = null; PrintWriter out = null; BufferedReader in = null; String mailinput; try{ remote = InetAddress.getByName(args[0]); } catch (java.net.UnknownHostException e) { e.printStackTrace(); }; try{ myself = InetAddress.getLocalHost (); } catch (java.net.UnknownHostException e) { e.printStackTrace(); }; System.out.println("Local hostname : " + myself.getHostName () + "\n"); System.out.println("server: " + remote); System.out.println("rcpt: " + mail_recipient); System.out.println("subject: " + subject); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); try { conn = new Socket(args[0], 1025); out = new PrintWriter(conn.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); } catch (java.io.IOException e) { e.printStackTrace(); }; out.println("EHLO " + myself.getHostName () + "\n"); // out.println("EHLO relay.att.com"); out.println("MAIL FROM: "); out.println("RCPT TO: <" + mail_recipient + ">"); out.println("DATA"); //header // out.println("Received: from relay.att.com (relay.att.com [12.109.68.210]) by " + myself.getHostName () + " (8.13.0/8.13.0) with SMTP id jdfglXhs038720 for <" + mail_recipient + ">; Mon, 18 Apr 2005 11:46:19 -0600"); // out.println("Received: from mail.microsoft.com (mail.microsoft.com [131.107.0.15]) by relay.att.com (8.13.0/8.13.0) with SMTP id j3IHlXhffs2720 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for <" + mail_recipient + ">; Mon, 18 Apr 2005 11:46:19 -0600"); out.println("Received: from mail.microsoft.com ([131.107.0.15]) by relay.att.com (8.13.1/8.13.1) with ESMTP id j3IHsWh0001059 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for <" + mail_recipient + ">; Mon, 18 Apr 2005 10:54:32 -0700"); out.println("From: Steve Ballmer "); out.println("Date: Mon, 11 May 2005 05:19:24 +0200"); out.println("Subject: " + subject + ""); out.println("To: " + mail_recipient + ""); out.println(""); // body try { do { mailinput=stdIn.readLine(); if (mailinput != null) { if (mailinput.startsWith(".")) { out.print("."); } out.println(mailinput); } } while (mailinput != null); }catch (java.io.IOException e){ e.printStackTrace();} // politely disconnect out.println("."); out.println("QUIT"); // mailinput recycle, read status try { do { mailinput=in.readLine(); System.out.println(mailinput); // if (mailinput.indexOf("Message accepted for delivery") != -1) { if (mailinput.startsWith("250")) { // message delivered, ... System.exit(0); } if (mailinput.startsWith("5")) { // permanent error System.exit(1); } } while (mailinput != null); }catch (java.io.IOException e){ e.printStackTrace();} // no successful exit yet, so we complain System.out.println("no success, bailing out"); System.exit(1); } }