[Spring Boot] Naver Mail

Spring Boot

Language :

build.gradle.kts

implementation("org.springframework.boot:spring-boot-starter-mail:3.1.1")

Mail SMTP 허용

1. 네이버 로그인 > 메일 > 환경설정 > POP3/IMAP 설정

Untitled.png

POP3/IMAP Setting

2. 설정값 확인

Untitled (1).png

mail setting properties

application.yml

naver:
    mail:
        username: "user@naver.com"
        password: "password"
        from-name: "userName"

MailConfig

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.mail.javamail.JavaMailSenderImpl
import java.util.*

@Configuration
class MailConfig {

    @Value("\${naver.mail.username}")
    private val username: String = "naver.mail.username"

    @Value("\${naver.mail.password}")
    private val password: String = "naver.mail.password"

    @Bean
    fun javaMailService(): JavaMailSender {
        val sender = JavaMailSenderImpl()
        sender.host = "smtp.naver.com"
        sender.username = username
        sender.password = password
        sender.port = 465
        sender.javaMailProperties = getMailProperties()
        return sender
    }

    private fun getMailProperties(): Properties {
        val prop = Properties()
        prop.setProperty("mail.transport.protocol", "smtp")
        prop.setProperty("mail.smtp.auth", "true")  // smtp 인증
        prop.setProperty("mail.smtp.ssl.trust", "smtp.naver.com")  // ssl 인증 서버
        prop.setProperty("mail.smtp.ssl.enable", "true")
        prop.setProperty("mail.smtp.starttls.enable", "true")  // smtp starttls 사용
        prop.setProperty("mail.debug", "true")
        return prop
    }

}

Mailer Service

interface MailerService {

    fun send(toAddress: String, subject: String, htmlContent: String)

}
import jakarta.mail.Message.RecipientType
import jakarta.mail.internet.InternetAddress
import org.springframework.beans.factory.annotation.Value
import org.springframework.mail.MailException
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.stereotype.Service

@Service
class NaverMailerService(
    private val emailSender: JavaMailSender,
    @Value("\${naver.mail.from-name}") private val fromName: String,
    @Value("\${naver.mail.username}") private val fromAddress: String
) : MailerService {

    override fun send(toAddress: String, subject: String, htmlContent: String) {
        val message = emailSender.createMimeMessage()
        message.addRecipients(RecipientType.TO, toAddress)
        message.subject = subject
        message.setText(htmlContent, "UTF-8", "html")
        message.setFrom(InternetAddress(fromAddress, fromName))

        try {
            emailSender.send(message)
        } catch (e: MailException) {
            e.printStackTrace()
        }
    }

}

Send

@Service
class HelpServiceImpl(
    private val mailerService: MailerService
) : HelpService {

    override fun sendVerifyEmail(email: String) {
        val subject = "인증 번호 안내"
        val content = "<html>" +
            "<body>" +
            "<div style='margin:0 auto;width:100px; height:50px; border:1px solid black; text-align:center; line-height:45px;'>$code</div>" +
            "</body>" +
            "</html>"
        mailerService.send(email, subject, content)
    }

}

민갤

Back-End Developer

백엔드 개발자입니다.