Subversion Repositories DevTools

Rev

Rev 7082 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
/*
2
 * @(#)smtpsend.java	1.5 05/12/09
3
 *
4
 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * 
13
 * - Redistribution in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
16
 * 
17
 * Neither the name of Sun Microsystems, Inc. or the names of contributors
18
 * may be used to endorse or promote products derived from this software
19
 * without specific prior written permission.
20
 * 
21
 * This software is provided "AS IS," without a warranty of any kind. ALL
22
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25
 * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26
 * SUFFERED BY LICENSEE AS A RESULT OF  OR RELATING TO USE, MODIFICATION
27
 * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28
 * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30
 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33
 * 
34
 * You acknowledge that Software is not designed, licensed or intended
35
 * for use in the design, construction, operation or maintenance of any
36
 * nuclear facility.
37
 */
38
package com.erggroup.buildtool.smtp;
39
 
40
import java.util.Properties;
41
import java.util.Date;
42
 
43
import javax.mail.*;
44
import javax.mail.internet.*;
45
 
46
import com.sun.mail.smtp.*;
47
 
48
/**
49
 * Demo app that shows how to construct and send an RFC822
50
 * (singlepart) message.
51
 *
52
 * XXX - allow more than one recipient on the command line
53
 *
54
 * This is just a variant of msgsend.java that demonstrates use of
55
 * some SMTP-specific features.
56
 *
57
 * @author Max Spivak
58
 * @author Bill Shannon
59
 * Modified for buildtool use Martin Hunt
60
 */
61
 
62
public class Smtpsend
63
{
64
    static boolean warningShown = false;
65
 
66
  public static void main(String[] args)
67
  {
68
    try
69
    {
70
      send("smtp-au.vix.local", "dpurdie@vixtechnology.com", "mrblobby,dpurdie@vixtechnology.com", "", "", "test", "test", null);
71
    }
72
    catch( Exception e )
73
    {
7082 dpurdie 74
        // Keep quiet
6914 dpurdie 75
    }
76
  }
77
 
78
  public static void send(String mailServer, String source, String target, String cc, String bcc, String subject, String body, String attachment) throws Exception
79
  {
80
    if ( mailServer == null ||
81
         mailServer.length() == 0 ||
82
         source == null ||
83
         source.length() == 0 ||
84
         target == null ||
85
         target.length() == 0 ||
86
         subject == null ||
87
         subject.length() == 0 ||
88
         body == null ||
89
         body.length() == 0 )
90
    {
91
      throw new Exception();
92
    }
93
 
94
    Properties props = System.getProperties();
95
    props.put( "mail.smtp.host", mailServer );
96
    props.put( "mail.smtp.sendpartial", "true" );
97
 
98
    //  Unit Test Support
99
    //      Don't send any emails at all
100
    //
101
    if (System.getProperty("vix.utf.name") != null) {
102
        if (!warningShown) {
103
            System.err.println("com.erggroup.buildtool.smtp: Send supressed by UTF flags");
104
            warningShown = true;
105
        }
106
        return;
107
    }
108
 
109
    // Debug support - send all emails to one person
110
    String gbeBtDebug = System.getenv("GBE_BUILDTOOL_DEBUG");
111
    if ( gbeBtDebug != null && gbeBtDebug.length() > 1  )
112
    {
113
      String header = "Original Email List: " + target;
114
      header += "<br>cc: " + cc;
115
      header += "<br>bcc: " + bcc;
116
      header += "<p><hr>";
117
      body = header + body;
118
      target = gbeBtDebug;
119
      subject = "BUILDTOOL DEBUG: " + subject;
120
      cc = null;
121
      bcc = null;
122
    }
123
 
124
    // Get a Session object
125
    Session session = Session.getInstance( props, null );
126
 
127
    // construct the message
128
    Message msg = new MimeMessage(session);
129
    msg.setFrom( new InternetAddress( source ) );
130
 
131
    msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( target, false ) );
132
    if ( cc != null && cc.length() > 0 )
133
    {
134
      msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc, false ) );
135
    }
136
    if ( bcc != null && bcc.length() > 0 )
137
    {
138
      msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, false ) );
139
    }
140
 
141
    msg.setSubject( subject );
142
 
143
    if ( attachment != null && attachment.length() > 0 )
144
    {
145
      // Attach the specified file.
146
      // We need a multipart message to hold the attachment.
147
      MimeBodyPart mbp1 = new MimeBodyPart();
148
      mbp1.setText( body );
149
      MimeBodyPart mbp2 = new MimeBodyPart();
150
      mbp2.attachFile( attachment );
151
      MimeMultipart mp = new MimeMultipart();
152
      mp.addBodyPart( mbp1 );
153
      mp.addBodyPart( mbp2 );
154
      msg.setContent( mp );
155
    }
156
    else
157
    {
158
      MimeBodyPart mbp1 = new MimeBodyPart();
159
      mbp1.setText( body, "us-ascii", "html" );
160
      MimeMultipart mp = new MimeMultipart();
161
      mp.addBodyPart( mbp1 );
162
      msg.setContent( mp );
163
    }
164
 
165
    msg.setHeader( "X-Mailer", "smtpsend" );
166
    msg.setSentDate( new Date() );
167
 
168
    // send the thing off
169
    /*
170
     * The simple way to send a message is this:
171
     *
7082 dpurdie 172
     *  Transport.send(msg);
6914 dpurdie 173
     *
174
     * But we're going to use some SMTP-specific features for
175
     * demonstration purposes so we need to manage the Transport
176
     * object explicitly.
177
     */
178
    SMTPTransport t = ( SMTPTransport )session.getTransport( "smtp" );
179
    try
180
    {
181
      t.connect();
182
      t.sendMessage( msg, msg.getAllRecipients() );
183
    }
184
    finally
185
    {
186
      t.close();
187
    }
188
  }
189
}