스프링

* 이메일 발송을 위해서는 메일 서버가 필요함

- 자체 구축

- gmail 에서 제공하는 smtp server 활용

 

* 메일 발송을 위한 환경 설정

 

보안 수준이 낮은 앱 으로 설정을 낮춘다.

https://www.google.com/settings/security/lesssecureapps

 

MimeMessageHelper 를 이용한 파일 첨부

는 웹 개발자를 위한 스프링 4.0 책을 볼것

작업실행과 스케줄

 

메일 서버 구축 : http://blog.naver.com/scroco/40002842950

http://diyall.tistory.com/689

http://james.apache.org/server/index.html

 

Spring - Java Mail Html Template 사용하기 

[출처] Spring - Java Mail Html Template 사용하기|작성자 전력질주

 

 

 

1. pom.xml 에서 스프링 버전 조정

버전을 조정 한다. 버전이 높으면 낮춘다.

<org.springframework-version>4.3.0.RELEASE</org.springframework-version>

위 버전도 된다.

 

<properties>

<java-version>1.8</java-version>

<org.springframework-version>4.1.6.RELEASE</org.springframework-version>

<org.aspectj-version>1.6.10</org.aspectj-version>

<org.slf4j-version>1.6.6</org.slf4j-version>

</properties>

 

2. pom.xml 에 라이브러리 추가

<!-- 구글 GMail 라이브러리   -->
<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>4.1.6.RELEASE</version>
</dependency>

 

3. root-context.xml 에 메일 발송을 위한 bean 등록

@Controller
@RequestMapping("/mail")
public class GoogleMailController {

	@Inject
	MailService mailService;
	
	
	
	@RequestMapping(value="/mail_form.do" , method=RequestMethod.GET)
	public String mail_form(){
		
		return "/mail/write";
	}
	
	
	@RequestMapping(value="/mail_sender.do", method=RequestMethod.POST)
	public String mail_sender(@ModelAttribute MailDTO mail, Model model){
		
		try {
			mailService.sendMail(mail);
			model.addAttribute("message", "메일을 발송 하였습니다.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:mail_form.do";
	}
	
	
}



    <!-- 구글 이메일 --> 
    <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="지메일 아이디" />
		<property name="password" value="지메일 비밀번호" />
		
		<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>

 

DTO

@Data
public class MailDTO {

	private String senderName;
	private String senderMail;
	private String receiveMail;
	private String subject;
	private String message;
	
}

 

서비스

 

package com.example.wbe04.service.mail;

import javax.inject.Inject;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.example.wbe04.model.mail.dto.MailDTO;

@Service
public class MailService {

	@Inject
	JavaMailSender mailSender;
	
	public void sendMail(MailDTO dto){
	    try {
			MimeMessage msg =mailSender.createMimeMessage();
			//javax.mail.internet.MimeMessage.RecipientType;
			//이메일 수신자
			msg.addRecipient(RecipientType.TO,
					new InternetAddress(dto.getReceiveMail()));
			
			//이메일 발신자
			msg.addFrom(new InternetAddress[]{
					new InternetAddress(dto.getSenderMail(), dto.getSenderName())
			});
			//제목
			msg.setSubject(dto.getSubject(), "utf-8");
			//본문
			msg.setText(dto.getMessage(), "utf-8");
			//메일 발송
			mailSender.send(msg);
			
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}

	
	
}






 

컨트롤

 

@Controller
@RequestMapping("/mail")
public class GoogleMailController {

	@Inject
	MailService mailService;
	
	
	
	@RequestMapping(value="/mail_form.do" , method=RequestMethod.GET)
	public String mail_form(){
		
		return "/mail/write";
	}
	
	
	@RequestMapping(value="/mail_sender.do", method=RequestMethod.POST)
	public String mail_sender(@ModelAttribute MailDTO mail, Model model){
		
		try {
			mailService.sendMail(mail);
			model.addAttribute("message", "메일을 발송 하였습니다.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "redirect:mail_form.do";
	}
	
	
}



 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<!DOCTYPE html>
<html>
	<head>
	<title>Macarnoics</title>
	<meta charset="utf-8">
	<link rel="icon" href="/resources/template/img/favicon.ico" type="image/x-icon">
	<link rel="shortcut icon" href="/resources/template/img/favicon.ico" type="image/x-icon" />
	<meta name="description" content="Your description">
	<meta name="keywords" content="Your keywords">
	<meta name="author" content="Your name">
	
	
	<!-- include libraries(jQuery, bootstrap) 충돌 섬머노트 -->
 <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet"> 
 <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>  
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 

<!-- include summernote css/js-->
<link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.js"></script>


	
	
	
<link rel="stylesheet" href="/resources/template/css/bootstrap.css" type="text/css" media="screen"> 
	<link rel="stylesheet" href="/resources/template/css/responsive.css" type="text/css" media="screen">
	<link rel="stylesheet" href="/resources/template/css/style.css" type="text/css" media="screen">
	
	
	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300' rel='stylesheet' type='text/css'>
	 <!-- <script type="text/javascript" src="/resources/template/js/jquery.js"></script>  -->
	<script type="text/javascript" src="/resources/template/js/superfish.js"></script>
    <script type="text/javascript" src="/resources/template/js/jquery.easing.1.3.js"></script>
	<script type="text/javascript" src="/resources/template/js/jquery.cookie.js"></script>
		
	<link rel="stylesheet" href="/resources/template/css/touchTouch.css" type="text/css" media="screen">
	<link rel="stylesheet" href="/resources/template/css/kwicks-slider.css" type="text/css" media="screen">

	<script type="text/javascript" src="/resources/template/js/jquery.flexslider-min.js"></script>
	<script type="text/javascript" src="/resources/template/js/jquery.kwicks-1.5.1.js"></script>
   
	<script type="text/javascript" src="/resources/template/js/touchTouch.jquery.js"></script>
	
	
	<script type="text/javascript">if($(window).width()>1024){document.write("<"+"script src='/resources/template/js/jquery.preloader.js'></"+"script>");}	</script>
	<script>		
		 jQuery(window).load(function() {	
		 $x = $(window).width();		
	if($x > 1024)
	{			
	jQuery("#content .row").preloader();    }			 
		
		 jQuery('.spinner').animate({'opacity':0},1000,'easeOutCubic',function (){jQuery(this).css('display','none')});	
  		  }); 
					
	</script>

	<!--[if lt IE 8]>
  		<div style='text-align:center'><a href="http://www.microsoft.com/windows/internet-explorer/default.aspx?ocid=ie6_countdown_bannercode"><img src="http://www.theie6countdown.com/img/upgrade.jpg"border="0"alt=""/></a></div>  
 	<![endif]-->
	<!--[if (gt IE 9)|!(IE)]><!-->
	<!--<![endif]-->
	<!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <link rel="stylesheet" href="/resources/template/css/docs.css" type="text/css" media="screen">
    <link rel="stylesheet" href="/resources/template/css/ie.css" type="text/css" media="screen">
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:400' rel='stylesheet' type='text/css'>
  <![endif]-->


	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.js"></script>

<link rel="stylesheet" href="//cdn.jsdelivr.net/highlight.js/8.7/styles/monokai_sublime.min.css">
<script src="//cdn.jsdelivr.net/highlight.js/8.7/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



	</head>




<%@ include file="../include/menu.jsp"  %>




<div class="bg-content">       
  <!--============================== content =================================-->      

<div class="container" style="">

<h3>이메일 쓰기</h3>
<p style="color:red;">${param.message }</p>

<form action="/mail/mail_sender.do" method="post">
<table class="table">

<tr>
 <th>발신자 이름</th>
 <td><input name="senderName" class="form-control"></td>
</tr>

<tr>
 <th>발신자 이메일</th>
 <td><input name="senderMail" class="form-control"></td>
</tr>

<tr>
 <th>수신자 이메일 주소</th>
 <td><input name="receiveMail"  class="form-control"></td>
</tr>


<tr>
 <th>제목</th>
 <td><input name="subject" class="form-control" ></td>
</tr>
 

<tr>
 <th>메시지</th>
 <td><textarea name="message"  class="form-control" id="message"></textarea>
 
 			<!-- include summernote-ko-KR -->
			<script src="/summernote/lang/summernote-ko-KR.js"></script>
								
						<script type="text/javascript">
						$(document).ready(function() {
							//아이디가 content 에 서머노트를 적용 한다.
							  $('#message').summernote({
								  height:600  ,
								  lang: 'ko-KR' // default: 'en-US'
							  });
						});
						
						</script>
 
 </td>
</tr>




<tr>
<td colspan="2">
<input type="submit" value="확인"  class="btn btn-info"/>
</td>
</tr>
</table>






</form>



</div>

</div>




<%@  include file="../include/footer.jsp" %>

 

 

spring

 

about author

PHRASE

Level 60  머나먼나라

인간은 입이 하나 귀가 둘이 있다. 이는 말하기보다 듣기를 두 배 더하라는 뜻이다. -탈무드

댓글 ( 4)

댓글 남기기

작성

스프링 목록    more