Subversion Repositories DevTools

Rev

Go to most recent revision | Details | 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
 
924 dpurdie 96
    // Debug support - send all emails to one person
97
    String gbeBtDebug = System.getenv("GBE_BUILDTOOL_DEBUG");
98
    if ( gbeBtDebug != null && gbeBtDebug.length() > 1  )
99
    {
100
      target = gbeBtDebug;
101
      cc = null;
102
      bcc = null;
103
    }
104
 
105
 
866 mhunt 106
    // Get a Session object
107
    Session session = Session.getInstance( props, null );
108
 
109
    // construct the message
110
    Message msg = new MimeMessage(session);
111
    msg.setFrom( new InternetAddress( source ) );
112
 
113
    msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( target, false ) );
868 mhunt 114
    if ( cc != null && cc.length() > 0 )
866 mhunt 115
    {
116
      msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc, false ) );
117
    }
868 mhunt 118
    if ( bcc != null && bcc.length() > 0 )
866 mhunt 119
    {
120
      msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, false ) );
121
    }
122
 
123
    msg.setSubject( subject );
124
 
868 mhunt 125
    if ( attachment != null && attachment.length() > 0 )
866 mhunt 126
    {
127
      // Attach the specified file.
128
      // We need a multipart message to hold the attachment.
129
      MimeBodyPart mbp1 = new MimeBodyPart();
130
      mbp1.setText( body );
131
      MimeBodyPart mbp2 = new MimeBodyPart();
132
      mbp2.attachFile( attachment );
133
      MimeMultipart mp = new MimeMultipart();
134
      mp.addBodyPart( mbp1 );
135
      mp.addBodyPart( mbp2 );
136
      msg.setContent( mp );
137
    }
138
    else
139
    {
140
      MimeBodyPart mbp1 = new MimeBodyPart();
141
      mbp1.setText( body, "us-ascii", "html" );
142
      MimeMultipart mp = new MimeMultipart();
143
      mp.addBodyPart( mbp1 );
144
      msg.setContent( mp );
145
    }
146
 
147
    msg.setHeader( "X-Mailer", "smtpsend" );
148
    msg.setSentDate( new Date() );
149
 
150
    // send the thing off
151
    /*
152
     * The simple way to send a message is this:
153
     *
154
    Transport.send(msg);
155
     *
156
     * But we're going to use some SMTP-specific features for
157
     * demonstration purposes so we need to manage the Transport
158
     * object explicitly.
159
     */
160
    SMTPTransport t = ( SMTPTransport )session.getTransport( "smtp" );
161
    try
162
    {
163
      t.connect();
164
      t.sendMessage( msg, msg.getAllRecipients() );
165
    }
166
    finally
167
    {
168
      t.close();
169
    }
170
  }
171
}