1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
84
85
86
87
88
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
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
121 if (message != null) {
122 bodyPartTxt.setContent(message, DEFAULT_CONTENT_TYPE);
123 mp.addBodyPart(bodyPartTxt);
124 }
125
126
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
145 InternetAddress[] addresses = new InternetAddress[to.length];
146 for (int i = 0; i < to.length; i++) {
147
148 addresses[i] = new InternetAddress(to[i].trim());
149 }
150 msg.setRecipients(Message.RecipientType.TO, addresses);
151
152
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
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 }