001package bradleyross.demonstrations; 002import java.io.PrintWriter; 003import java.io.OutputStreamWriter; 004import java.net.URL; 005import java.net.URLConnection; 006import java.text.SimpleDateFormat; 007import java.util.Date; 008/** 009 * Demonstration of sending mail using Java. 010 *<p><ul> 011 *<li><p>First parameter - domain name for SMTP (RFC 822) server.</p></li> 012 *<li><p>Second parameter - Email address to use for destination</p></li> 013 *<li><p>Third parameter - Text to go with Email address</p></li> 014 *<li><p>Fourth parameter - Email address of sender</p></li> 015 *<li><p>Fifth parameter - Text to go with e-mail address of sender</p></li> 016 *</ul></p> 017 *<p>Example:<br> 018 *<code>bradleyross.demonstrations.testMail "smtp.foo.com" 019 * "john.smith@foo.com" "John Smith" "bob.jones@foo.com" "Bob Jones"</code></p> 020 *<p>Whether or not you will be able to use the SMTP port in this manner will 021 * depend on how it is configured.</p> 022 * @author Bradley Ross 023 */ 024public class TestMail { 025 public static void main (String args[]) 026 { 027 if (args.length < 5) 028 { 029 System.out.println("Parameters for testMail program are"); 030 System.out.println("1 - Name of SMTP server (e.g. smtp.foo.com)"); 031 System.out.println("2 - EMail address (e.g. john.smith@foo.com)"); 032 System.out.println("3 - Text representation of EMail recipient (John Smith)"); 033 System.out.println("4 - Email of sender"); 034 System.out.println("5 - Text representation of sender"); 035 return; 036 } 037 try 038 { 039 System.getProperties().put("mail.host", args[0]); 040 System.out.println("SMTP host set to " + args[0]); 041 // BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); 042 URL u = new URL("mailto:" + args[1]); 043 URLConnection c = u.openConnection(); 044 System.out.println("Connection opened"); 045 c.setDoInput(false); 046 c.setDoOutput(true); 047 c.setReadTimeout(20000); 048 c.setConnectTimeout(20000); 049 System.out.flush(); 050 c.connect(); 051 System.out.println("Connected"); 052 System.out.flush(); 053 PrintWriter out = 054 new PrintWriter(new OutputStreamWriter(c.getOutputStream())); 055 System.out.println("New PrintWriter created"); 056 System.out.println("To: address is " + args[1]); 057 System.out.println("From: address is " + args[3] + "<" + args[4] + ">"); 058 System.out.println("Reply-To: address is " + args[3] + 059 "<" + args[4] + ">"); 060 System.out.flush(); 061 out.println("From: \"" + args[3] + "\" <" + args[4] + ">"); 062 System.out.println("First line sent"); 063 out.println("Reply-To: \"" + args[3] + "\" <" + args[4] + ">"); 064 // long currentTime = System.currentTimeMillis(); 065 Date currentDate = new Date(System.currentTimeMillis()); 066 SimpleDateFormat df = 067 new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z (zzz)"); 068 out.println("Date: " + df.format(currentDate)); 069 System.out.println("Date: value is " + df.format(currentDate)); 070 out.println("To: " + args[1]); 071 out.println("Subject: Demonstration"); 072 out.println("Content-type: text/html; charset=iso-8859-1"); 073 out.print("\n"); 074 out.println("<html><head></head><body>"); 075 out.println("<title>Test</title>"); 076 out.println("<p><font color=\"red\">"); 077 out.println("This is a demonstration of how easy it is to send mail"); 078 out.println("to an SMTP (RFC 822) server using Java"); 079 out.println("</font></p></body></html>"); 080 out.close(); 081 System.out.println("Closed"); 082 System.out.flush(); 083 } 084 catch (Exception e) 085 { 086 System.out.println("Error encountered in bradleyross.demonstrations.testMail"); 087 System.out.println("Error: " + e.getMessage()); 088 e.printStackTrace(System.out); 089 System.out.flush(); 090 // System.err.println(e); 091 } 092 } 093}