[Spring Boot] Naver Mail
Spring Boot2023-08-07 18:04 (KST)
Language :
build.gradle.kts
implementation("org.springframework.boot:spring-boot-starter-mail:3.1.1")
Allow Mail SMTP
1. Naver Login > Mail > Preferences > POP3/IMAP Settings
![Untitled.png](https://file.dico.me/2023/08/20230807085007_000.png)
POP3/IMAP Setting
2. Check the settings
![Untitled (1).png](https://file.dico.me/2023/08/20230807085220_000.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 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
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 = "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)
}
}
Previous Article
Using Multiple Github Accounts with SSH Key
ETC
Next Article
[Spring Boot] Event Tracking
Spring Boot