1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.changes.announcement.mailsender;
20
21 import javax.mail.Authenticator;
22 import javax.mail.Message;
23 import javax.mail.MessagingException;
24 import javax.mail.PasswordAuthentication;
25 import javax.mail.Session;
26 import javax.mail.Transport;
27 import javax.mail.internet.InternetAddress;
28 import javax.mail.internet.MimeMessage;
29
30 import java.util.Date;
31 import java.util.Properties;
32
33 import org.codehaus.plexus.mailsender.AbstractMailSender;
34 import org.codehaus.plexus.mailsender.MailMessage;
35 import org.codehaus.plexus.mailsender.MailSenderException;
36 import org.codehaus.plexus.mailsender.util.DateFormatUtils;
37 import org.codehaus.plexus.util.StringUtils;
38
39
40
41
42 public class ProjectJavamailMailSender extends AbstractMailSender {
43 private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
44
45 private Properties props;
46
47
48
49
50
51 public void initialize() {
52 if (StringUtils.isEmpty(getSmtpHost())) {
53 getLogger().error("Error in configuration: Missing smtpHost.");
54 }
55
56 if (getSmtpPort() == 0) {
57 setSmtpPort(DEFAULT_SMTP_PORT);
58 }
59
60 props = new Properties();
61
62 props.put("mail.smtp.host", getSmtpHost());
63
64 props.put("mail.smtp.port", String.valueOf(getSmtpPort()));
65
66 if (getUsername() != null) {
67 props.put("mail.smtp.auth", "true");
68 }
69
70 props.put("mail.debug", String.valueOf(getLogger().isDebugEnabled()));
71
72 if (isSslMode()) {
73 props.put("mail.smtp.socketFactory.port", String.valueOf(getSmtpPort()));
74
75 props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
76
77 props.put("mail.smtp.socketFactory.fallback", "false");
78 }
79
80 if (isTlsEnabled()) {
81 props.put("mail.smtp.starttls.enable", "true");
82 }
83 }
84
85
86
87
88 @Override
89 public void send(MailMessage mail) throws MailSenderException {
90 verify(mail);
91
92 try {
93 Authenticator auth = null;
94
95 if (getUsername() != null) {
96 auth = new Authenticator() {
97 protected PasswordAuthentication getPasswordAuthentication() {
98 return new PasswordAuthentication(getUsername(), getPassword());
99 }
100 };
101 }
102
103 Session session = Session.getDefaultInstance(props, auth);
104
105 session.setDebug(getLogger().isDebugEnabled());
106
107 Message msg = new MimeMessage(session);
108 InternetAddress addressFrom = new InternetAddress(mail.getFrom().getRfc2822Address());
109 msg.setFrom(addressFrom);
110
111 if (mail.getToAddresses().size() > 0) {
112 InternetAddress[] addressTo =
113 new InternetAddress[mail.getToAddresses().size()];
114 int count = 0;
115 for (Object o : mail.getToAddresses()) {
116 String address = ((MailMessage.Address) o).getRfc2822Address();
117 addressTo[count++] = new InternetAddress(address);
118 }
119 msg.setRecipients(Message.RecipientType.TO, addressTo);
120 }
121
122 if (mail.getCcAddresses().size() > 0) {
123 InternetAddress[] addressCc =
124 new InternetAddress[mail.getCcAddresses().size()];
125 int count = 0;
126 for (Object o : mail.getCcAddresses()) {
127 String address = ((MailMessage.Address) o).getRfc2822Address();
128 addressCc[count++] = new InternetAddress(address);
129 }
130 msg.setRecipients(Message.RecipientType.CC, addressCc);
131 }
132
133 if (mail.getBccAddresses().size() > 0) {
134 InternetAddress[] addressBcc =
135 new InternetAddress[mail.getBccAddresses().size()];
136 int count = 0;
137 for (Object o : mail.getBccAddresses()) {
138 String address = ((MailMessage.Address) o).getRfc2822Address();
139 addressBcc[count++] = new InternetAddress(address);
140 }
141 msg.setRecipients(Message.RecipientType.BCC, addressBcc);
142 }
143
144
145 msg.setSubject(mail.getSubject());
146 msg.setContent(mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType());
147
148 if (mail.getSendDate() != null) {
149 msg.setHeader("Date", DateFormatUtils.getDateHeader(mail.getSendDate()));
150 } else {
151 msg.setHeader("Date", DateFormatUtils.getDateHeader(new Date()));
152 }
153
154
155 Transport.send(msg);
156 } catch (MessagingException e) {
157 throw new MailSenderException("Error while sending mail.", e);
158 }
159 }
160 }