1. 계정설정
gmail 2단계 인증
gmail 앱 비밀번호 발급
gmail 설정 -> IMAP 허용
2. build.gradle
implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2' implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail' |
3. application-properties (.yml 기준)
spring:
# se.shin 메일 전송
mail:
host: smtp.gmail.com #SMTP 서버 호스트
port: 587 #SMTP 서버 포트
username: tesgt@gmail.com #SMTP 서버 로그인 아이디 (발신자)
password: test #SMTP 서버 로그인 패스워드 (앱 비밀번호), 보안되어야 함
properties:
mail:
smtp:
auth: true #사용자 인증 시도 여부 (기본값 : false)
starttls:
enable: true #StartTLS 활성화 여부 (기본값 : false)
4. 소스코드
sendService.java
package com.test.service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.validation.Valid;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import com.test.dto.CommonBoolDto;
import com.test.dto.EmailSendPararm;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@RequiredArgsConstructor
public class SendService {
private final JavaMailSender javaMailSender;
public CommonBoolDto sendEmail(@Valid EmailSendPararm emailSendPararm) {
CommonBoolDto commonBoolDto = new CommonBoolDto();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "UTF-8");
mimeMessageHelper.setTo(emailSendPararm.getTo()); // 메일 수신자
mimeMessageHelper.setSubject(emailSendPararm.getSubject()); // 메일 제목
mimeMessageHelper.setText(emailSendPararm.getMessage(), true); // 메일 내용, HTML 여부(true : html허용)
javaMailSender.send(mimeMessage);
log.info("Success!!");
// 리턴 성공 값 담음
commonBoolDto.setResult(true);
return commonBoolDto;
} catch (MessagingException e) {
log.info("fail!!");
throw new RuntimeException(e);
}
}
}
Dto
package com.test.dto;
import org.apache.ibatis.type.Alias;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
@Alias("EmailSendPararm")
public class EmailSendPararm {
private String to;
private String subject;
private String message;
}
개발중 발생한 오류..
Mail server connection failed; nested exception is javax.mail.MessagingException:
Could not convert socket to TLS;\n nested exception is:\n\tjavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;\n nested exception is:\n\tjavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
나같은 경우, Avast 라는 안티 프로그램 때문^^
다른 pc에서 테스트 하니 바로됨.
그래서 제거 후 실행해줬다..
https://okky.kr/articles/870115
OKKY - 스프링 폼 데이터 이메일로 보내기 오류
Failed message 1: javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path buildi
okky.kr
https://www.baeldung.com/spring-email#sending-email
Guide to Spring Email | Baeldung
In this article, we'll walk through the steps needed to send emails from both a plain vanilla Spring application as well as from a Spring Boot application.
www.baeldung.com
[Spring] SMTP 서버를 이용한 이메일 발송
Gmail SMTP 서버를 활용하여 이메일 발송하기
dev-aiden.com