package votorola.g.mail; // Copyright 2007-2009, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.sun.mail.smtp.*; import java.util.*; import java.util.logging.*; import votorola.g.logging.*; import javax.mail.*; import votorola.g.lang.*; import votorola.g.script.*; /** A sender of messages, with access to an SMTP mail transfer service. */ public final @ThreadRestricted class MailSender { /** Constructs a MailSender. */ public MailSender( final SMTPTransportX.ConstructionContext cc ) throws MisconfigurationException { authentication = cc.getAuthenticationMethod(); serverName = cc.getServerName(); serverPort = cc.getServerPort(); final String enc = cc.getEncryptionMethod(); if( enc == null ) protocolID = "smtp"; else if( "ssl".equals( enc )) protocolID = "smtps"; else throw new MisconfigurationException( "unrecognized encryption method: " + enc, cc.startupConfigurationFile() ); } // ------------------------------------------------------------------------------------ /** Returns true if this mail sender is to run without actually sending any mail; * while doing everything else, as far as possible. * * @see #setDryRun(boolean) */ public boolean isDryRun() { return dryRun; } private boolean dryRun; /** Sets whether the mail sender is to run without actually sending any mail. * The default value is false; mail will actually be sent. * * @see #isDryRun() */ public final void setDryRun( boolean newDryRun ) { dryRun = newDryRun; } /** Attempts to send a message. Returns any exception that occured, * as a result of the attempt. * * @return any exception that occured; or null, if the attempt succeeded */ public Exception trySend( final Message message, Session session ) { try { message.setSentDate( new Date() ); logger.fine( "sending From=" + MessageX.getFirstFrom(message) + ", To[0]=" + MessageX.getFirstRecipient(message,Message.RecipientType.TO) ); final SMTPTransport transport = (SMTPTransport)session.getTransport( protocolID ); if( dryRun ) return null; try { if( authentication == null ) { transport.connect( serverName, serverPort, null, null ); } else { transport.connect( serverName, serverPort, authentication.name(), authentication.password() ); } transport.sendMessage( message, message.getAllRecipients() ); } finally { final String s = transport.getLastServerResponse(); final StringBuilder b = new StringBuilder( s == null? "(no response)": s ); StringBuilderX.trim( b ); logger.fine( "server responds: " + b.toString() ); transport.close(); } } catch( Exception x ) { return x; } return null; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final SMTPTransportX.SimpleAuthentication authentication; private static final Logger logger = LoggerX.i( MailSender.class ); private final String protocolID; private final String serverName; private final int serverPort; }