Subversion Repositories DevTools

Rev

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
    {
68
      send("aupera03.aupera.erggroup.com", "mhunt@erggroup.com", "mrblobby,mhunt@erggroup.com", "", "", "test", "test", null);
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
  {
78
    if ( mailServer == null || source == null || target == null || subject == null || body == null )
79
    {
80
      throw new Exception();
81
    }
82
 
83
    Properties props = System.getProperties();
84
    props.put( "mail.smtp.host", mailServer );
85
    props.put( "mail.smtp.sendpartial", "true" );
86
 
87
    // Get a Session object
88
    Session session = Session.getInstance( props, null );
89
 
90
    // construct the message
91
    Message msg = new MimeMessage(session);
92
    msg.setFrom( new InternetAddress( source ) );
93
 
94
    msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( target, false ) );
95
    if ( cc != null )
96
    {
97
      msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse( cc, false ) );
98
    }
99
    if ( bcc != null )
100
    {
101
      msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse( bcc, false ) );
102
    }
103
 
104
    msg.setSubject( subject );
105
 
106
    if ( attachment != null )
107
    {
108
      // Attach the specified file.
109
      // We need a multipart message to hold the attachment.
110
      MimeBodyPart mbp1 = new MimeBodyPart();
111
      mbp1.setText( body );
112
      MimeBodyPart mbp2 = new MimeBodyPart();
113
      mbp2.attachFile( attachment );
114
      MimeMultipart mp = new MimeMultipart();
115
      mp.addBodyPart( mbp1 );
116
      mp.addBodyPart( mbp2 );
117
      msg.setContent( mp );
118
    }
119
    else
120
    {
121
      MimeBodyPart mbp1 = new MimeBodyPart();
122
      mbp1.setText( body, "us-ascii", "html" );
123
      MimeMultipart mp = new MimeMultipart();
124
      mp.addBodyPart( mbp1 );
125
      msg.setContent( mp );
126
    }
127
 
128
    msg.setHeader( "X-Mailer", "smtpsend" );
129
    msg.setSentDate( new Date() );
130
 
131
    // send the thing off
132
    /*
133
     * The simple way to send a message is this:
134
     *
135
    Transport.send(msg);
136
     *
137
     * But we're going to use some SMTP-specific features for
138
     * demonstration purposes so we need to manage the Transport
139
     * object explicitly.
140
     */
141
    SMTPTransport t = ( SMTPTransport )session.getTransport( "smtp" );
142
    try
143
    {
144
      t.connect();
145
      t.sendMessage( msg, msg.getAllRecipients() );
146
    }
147
    finally
148
    {
149
      t.close();
150
    }
151
  }
152
}