[Spring Boot] Naver Mail

Spring Boot

Language :

build.gradle.kts

kotlin

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

Allow Mail SMTP

1. Naver Login > Mail > Preferences > POP3/IMAP Settings

POP3/IMAP Setting

2.  Check the settings

mail setting properties

application.yml

yml

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

MailConfig

kotlin

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 authentication
        prop.setProperty("mail.smtp.ssl.trust", "smtp.naver.com")  // ssl authentication server
        prop.setProperty("mail.smtp.ssl.enable", "true")
        prop.setProperty("mail.smtp.starttls.enable", "true")  // use smtp starttls
        prop.setProperty("mail.debug", "true")
        return prop
    }

}

Mailer Service

kotlin

interface MailerService {

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

}

kotlin

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

kotlin

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

    override fun sendVerifyEmail(email: String) {
        val subject = "Certification Number Guide"
        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

백엔드 개발자입니다.