001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 002// The Molly framework is freely distributable under the terms of an 003// MIT-style license. For details, see the molly pages web site at: 004// http://www.mollypages.org/. Use, modify, have fun ! 005 006package fc.util; 007 008import fc.util.*; 009import java.net.*; 010import java.util.*; 011import javax.mail.*; 012import javax.mail.internet.*; 013 014/** 015Sends a simple SMTP message. Useful for sending system and admin alerts 016from the command line or within an application. 017<p> 018Notes: <br> 019Make sure that the CLASSPATH is set properly. If invoked from the command 020line, the CLASSPATH should include the following classes: 021<blockquote> 022 <pre> 023 1. activation.jar (java activation framework API's, needed for javamail) 024 2. mail.jar (javamail API's) 025 3. fc.util.Args (for parsing arguments) 026 </pre> 027</blockquote> 028 029Also, manually do a nslookup and find out the MX for the mail server that 030you should use. Most any mail servers will not allow relays to email 031addresses outside of that servers domain, so make sure the recipient is 032in the same domain as the mail server. 033 034@author hursh jain 035**/ 036public class SimpleMail 037{ 038static final String nl = System.getProperty("line.separator"); 039 040//server socket connection timeout in ms, change and recompile if needed 041static final String socketConnectiontimeout = "10000"; 042 043//server socket IO timeout in ms, change and recompile if needed 044static final String socketIOtimeout = "10000"; 045 046public static void main(String[] args) 047 { 048 try { 049 doSend(args); 050 } 051 catch (Exception e) { 052 e.printStackTrace(); 053 } 054 } 055 056static void doSend(String[] args) throws Exception 057 { 058 Args myargs = new Args(args); 059 myargs.setUsage 060 ( 061 "Usage: java smtpsend <options> " + nl + 062 "where <options> are: " + nl + nl + 063 " -server smtpserver the smtp server to use for the outgoing message"+ nl + 064 " -from email-address the from field in the email message" + nl + 065 " -to email-address the email address(only 1) to send to" + nl + 066 " [-subject subject-string] the subject of the message" + nl + 067 " -message message-string the plain text message to be sent" + nl + 068 " [-debug true] turns on program debugging output" + nl + nl + 069 "Note: arguments (if any) shown inside \"[..]\" are optional, all else are required" 070 ); 071 String server = myargs.getRequired("server"); 072 String from = myargs.getRequired("from"); 073 String to = myargs.getRequired("to"); 074 String subject = myargs.get("subject", ""); 075 String message = myargs.getRequired("message"); 076 boolean dbg = myargs.flagExists("debug"); 077 078 if (dbg) System.out.println("Parsed args: " + "server="+server + ";from="+from + ";to="+to + ";subject="+subject + ";message="+message); 079 send(server, from, to, subject, message, dbg); 080 } 081 082public static void send( 083 String targetMailServer, String from, String to, String subject, 084 String message, boolean dbg) throws Exception 085 { 086 Properties props = System.getProperties(); 087 props.setProperty("mail.smtp.host", targetMailServer); 088 props.setProperty("mail.smtp.connectiontimeout", socketConnectiontimeout); 089 props.setProperty("mail.smtp.timeout", socketIOtimeout); 090 091 Session session = Session.getDefaultInstance(props, null); 092 if (dbg) { 093 session.setDebug(true); 094 } 095 096 Message msg = new MimeMessage(session); 097 msg.setFrom(new InternetAddress(from)); 098 msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); 099 msg.setSubject(subject); 100 msg.setText(message); 101 msg.setSentDate(new Date()); 102 103 Transport.send(msg); 104 } 105 106}