View Javadoc

1   /*
2    * Created on Jul 21, 2004 
3    * 
4    *   Firemox is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   * 
21   */
22  package net.sf.firemox.mail;
23  
24  import java.io.File;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import javax.activation.DataHandler;
29  import javax.activation.FileDataSource;
30  import javax.mail.Authenticator;
31  import javax.mail.Message;
32  import javax.mail.Part;
33  import javax.mail.PasswordAuthentication;
34  import javax.mail.Session;
35  import javax.mail.Transport;
36  import javax.mail.internet.InternetAddress;
37  import javax.mail.internet.MimeBodyPart;
38  import javax.mail.internet.MimeMessage;
39  import javax.mail.internet.MimeMultipart;
40  
41  import net.sf.firemox.tools.Configuration;
42  import net.sf.firemox.tools.MToolKit;
43  
44  /***
45   * To send a mail.
46   * 
47   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
48   * @since 0.90
49   */
50  public final class MailUtils {
51  
52  	/***
53  	 * Create a new instance of this class.
54  	 */
55  	private MailUtils() {
56  		super();
57  	}
58  
59  	/***
60  	 * 
61  	 */
62  	public static final String DEFAULT_CONTENT_TYPE = "text/html";
63  
64  	/***
65  	 * @param userName
66  	 * @param from
67  	 * @param to
68  	 * @param message
69  	 * @param subject
70  	 * @param attachments
71  	 * @param headers
72  	 * @param mailerUser
73  	 * @param hostName
74  	 * @return a MIME message
75  	 */
76  	public static MimeMessage sendEmail(String userName, String from,
77  			String[] to, String message, String subject, List<String> attachments,
78  			Header[] headers, String mailerUser, String hostName) {
79  
80  		final Properties props = System.getProperties();
81  		props.put("user.name", userName);
82  		props.put("mail.user", userName);
83  		// props.put("user.passwd", "secret");
84  		// props.put("user.psswd", "secret");
85  		// props.put("user.pwd", "secret");
86  		// props.put("mail.smtp.password", "secret");
87  		// props.put("mail.smtp.auth", "true");
88  		// props.put("mail.smtp.port", "25");
89  		props.put("mail.smtp.user", userName);
90  		props.put("mail.smtp.host", hostName);
91  		props.put("mail.host", hostName);
92  		props.put("mail.smtp.auth", "true");
93  		props.put("mail.transport.protocol", "smtp");
94  		props.putAll(MToolKit.getSmtpProperties());
95  		Session mailSession = null;
96  
97  		if (Configuration.getBoolean("useProxy", false)) {
98  			mailSession = Session.getDefaultInstance(props, new Authenticator() {
99  				@Override
100 				public PasswordAuthentication getPasswordAuthentication() {
101 					return new PasswordAuthentication(props
102 							.getProperty("socks.proxyUserName"), props
103 							.getProperty("socks.proxyPassword"));
104 				}
105 			});
106 		} else {
107 			mailSession = Session.getDefaultInstance(props, null);
108 		}
109 
110 		mailSession.setDebug(false);
111 		try {
112 
113 			// load the key store
114 			MimeMessage msg = new MimeMessage2(mailSession);
115 			msg.setHeader("X-Mailer", "JavaMailer");
116 			MimeMultipart mp = new MimeMultipart();
117 			MimeBodyPart bodyPart = null;
118 			MimeBodyPart bodyPartTxt = new MimeBodyPart();
119 
120 			// add the message text to the mail
121 			if (message != null) {
122 				bodyPartTxt.setContent(message, DEFAULT_CONTENT_TYPE);
123 				mp.addBodyPart(bodyPartTxt);
124 			}
125 
126 			// add the attachment files
127 			if (attachments != null) {
128 				for (String attachment : attachments) {
129 					bodyPart = new MimeBodyPart();
130 					if (!new File(attachment).exists()) {
131 						throw new InternalError("File " + attachment + " does not exist");
132 					}
133 					FileDataSource fds = new FileDataSource(attachment);
134 					DataHandler dh = new DataHandler(fds);
135 					bodyPart.setFileName(attachment
136 							.substring(attachment.lastIndexOf('/') + 1));
137 					bodyPart.setDisposition(Part.ATTACHMENT);
138 					bodyPart.setDescription("File Attachment");
139 					bodyPart.setDataHandler(dh);
140 					mp.addBodyPart(bodyPart);
141 				}
142 			}
143 
144 			// add the Recipients to
145 			InternetAddress[] addresses = new InternetAddress[to.length];
146 			for (int i = 0; i < to.length; i++) {
147 				// to[i]="f-daugan@laptop2.fabdouglas.fr";
148 				addresses[i] = new InternetAddress(to[i].trim());
149 			}
150 			msg.setRecipients(Message.RecipientType.TO, addresses);
151 
152 			// add custom headers
153 			if (headers != null) {
154 				for (Header header : headers) {
155 					msg.addHeader(header.getHeaderName(), header.getHeaderValue());
156 				}
157 			}
158 
159 			msg.setFrom(new InternetAddress(from));
160 
161 			if (attachments == null || bodyPart == null) {
162 				// simple mail
163 				msg.setContent(bodyPartTxt.getContent(), bodyPartTxt.getContentType());
164 			} else {
165 				msg.setContent(mp);
166 			}
167 
168 			msg.setSubject(subject);
169 			msg.setSentDate(new java.util.Date());
170 			msg.saveChanges();
171 
172 			Transport transport = mailSession.getTransport("smtp");
173 			transport.connect(hostName, 25, userName, "");
174 			transport.sendMessage(msg, addresses);
175 			transport.close();
176 			return msg;
177 		} catch (Exception e) {
178 			e.printStackTrace();
179 		}
180 		return null;
181 	}
182 }