스프링부트에서 gmail 전송하기

sangeun jo·2022년 6월 27일
0

사내결재사이트

목록 보기
7/7

구글 계정 > 보안에서 2단계인증을 활성화하고 앱 비밀번호를 발급 받아야한다.
앱은 지메일, 플랫폼은 아무거나 선택하면 된다.

application.properties

### mail service ### 
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username='sender email'
spring.mail.password='app passowrd'
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

GmailService.java

...

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
...


@Service
@RequiredArgsConstructor
public class GmailService {

    private final JavaMailSender javaMailSender;    
    
    public void send(String email, String title, String content) {
        SimpleMailMessage smm = new SimpleMailMessage();  
        smm.setTo(email);  
        smm.setSubject(title);      
        smm.setText(content);      
        javaMailSender.send(smm);  
    }
profile
코더가 아니라 개발자가 되자

0개의 댓글