1 package org.apache.archiva.redback.integration.mail;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.mail.Address;
30 import javax.mail.Message;
31 import javax.mail.MessagingException;
32 import javax.mail.internet.AddressException;
33 import javax.mail.internet.InternetAddress;
34 import javax.mail.internet.MimeMessage;
36 import org.apache.archiva.redback.configuration.UserConfiguration;
37 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
38 import org.apache.archiva.redback.keys.AuthenticationKey;
39 import org.apache.archiva.redback.policy.UserSecurityPolicy;
40 import org.apache.archiva.redback.policy.UserValidationSettings;
41 import org.apache.archiva.redback.system.SecuritySystem;
42 import org.apache.commons.lang.StringUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.mail.javamail.JavaMailSender;
46 import org.springframework.stereotype.Service;
51 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
54 public class MailerImpl
57 protected Logger log = LoggerFactory.getLogger( getClass() );
60 @Named( value = "mailGenerator#velocity" )
61 private MailGenerator generator;
64 @Named( value = "mailSender" )
65 private JavaMailSender javaMailSender;
68 private SecuritySystem securitySystem;
71 @Named( value = "userConfiguration" )
72 private UserConfiguration config;
74 public void sendAccountValidationEmail( Collection<String> recipients, AuthenticationKey authkey, String baseUrl )
76 String content = generator.generateMail( "newAccountValidationEmail", authkey, baseUrl );
78 UserSecurityPolicy policy = securitySystem.getPolicy();
79 UserValidationSettings validation = policy.getUserValidationSettings();
80 sendMessage( recipients, validation.getEmailSubject(), content );
83 public void sendPasswordResetEmail( Collection<String> recipients, AuthenticationKey authkey, String baseUrl )
85 String content = generator.generateMail( "passwordResetEmail", authkey, baseUrl );
87 UserSecurityPolicy policy = securitySystem.getPolicy();
88 UserValidationSettings validation = policy.getUserValidationSettings();
89 sendMessage( recipients, validation.getEmailSubject(), content );
92 public void sendMessage( Collection<String> recipients, String subject, String content )
94 if ( recipients.isEmpty() )
96 log.warn( "Mail Not Sent - No mail recipients for email. subject [" + subject + "]" );
100 String fromAddress = config.getString( UserConfigurationKeys.EMAIL_FROM_ADDRESS );
101 String fromName = config.getString( UserConfigurationKeys.EMAIL_FROM_NAME );
103 if ( StringUtils.isEmpty( fromAddress ) )
105 fromAddress = System.getProperty( "user.name" ) + "@localhost";
108 // TODO: Allow for configurable message headers.
113 MimeMessage message = javaMailSender.createMimeMessage();
115 message.setSubject( subject );
116 message.setText( content );
118 InternetAddress from = new InternetAddress( fromAddress, fromName );
120 message.setFrom( from );
122 List<Address> tos = new ArrayList<Address>();
124 for ( String mailbox : recipients )
126 InternetAddress to = new InternetAddress( mailbox.trim() );
131 message.setRecipients( Message.RecipientType.TO, tos.toArray( new Address[tos.size()] ) );
133 log.debug( "mail content {}", content );
135 javaMailSender.send( message );
137 catch ( AddressException e )
139 log.error( "Unable to send message, subject [" + subject + "]", e );
141 catch ( MessagingException e )
143 log.error( "Unable to send message, subject [" + subject + "]", e );
145 catch ( UnsupportedEncodingException e )
147 log.error( "Unable to send message, subject [" + subject + "]", e );