본문 바로가기
Backend FrameWork/Spring

[SpringFrameWork] Google SMPT 사용법

by IsBerry 2019. 1. 22.
반응형

Google SMPT 사용법

 

 




SMPT란? (Simple Mail Transfer Protocol)

 : E-mail을 컴퓨터에서 다른 컴퓨터로 전송할 때 사용하는 메일 서버의 기본 프로토콜


일반적으로 메일을 송신할 때사용


Google SMPT 사용


SpringFrameWork에서 Google SMPT사용법


 1. POM.XML - SMPT 라이브러리 추가

1
2
3
4
5
6
7
8
9
10
11
<!-- 이메일-->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.7</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${org.springframework-version}</version>
</dependency>
cs


2. root-context.xml  - SMPT , 트랜잭션 설정 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
 
      <!-- mybatis-spring추가 -->
      <!-- Root Context: defines shared resources visible to all other web components -->
    <!-- 메일 보내기 -->
    // ...
    <bean id="dataSource"
       class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
        <!-- 연결문자열에 log4jdbc가 추가됨 -->
         <property name="url" value="DB URL" />
        <property name="username" value="DB계정아이디" />
        <property name="password" value="계정비밀번호" />
    </bean>
    
    <!-- SqlSessionFactory 객체 주입 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:/mappers/**/*Mapper.xml"></property>
    </bean>
    <!-- SqlSession 객체 주입 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>    
    
 
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="구글아이디@gmail.com" />
        <property name="password" value="비밀번호" />
 
    <!-- email 요청시는 SMTP -->
        <property name="javaMailProperties">
            <props>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>
    <!-- 트랜잭션 관련 설정 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 트랜잭션 관련 어노테이션을 자동인식하는 옵션 -->
    <tx:annotation-driven/>
</beans>
 
cs




- MailHandler.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
 
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
 
public class MailHandler 
{
 
    private JavaMailSender mailSender;
    private MimeMessage message;
    private MimeMessageHelper messageHelper;
 
    public MailHandler(JavaMailSender mailSender) throws MessagingException 
    {
        this.mailSender = mailSender;
        message = this.mailSender.createMimeMessage();
        messageHelper = new MimeMessageHelper(message, true"UTF-8");
    }
 
    public void setSubject(String subject) throws MessagingException 
    {
        messageHelper.setSubject(subject);
    }
    public void setText(String htmlContent) throws MessagingException 
    {
        messageHelper.setText(htmlContent, true);
    }
    public void setFrom(String email, String name) throws UnsupportedEncodingException, MessagingException 
    {
        messageHelper.setFrom(email, name);
    }
    public void setTo(String email) throws MessagingException 
    {
        messageHelper.setTo(email);
    }
    public void addInline(String contentId, DataSource dataSource) throws MessagingException 
    {
        messageHelper.addInline(contentId, dataSource);
    }
    public void send() 
    {
        mailSender.send(message);
    }
}
 
cs



TempKey.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Random;
 
public class TempKey 
{
 
    private boolean lowerCheck;
    private int size;
 
    public String getKey(int size, boolean lowerCheck) 
    {
        this.size = size;
        this.lowerCheck = lowerCheck;
        return init();
    }
 
    private String init() 
    {
        Random ran = new Random();
        StringBuffer sb = new StringBuffer();
        int num = 0;
        do 
        {
            num = ran.nextInt(75)+48;
            if((num>=48 && num<=57|| (num>=65 && num<=90|| (num>=97 && num<=122)) 
            {
                sb.append((char)num);
            }
            else 
            {
                continue;
            }
        } 
        while (sb.length() < size);
        if(lowerCheck) 
        {
            return sb.toString().toLowerCase();
        }
        return sb.toString();
    }
 
}
cs



@Controller

반응형