Browse Source
This commit adds a new starter to auto-configure a MailSender when the necessary classes are present and when the property "spring.mail.host" is set. The auto-configuration also accepts any arbitrary properties that JavaMail might need using the "spring.mail.properties" prefix. Fixes gh-1760pull/1578/head
9 changed files with 351 additions and 0 deletions
@ -0,0 +1,89 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.mail; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
|
||||||
|
/** |
||||||
|
* Configuration properties for email support. |
||||||
|
* |
||||||
|
* @author Oliver Gierke |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
@ConfigurationProperties(prefix = "spring.mail") |
||||||
|
public class MailProperties { |
||||||
|
|
||||||
|
private String host; |
||||||
|
|
||||||
|
private Integer port; |
||||||
|
|
||||||
|
private String username; |
||||||
|
|
||||||
|
private String password; |
||||||
|
|
||||||
|
private String defaultEncoding = "UTF-8"; |
||||||
|
|
||||||
|
private Map<String, String> properties = new HashMap<String, String>(); |
||||||
|
|
||||||
|
public String getHost() { |
||||||
|
return host; |
||||||
|
} |
||||||
|
|
||||||
|
public void setHost(String host) { |
||||||
|
this.host = host; |
||||||
|
} |
||||||
|
|
||||||
|
public Integer getPort() { |
||||||
|
return port; |
||||||
|
} |
||||||
|
|
||||||
|
public void setPort(Integer port) { |
||||||
|
this.port = port; |
||||||
|
} |
||||||
|
|
||||||
|
public String getUsername() { |
||||||
|
return username; |
||||||
|
} |
||||||
|
|
||||||
|
public void setUsername(String username) { |
||||||
|
this.username = username; |
||||||
|
} |
||||||
|
|
||||||
|
public String getPassword() { |
||||||
|
return password; |
||||||
|
} |
||||||
|
|
||||||
|
public void setPassword(String password) { |
||||||
|
this.password = password; |
||||||
|
} |
||||||
|
|
||||||
|
public String getDefaultEncoding() { |
||||||
|
return defaultEncoding; |
||||||
|
} |
||||||
|
|
||||||
|
public void setDefaultEncoding(String defaultEncoding) { |
||||||
|
this.defaultEncoding = defaultEncoding; |
||||||
|
} |
||||||
|
|
||||||
|
public Map<String, String> getProperties() { |
||||||
|
return properties; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,72 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.mail; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
import java.util.Properties; |
||||||
|
import javax.activation.MimeType; |
||||||
|
import javax.mail.internet.MimeMessage; |
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.mail.MailSender; |
||||||
|
import org.springframework.mail.javamail.JavaMailSender; |
||||||
|
import org.springframework.mail.javamail.JavaMailSenderImpl; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link EnableAutoConfiguration Auto configuration} for email support. |
||||||
|
* |
||||||
|
* @author Oliver Gierke |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
@Configuration |
||||||
|
@ConditionalOnClass({MimeMessage.class, MimeType.class}) |
||||||
|
@ConditionalOnProperty(prefix = "spring.mail", value = "host") |
||||||
|
@ConditionalOnMissingBean(MailSender.class) |
||||||
|
@EnableConfigurationProperties(MailProperties.class) |
||||||
|
public class MailSenderAutoConfiguration { |
||||||
|
|
||||||
|
@Autowired MailProperties properties; |
||||||
|
|
||||||
|
@Bean |
||||||
|
public JavaMailSender mailSender() { |
||||||
|
JavaMailSenderImpl sender = new JavaMailSenderImpl(); |
||||||
|
sender.setHost(this.properties.getHost()); |
||||||
|
if (this.properties.getPort() != null) { |
||||||
|
sender.setPort(this.properties.getPort()); |
||||||
|
} |
||||||
|
sender.setUsername(this.properties.getUsername()); |
||||||
|
sender.setPassword(this.properties.getPassword()); |
||||||
|
sender.setDefaultEncoding(this.properties.getDefaultEncoding()); |
||||||
|
Map<String,String> properties = this.properties.getProperties(); |
||||||
|
if (!properties.isEmpty()) { |
||||||
|
Properties javaMailProperties= new Properties(); |
||||||
|
for (Map.Entry<String, String> entry : properties.entrySet()) { |
||||||
|
javaMailProperties.setProperty(entry.getKey(), entry.getValue()); |
||||||
|
} |
||||||
|
sender.setJavaMailProperties(javaMailProperties); |
||||||
|
} |
||||||
|
return sender; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,123 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2012-2014 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.mail; |
||||||
|
|
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.rules.ExpectedException; |
||||||
|
|
||||||
|
import org.springframework.boot.test.EnvironmentTestUtils; |
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.mail.javamail.JavaMailSender; |
||||||
|
import org.springframework.mail.javamail.JavaMailSenderImpl; |
||||||
|
|
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests for {@link MailSenderAutoConfiguration}. |
||||||
|
* |
||||||
|
* @author Stephane Nicoll |
||||||
|
*/ |
||||||
|
public class MailSenderAutoConfigurationTests { |
||||||
|
|
||||||
|
@Rule |
||||||
|
public ExpectedException thrown = ExpectedException.none(); |
||||||
|
|
||||||
|
private AnnotationConfigApplicationContext context; |
||||||
|
|
||||||
|
@After |
||||||
|
public void close() { |
||||||
|
if (this.context != null) { |
||||||
|
this.context.close(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void smtpHostSet() { |
||||||
|
String host = "192.168.1.234"; |
||||||
|
load(EmptyConfig.class, "spring.mail.host:" + host); |
||||||
|
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class); |
||||||
|
assertEquals(host, bean.getHost()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void smptHostWithSettings() { |
||||||
|
String host = "192.168.1.234"; |
||||||
|
load(EmptyConfig.class, "spring.mail.host:" + host, "spring.mail.port:42", |
||||||
|
"spring.mail.username:john", "spring.mail.password:secret", "spring.mail.default-encoding:ISO-9"); |
||||||
|
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class); |
||||||
|
assertEquals(host, bean.getHost()); |
||||||
|
assertEquals(42, bean.getPort()); |
||||||
|
assertEquals("john", bean.getUsername()); |
||||||
|
assertEquals("secret", bean.getPassword()); |
||||||
|
assertEquals("ISO-9", bean.getDefaultEncoding()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void smptHostWithJavaMailProperties() { |
||||||
|
load(EmptyConfig.class, "spring.mail.host:localhost", "spring.mail.properties.mail.smtp.auth:true"); |
||||||
|
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class); |
||||||
|
assertEquals("true", bean.getJavaMailProperties().get("mail.smtp.auth")); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void smtpHostNotSet() { |
||||||
|
load(EmptyConfig.class); |
||||||
|
assertEquals(0, context.getBeansOfType(JavaMailSender.class).size()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void mailSenderBackOff() { |
||||||
|
load(ManualMailConfiguration.class, "spring.mail.host:smtp.acme.org", |
||||||
|
"spring.mail.user:user", "spring.mail.password:secret"); |
||||||
|
JavaMailSenderImpl bean = (JavaMailSenderImpl) context.getBean(JavaMailSender.class); |
||||||
|
assertNull(bean.getUsername()); |
||||||
|
assertNull(bean.getPassword()); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private void load(Class<?> config, String... environment) { |
||||||
|
this.context = doLoad(new Class<?>[] {config}, environment); |
||||||
|
} |
||||||
|
|
||||||
|
private AnnotationConfigApplicationContext doLoad(Class<?>[] configs, |
||||||
|
String... environment) { |
||||||
|
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); |
||||||
|
EnvironmentTestUtils.addEnvironment(applicationContext, environment); |
||||||
|
applicationContext.register(configs); |
||||||
|
applicationContext.register(MailSenderAutoConfiguration.class); |
||||||
|
applicationContext.refresh(); |
||||||
|
return applicationContext; |
||||||
|
} |
||||||
|
|
||||||
|
@Configuration |
||||||
|
static class EmptyConfig { |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Configuration |
||||||
|
static class ManualMailConfiguration { |
||||||
|
|
||||||
|
@Bean |
||||||
|
JavaMailSender customMailSender() { |
||||||
|
return new JavaMailSenderImpl(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,49 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||||
|
<modelVersion>4.0.0</modelVersion> |
||||||
|
<parent> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starters</artifactId> |
||||||
|
<version>1.2.0.BUILD-SNAPSHOT</version> |
||||||
|
</parent> |
||||||
|
<artifactId>spring-boot-starter-mail</artifactId> |
||||||
|
<name>Spring Boot Mail Starter</name> |
||||||
|
<description>Spring Boot Mail Starter</description> |
||||||
|
<url>http://projects.spring.io/spring-boot/</url> |
||||||
|
<organization> |
||||||
|
<name>Pivotal Software, Inc.</name> |
||||||
|
<url>http://www.spring.io</url> |
||||||
|
</organization> |
||||||
|
<properties> |
||||||
|
<main.basedir>${basedir}/../..</main.basedir> |
||||||
|
</properties> |
||||||
|
<dependencies> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework.boot</groupId> |
||||||
|
<artifactId>spring-boot-starter</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework</groupId> |
||||||
|
<artifactId>spring-core</artifactId> |
||||||
|
<exclusions> |
||||||
|
<exclusion> |
||||||
|
<groupId>commons-logging</groupId> |
||||||
|
<artifactId>commons-logging</artifactId> |
||||||
|
</exclusion> |
||||||
|
</exclusions> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework</groupId> |
||||||
|
<artifactId>spring-context</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>org.springframework</groupId> |
||||||
|
<artifactId>spring-context-support</artifactId> |
||||||
|
</dependency> |
||||||
|
<dependency> |
||||||
|
<groupId>javax.mail</groupId> |
||||||
|
<artifactId>mail</artifactId> |
||||||
|
</dependency> |
||||||
|
</dependencies> |
||||||
|
</project> |
||||||
Loading…
Reference in new issue