001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.autoconfigure.mail; 018 019import java.util.Map; 020import java.util.Properties; 021 022import javax.activation.MimeType; 023import javax.mail.Session; 024import javax.mail.internet.MimeMessage; 025 026import org.springframework.beans.factory.ObjectProvider; 027import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 028import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; 029import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 030import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 031import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 032import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration.MailSenderCondition; 033import org.springframework.boot.context.properties.EnableConfigurationProperties; 034import org.springframework.context.annotation.Bean; 035import org.springframework.context.annotation.Conditional; 036import org.springframework.context.annotation.Configuration; 037import org.springframework.context.annotation.Import; 038import org.springframework.mail.MailSender; 039import org.springframework.mail.javamail.JavaMailSenderImpl; 040 041/** 042 * {@link EnableAutoConfiguration Auto configuration} for email support. 043 * 044 * @author Oliver Gierke 045 * @author Stephane Nicoll 046 * @author Eddú Meléndez 047 * @since 1.2.0 048 */ 049@Configuration 050@ConditionalOnClass({ MimeMessage.class, MimeType.class }) 051@ConditionalOnMissingBean(MailSender.class) 052@Conditional(MailSenderCondition.class) 053@EnableConfigurationProperties(MailProperties.class) 054@Import(JndiSessionConfiguration.class) 055public class MailSenderAutoConfiguration { 056 057 private final MailProperties properties; 058 059 private final Session session; 060 061 public MailSenderAutoConfiguration(MailProperties properties, 062 ObjectProvider<Session> session) { 063 this.properties = properties; 064 this.session = session.getIfAvailable(); 065 } 066 067 @Bean 068 public JavaMailSenderImpl mailSender() { 069 JavaMailSenderImpl sender = new JavaMailSenderImpl(); 070 if (this.session != null) { 071 sender.setSession(this.session); 072 } 073 else { 074 applyProperties(sender); 075 } 076 return sender; 077 } 078 079 private void applyProperties(JavaMailSenderImpl sender) { 080 sender.setHost(this.properties.getHost()); 081 if (this.properties.getPort() != null) { 082 sender.setPort(this.properties.getPort()); 083 } 084 sender.setUsername(this.properties.getUsername()); 085 sender.setPassword(this.properties.getPassword()); 086 sender.setProtocol(this.properties.getProtocol()); 087 if (this.properties.getDefaultEncoding() != null) { 088 sender.setDefaultEncoding(this.properties.getDefaultEncoding().name()); 089 } 090 if (!this.properties.getProperties().isEmpty()) { 091 sender.setJavaMailProperties(asProperties(this.properties.getProperties())); 092 } 093 } 094 095 private Properties asProperties(Map<String, String> source) { 096 Properties properties = new Properties(); 097 properties.putAll(source); 098 return properties; 099 } 100 101 /** 102 * Condition to trigger the creation of a {@link JavaMailSenderImpl}. This kicks in if 103 * either the host or jndi name property is set. 104 */ 105 static class MailSenderCondition extends AnyNestedCondition { 106 107 MailSenderCondition() { 108 super(ConfigurationPhase.PARSE_CONFIGURATION); 109 } 110 111 @ConditionalOnProperty(prefix = "spring.mail", name = "host") 112 static class HostProperty { 113 114 } 115 116 @ConditionalOnProperty(prefix = "spring.mail", name = "jndi-name") 117 static class JndiNameProperty { 118 119 } 120 121 } 122 123}