Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
866 mhunt 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
  public static void main(String[] args)
65
  {
66
    try
67
    {
914 dpurdie 68
      send("auperadom10.aupera.erggroup.com", "mhunt@erggroup.com", "mrblobby,mhunt@erggroup.com", "", "", "test", "test", null);
866 mhunt 69
    }
70
    catch( Exception e )
71
    {
72
 
73
    }
74
  }
75
 
76
  public static void send(String mailServer, String source, String target, String cc, String bcc, String subject, String body, String attachment) throws Exception
77
  {
868 mhunt 78
    if ( mailServer == null ||
79
         mailServer.length() == 0 ||
80
         source == null ||
81
         source.length() == 0 ||
82
         target == null ||
83
         target.length() == 0 ||
84
         subject == null ||
85
         subject.length() == 0 ||
86
         body == null ||
87
         body.length() == 0 )
866 mhunt 88
    {
89
      throw new Exception();
90
    }
91
 
92
    Properties props = System.getProperties();
93
    props.put( "mail.smtp.host", mailServer );
94
    props.put( "mail.smtp.sendpartial", "true" );
95
 
4285 dpurdie 96
    //  Unit Test Support
97
    //      Don't send any emails at all
98
    //
99
    if (System.getProperty("vix.utf.name") != null) {
100
        System.err.println("com.erggroup.buildtool.smtp: Send supressed by UTF flags");
101
        return;
102
    }
103
 
924 dpurdie 104
    // Debug support - send all emails to one person
105
    String gbeBtDebug = System.getenv("GBE_BUILDTOOL_DEBUG");
106
    if ( gbeBtDebug != null && gbeBtDebug.length() > 1  )
107
    {
108
      target = gbeBtDebug;
4285 dpurdie 109
      subject = "BUILDTOOL DEBUG: " + subject;
924 dpurdie 110
      cc = null;
111
      bcc = null;
112
    }
113
 
866 mhunt 114
    // Get a Session object
115
    Session session = Session.getInstance( props, null );
116
 
117
    // construct the message
118
    Message msg = new MimeMessage(session);
119
    msg.setFrom( new InternetAddress( source ) );
120
 
121
    msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( target, false ) );
868 mhunt 122
    if ( cc != null && cc.length() > 0 )
866 mhunt 123
    {
124
      msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc, false ) );
125
    }
868 mhunt 126
    if ( bcc != null && bcc.length() > 0 )
866 mhunt 127
    {
128
      msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, false ) );
129
    }
130
 
131
    msg.setSubject( subject );
132
 
868 mhunt 133
    if ( attachment != null && attachment.length() > 0 )
866 mhunt 134
    {
135
      // Attach the specified file.
136
      // We need a multipart message to hold the attachment.
137
      MimeBodyPart mbp1 = new MimeBodyPart();
138
      mbp1.setText( body );
139
      MimeBodyPart mbp2 = new MimeBodyPart();
140
      mbp2.attachFile( attachment );
141
      MimeMultipart mp = new MimeMultipart();
142
      mp.addBodyPart( mbp1 );
143
      mp.addBodyPart( mbp2 );
144
      msg.setContent( mp );
145
    }
146
    else
147
    {
148
      MimeBodyPart mbp1 = new MimeBodyPart();
149
      mbp1.setText( body, "us-ascii", "html" );
150
      MimeMultipart mp = new MimeMultipart();
151
      mp.addBodyPart( mbp1 );
152
      msg.setContent( mp );
153
    }
154
 
155
    msg.setHeader( "X-Mailer", "smtpsend" );
156
    msg.setSentDate( new Date() );
157
 
158
    // send the thing off
159
    /*
160
     * The simple way to send a message is this:
161
     *
162
    Transport.send(msg);
163
     *
164
     * But we're going to use some SMTP-specific features for
165
     * demonstration purposes so we need to manage the Transport
166
     * object explicitly.
167
     */
168
    SMTPTransport t = ( SMTPTransport )session.getTransport( "smtp" );
169
    try
170
    {
171
      t.connect();
172
      t.sendMessage( msg, msg.getAllRecipients() );
173
    }
174
    finally
175
    {
176
      t.close();
177
    }
178
  }
179
}