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