![[Tistory] 이메일 인증 보내기 테스트 샘플링](https://image.inblog.dev?url=https%3A%2F%2Finblog.ai%2Fapi%2Fog%3Ftitle%3D%255BTistory%255D%2520%25EC%259D%25B4%25EB%25A9%2594%25EC%259D%25BC%2520%25EC%259D%25B8%25EC%25A6%259D%2520%25EB%25B3%25B4%25EB%2582%25B4%25EA%25B8%25B0%2520%25ED%2585%258C%25EC%258A%25A4%25ED%258A%25B8%2520%25EC%2583%2598%25ED%2594%258C%25EB%25A7%2581%26logoUrl%3Dhttps%253A%252F%252Finblog.ai%252Finblog_logo.png%26blogTitle%3DCoding_study&w=2048&q=75)

Could not autowire. No beans of 'JavaMailSender' type found
yml에 문제 없는 것 같은데...?

dev에만 메일 설정해서 그렇다나봄. 쓰는데 지장은 없다.
(활성화되면 JavaMailSender 빈이 정상적으로 생성됨)
난 yml 설정 함
1. yml 설정
[application-dev.yml]
server: servlet: session: tracking-modes: cookie encoding: charset: utf-8 force: true port: 8080 spring: mail: host: smtp.gmail.com port: 587 username: compilemate@gmail.com password: your-app-password properties: mail: smtp: starttls: enable: true required: true auth: true
[application.properties]
spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=compilemate@gmail.com spring.mail.password=your-app-password spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.auth=true
2. EmailController
package com.example.emailtest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @RestController public class EmailController { private final EmailUtil emailUtil; @GetMapping("/sendmail") public String sendMail() { emailUtil.sendEmail("compilemate@gmail.com", "[Tistory 인증메일]", "되는지 테스트 하는 거예요"); return "메일 잘 보내졌어"; } }
3. EmailUtil
JavaMailSender를 이용하여 메일을 보낼 수 있다.
MimeMessage는 글자외에 그림과 파일을 보낼 수 있다.
package com.example.emailtest; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @Component public class EmailUtil { private final JavaMailSender sender; // 메일 전송하는 메소드 public void sendEmail(String toAddress, String subject, String body) { MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message); String htmlContent = getCertificationMessage(body); try { // 어디로 메일 보낼거냐 helper.setTo(toAddress); // 제목이 무엇이냐 helper.setSubject(subject); // true로 하면 html 디자인 되어있는 콘텐트를 넣어준다 helper.setText(htmlContent, true); } catch (MessagingException e) { e.printStackTrace(); } // 내가 설정한 이메일로 날아감 sender.send(message); } //html 꾸미기 private String getCertificationMessage(String body) { String certificationMessage = ""; certificationMessage += "<h1 style='text-align: center;'>[Tistory 인증메일]</h1>"; certificationMessage += "<h3 style='text-align: center;'>" + "인증코드 : <strong style='font-size: 32px; letter-spacing: 8px;'>" + body + "</strong></h3>"; return certificationMessage; } }
HTML 꾸며서 받기
private String getCertificationMessage(String certificationNumber) { String certificationMessage = ""; certificationMessage += "<h1 style='text-align: center;'>[Tistory 인증메일]</h1>"; certificationMessage += "<h3 style='text-align: center;'>" + "인증코드 : <strong style='font-size: 32px; letter-spacing: 8px;'>" + certificationNumber + "</strong></h3>"; return certificationMessage; }
true라고 넣으면 html을 적용 할 수 있다.

4. http://localhost:8080/sendmail 주소로 요청

5. 결과


html도 적용 됨!!
Share article