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