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