개발 창고/Web
[Spring] Email 발송하기
로이제로
2020. 11. 30. 10:12
반응형
최근에 임의로 채번된 비밀번호를 발송하기 위한 메일링 서비스를 제작하면서 한번 작성해볼까 합니다. 저는 일단 Gmail 계정 기준으로 했지만, SMTP설정에 따라 다르게 발송 가능합니다.
1. Gmail 계정 준비하기
- 따로 쓸 말이 없네요
2. Gmail SMTP 보안 설정
- myaccount.google.com/security?gar=1
3. 관련 Libriary 설정
- pom.xml
<!-- Mail -->
<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>
4. Properties 설정
- WEB-INF > config > email.properties
email.account=이메일주소@gmail.com
email.password=비밀번호
- WEB-INF > config > email-config.xml
<?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"
xsi:schemaLocation="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.2.xsd">
<context:property-placeholder location="/WEB-INF/config/email.properties"/>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="${email.account}" />
<property name="password" value="${email.password}" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="ail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
</beans>
5. Handler 작성
import java.io.UnsupportedEncodingException;
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 msg;
private MimeMessageHelper msgHelper;
public MailHandler(JavaMailSender mailSender) throws MessagingException {
this.mailSender = mailSender;
msg = this.mailSender.createMimeMessage();
msgHelper = new MimeMessageHelper(msg, true, "UTF-8");
}
// Email Title
public void setSubject(String subject) throws MessagingException {
msgHelper.setSubject(subject);
}
// Email Content(Text)
public void setText(String htmlContent) throws MessagingException {
msgHelper.setText(htmlContent, true);
}
// Sender Info
public void setFrom(String email, String name) throws UnsupportedEncodingException, MessagingException {
msgHelper.setFrom(email, name);
}
// Recipient Info
public void setTo(String email) throws MessagingException {
msgHelper.setTo(email);
}
public void addInline(String cotentId, DataSource dataSource) throws MessagingException {
msgHelper.addInline(cotentId, dataSource);
}
public void send() {
try {
mailSender.send(msg);
}catch (Exception e) {
e.printStackTrace();
}
}
}
5. 메일링 서비스 사용
@Controller
public class RestfulController extends FrontendController{
@Inject private JavaMailSender mailSender;
/**
* 메일발송
* @return
*/
@RequestMapping(value="sendMail", method=RequestMethod.GET)
public HashMap<String, Object> sendMail(){
HashMap<String, Object> result = new HashMap<String, Object>();
this.sendEmail("받는사람@gmail.com", "메일제목", "메일내용");
return result;
}
private void sendEmail(String emailAddr, String title, String contents) {
try {
//메일 전송
MailHandler sendMail = new MailHandler(mailSender);
sendMail.setSubject(title);
sendMail.setText(contents);
sendMail.setFrom("이메일@gmail.com", "발송자 이름");
sendMail.setTo(emailAddr);
sendMail.send();
}catch(Exception e) {
e.printStackTrace();
}
}
}
반응형