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
006 package fc.util;
007
008 import fc.util.*;
009 import java.net.*;
010 import java.util.*;
011 import javax.mail.*;
012 import javax.mail.internet.*;
013
014 /**
015 Sends a simple SMTP message. Useful for sending system and admin alerts
016 from the command line or within an application.
017 <p>
018 Notes: <br>
019 Make sure that the CLASSPATH is set properly. If invoked from the command
020 line, 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
029 Also, manually do a nslookup and find out the MX for the mail server that
030 you should use. Most any mail servers will not allow relays to email
031 addresses outside of that servers domain, so make sure the recipient is
032 in the same domain as the mail server.
033
034 @author hursh jain
035 **/
036 public class SimpleMail
037 {
038 static final String nl = System.getProperty("line.separator");
039
040 //server socket connection timeout in ms, change and recompile if needed
041 static final String socketConnectiontimeout = "10000";
042
043 //server socket IO timeout in ms, change and recompile if needed
044 static final String socketIOtimeout = "10000";
045
046 public static void main(String[] args)
047 {
048 try {
049 doSend(args);
050 }
051 catch (Exception e) {
052 e.printStackTrace();
053 }
054 }
055
056 static 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
082 public 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 }