I'm leaving this code here but I'm recommending against its use. The Jakarta
Commons Email library (http://jakarta.apache.org/commons/email/) is a better
choice. It's just as easy to use but it also has support for more features
of sending email and it will handle one thing in particular that is difficult
to get right on your own.
If you want to send an email with embedded graphics and include all those
graphics in the email (i.e. they aren't just links to graphics on some remote
server) then the Commons Email library will let you do that easily. Trust me,
it beats having to figure out how to do it yourself in a way that works
across email clients.
// This example of sending mail is a little different from the typical one you
// see in Java. For one thing, when you call the function to send the email
// you send in both a plain text version of the email and an HTML version. The
// recipient's email reader will pick the version to display (usually favoring
// the HTML version if it can display both).
//
// The other thing to note is that there is some commented out code in the
// method for dealing with SMTP servers which require authentication. As best
// I can remember this code worked fine but it's not in the current version.
1
2 // Copyright (c) 2002, John Munsch
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // * Neither the name of the John Munsch nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //
31 // To learn more about open source licenses, please visit:
32 // http://opensource.org/index.php
33
34 package com.johnmunsch.util;
35
36 import java.util.Properties;
37 import javax.mail.*;
38 import javax.mail.internet.*;
39
40 import org.apache.log4j.*;
41
42 /**
43 * Handles sending email to a user. Slightly different from some of the examples
44 * you see in that it will send a multi-format email with both a HTML "pretty"
45 * version of the email and a straight text version.
46 */
47 public class Mail {
48 private static Logger log = Logger.getLogger(Mail.class.getName());
49
50 /**
51 * Send an email from one user to another user with a given subject using a
52 * given SMTP host. You can send both text and HTML versions of the same
53 * email, and it should in fact be the same email content in both cases,
54 * because the end user's email program will be the one to pick the version
55 * to display to the user.
56 *
57 * @param from
58 * @param to
59 * @param subject
60 * @param textBody
61 * @param htmlBody
62 * @param host
63 * @throws AddressException
64 * @throws MessagingException
65 */
66 public static void sendMail(String from, String to, String subject,
67 String textBody, String htmlBody, String host)
68 throws AddressException, MessagingException {
69 // Get system properties.
70 Properties props = System.getProperties();
71
72 // Setup the mail server.
73 props.put("mail.smtp.host", host);
74
75 // Get a session.
76 Session session = Session.getInstance(props, null);
77
78 // The following is required for SMTP servers that require
79 // authentication in order to send an email.
80 // Transport transport = session.getTransport("smtp");
81 // transport.connect(host, username, password);
82 // props.put("mail.smtp.auth", "true");
83
84 // Define the message.
85 MimeMessage message = new MimeMessage(session);
86 message.setFrom(new InternetAddress(from));
87 message.addRecipient(Message.RecipientType.TO,
88 new InternetAddress(to));
89 message.setSubject(subject);
90 message.setText(textBody);
91
92 message.setContent(htmlBody, "text/html");
93
94 // Send message
95 Transport.send(message);
96 }
97 }