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