Subversion Repositories DevTools

Rev

Rev 6914 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * @(#)smtpsend.java    1.5 05/12/09
 *
 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of Sun Microsystems, Inc. or the names of contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 * 
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
 * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
 * SUFFERED BY LICENSEE AS A RESULT OF  OR RELATING TO USE, MODIFICATION
 * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that Software is not designed, licensed or intended
 * for use in the design, construction, operation or maintenance of any
 * nuclear facility.
 */
package com.erggroup.buildtool.smtp;

import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.smtp.*;

/**
 * Demo app that shows how to construct and send an RFC822
 * (singlepart) message.
 *
 * XXX - allow more than one recipient on the command line
 *
 * This is just a variant of msgsend.java that demonstrates use of
 * some SMTP-specific features.
 *
 * @author Max Spivak
 * @author Bill Shannon
 * Modified for buildtool use Martin Hunt
 */

public class Smtpsend
{
    static boolean warningShown = false;
    
  public static void main(String[] args)
  {
    try
    {
      send("smtp-au.vix.local", "dpurdie@vixtechnology.com", "mrblobby,dpurdie@vixtechnology.com", "", "", "test", "test", null);
    }
    catch( Exception e )
    {
        // Keep quiet
    }
  }

  public static void send(String mailServer, String source, String target, String cc, String bcc, String subject, String body, String attachment) throws Exception
  {
    if ( mailServer == null ||
         mailServer.length() == 0 ||
         source == null ||
         source.length() == 0 ||
         target == null ||
         target.length() == 0 ||
         subject == null ||
         subject.length() == 0 ||
         body == null ||
         body.length() == 0 )
    {
      throw new Exception();
    }
            
    Properties props = System.getProperties();
    props.put( "mail.smtp.host", mailServer );
    props.put( "mail.smtp.sendpartial", "true" );

    //  Unit Test Support
    //      Don't send any emails at all
    //
    if (System.getProperty("vix.utf.name") != null) {
        if (!warningShown) {
            System.err.println("com.erggroup.buildtool.smtp: Send supressed by UTF flags");
            warningShown = true;
        }
        return;
    }

    // Debug support - send all emails to one person
    String gbeBtDebug = System.getenv("GBE_BUILDTOOL_DEBUG");
    if ( gbeBtDebug != null && gbeBtDebug.length() > 1  )
    {
      String header = "Original Email List: " + target;
      header += "<br>cc: " + cc;
      header += "<br>bcc: " + bcc;
      header += "<p><hr>";
      body = header + body;
      target = gbeBtDebug;
      subject = "BUILDTOOL DEBUG: " + subject;
      cc = null;
      bcc = null;
    }

    // Get a Session object
    Session session = Session.getInstance( props, null );

    // construct the message
    Message msg = new MimeMessage(session);
    msg.setFrom( new InternetAddress( source ) );

    msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( target, false ) );
    if ( cc != null && cc.length() > 0 )
    {
      msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc, false ) );
    }
    if ( bcc != null && bcc.length() > 0 )
    {
      msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, false ) );
    }

    msg.setSubject( subject );

    if ( attachment != null && attachment.length() > 0 )
    {
      // Attach the specified file.
      // We need a multipart message to hold the attachment.
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText( body );
      MimeBodyPart mbp2 = new MimeBodyPart();
      mbp2.attachFile( attachment );
      MimeMultipart mp = new MimeMultipart();
      mp.addBodyPart( mbp1 );
      mp.addBodyPart( mbp2 );
      msg.setContent( mp );
    }
    else
    {
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText( body, "us-ascii", "html" );
      MimeMultipart mp = new MimeMultipart();
      mp.addBodyPart( mbp1 );
      msg.setContent( mp );
    }

    msg.setHeader( "X-Mailer", "smtpsend" );
    msg.setSentDate( new Date() );

    // send the thing off
    /*
     * The simple way to send a message is this:
     *
     *  Transport.send(msg);
     *
     * But we're going to use some SMTP-specific features for
     * demonstration purposes so we need to manage the Transport
     * object explicitly.
     */
    SMTPTransport t = ( SMTPTransport )session.getTransport( "smtp" );
    try
    {
      t.connect();
      t.sendMessage( msg, msg.getAllRecipients() );
    }
    finally
    {
      t.close();
    }
  }
}