email sender

This commit is contained in:
geek 2017-06-28 17:55:11 +09:00
parent c16cb8e81d
commit 820a9b02c8
6 changed files with 171 additions and 11 deletions

37
pom.xml
View File

@ -19,9 +19,12 @@
<properties> <properties>
<grpc.version>1.2.0</grpc.version> <grpc.version>1.2.0</grpc.version>
<protoc.version>3.2.0</protoc.version> <protoc.version>3.2.0</protoc.version>
<spring-test-version>4.3.9.RELEASE</spring-test-version> <spring.version>4.3.9.RELEASE</spring.version>
<hibernate.version>4.3.10.Final</hibernate.version> <spring.data.jpa.version>1.11.4.RELEASE</spring.data.jpa.version>
<spring-data-jpa>1.11.4.RELEASE</spring-data-jpa> <spring.crypto.version>4.2.3.RELEASE</spring.crypto.version>
<hibernate.version>5.2.10.Final</hibernate.version>
<javax.mail.version>1.4.7</javax.mail.version>
<jackson.mapper.version>1.9.13</jackson.mapper.version>
</properties> </properties>
<dependencies> <dependencies>
@ -51,17 +54,25 @@
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId> <artifactId>spring-data-jpa</artifactId>
<version>${spring-data-jpa}</version> <version>${spring.data.jpa.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
<version>${spring-test-version}</version> <version>${spring.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency> <dependency>
<groupId>org.postgresql</groupId> <groupId>org.postgresql</groupId>
@ -72,7 +83,7 @@
<dependency> <dependency>
<groupId>org.codehaus.jackson</groupId> <groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId> <artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version> <version>${jackson.mapper.version}</version>
</dependency> </dependency>
@ -80,20 +91,28 @@
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId> <artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version> <version>${hibernate.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hibernate</groupId> <groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId> <artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version> <version>${hibernate.version}</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto --> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-crypto -->
<dependency> <dependency>
<groupId>org.springframework.security</groupId> <groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId> <artifactId>spring-security-crypto</artifactId>
<version>4.2.3.RELEASE</version> <version>${spring.crypto.version}</version>
</dependency>
<!-- Mail -->
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javax.mail.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -0,0 +1,31 @@
package com.loafle.overflow.module.email.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
/**
* Created by geek on 17. 6. 28.
*/
@Service
public class EmailAuthService {
@Autowired
private JavaMailSender mailSender;
public void sendEmail(String to, String sub, String message) {
try {
SimpleMailMessage message1 = new SimpleMailMessage();
message1.setTo(to);
message1.setSubject(sub);
message1.setText(message);
message1.setFrom("geek@loafle.com");
mailSender.send(message1);
} catch (MailException e) {
e.printStackTrace();
}
}
}

View File

@ -6,18 +6,23 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/** /**
* Created by insanity on 17. 6. 13. * Created by insanity on 17. 6. 13.
*/ */
@Configuration @Configuration
@ComponentScan(basePackages = {"com.loafle.overflow"}, excludeFilters = @ComponentScan.Filter({Configuration.class})) @ComponentScan(basePackages = {"com.loafle.overflow"}, excludeFilters = @ComponentScan.Filter({Configuration.class}))
@PropertySource({"classpath:database.properties"}) @PropertySource({"classpath:database.properties","classpath:mail.properties"})
public class AppConfig { public class AppConfig {
@Bean @Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() { public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("database.properties")); // ppc.setLocation(new ClassPathResource("database.properties"));
ppc.setLocations(new Resource[] {
new ClassPathResource("database.properties"),
new ClassPathResource("mail.properties")
});
ppc.setIgnoreUnresolvablePlaceholders(true); ppc.setIgnoreUnresolvablePlaceholders(true);
return ppc; return ppc;

View File

@ -0,0 +1,63 @@
package com.loafle.overflow.spring;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
/**
* Created by geek on 17. 6. 28.
*/
@Configurable
@ComponentScan(basePackages = "com.loafle.overflow")
public class MailConfiguration {
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private int port;
@Value("${mail.username}")
private String username;
@Value("${mail.password}")
private String password;
@Value("${mail.protocol}")
private String protocol;
@Value("${mail.properties.mail.smtp.auth}")
private boolean smtpAuth;
@Value("${mail.properties.mail.smtp.starttls.enable}")
private boolean ttlEnable;
@Value("${mail.transport.protocol}")
private String transProtocol;
@Value("${mail.smtps.ssl.checkserveridentity}")
private boolean checkserver;
@Value("${mail.smtps.ssl.trust}")
private String sslTrust;
@Bean
public JavaMailSender getMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(this.host);
mailSender.setPort(this.port);
mailSender.setUsername(this.username);
mailSender.setPassword(this.password);
mailSender.setProtocol(this.protocol);
Properties javaMailProperties = new Properties();
javaMailProperties.put("mail.smtp.starttls.enable", this.ttlEnable);
javaMailProperties.put("mail.smtp.auth", this.smtpAuth);
javaMailProperties.put("mail.transport.protocol", this.transProtocol);
javaMailProperties.put("mail.debug", "true");
javaMailProperties.put("mail.smtps.ssl.checkserveridentity", this.checkserver);
javaMailProperties.put("mail.smtps.ssl.trust", this.sslTrust);
mailSender.setJavaMailProperties(javaMailProperties);
return mailSender;
}
}

View File

@ -0,0 +1,14 @@
# Naver SMTP
mail.host=smtp.worksmobile.com
mail.port=465
mail.username=geek@loafle.com
mail.password=@loafle@5795
mail.protocol=smtps
mail.properties.mail.smtp.auth=true
mail.transport.protocol=smtp
mail.properties.mail.smtp.starttls.enable=true
mail.smtps.ssl.checkserveridentity=true
mail.smtps.ssl.trust=*

View File

@ -0,0 +1,28 @@
package com.loafle.overflow.module.email.service;
import com.loafle.overflow.spring.AppConfig;
import com.loafle.overflow.spring.JdbcConfiguration;
import com.loafle.overflow.spring.MailConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* Created by geek on 17. 6. 28.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class,MailConfiguration.class})
public class EmailAuthServiceTest {
@Autowired
EmailAuthService emailAuthService;
@Test
public void TestMailSend() throws Exception {
this.emailAuthService.sendEmail("geek@loafle.com","Spring Email Test","Spring Email Test");
}
}