Setting up Yandex mail server spring boot

In this article I would like to tell you how to set up sending mail from your personal mail using Yandex and Spring Boot. I have seen many articles on setting up an smtp server on Spring Boot, but without a link to Yandex. And this is a key point, since finding free smtp servers is not so easy.

First, we need Yandex mail. Then we look at what configs we need here. Yandex offers to create a password for our application. Which we do linkWhen creating a password, copy it to yourself, since it is only visible the first time.

In essence, we are done with Yandex.

Let's go to our project. I assume that you already have Spring boot configured. So you need to add a dependency for mail. I use Gradle, so for me it looks like this:

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

In addition, you will need to add configs to the application.properties file.

#email
spring.mail.host=smtp.yandex.ru
spring.mail.port=465
spring.mail.protocol=smtps
spring.mail.username=яндекс_логин
spring.mail.password=яндекс_пароль
mail.debug=false - #опционально, можно выключать если не нужно

Next, we need to manually create an implementation of the JavaMailSender class. In it, we will specify all the configs for our mail.

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
@Configuration
class MailConfig {
@Value("${spring.mail.host}")
private val host: String? = null
@Value("\${spring.mail.username}")
private val username: String? = null

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

@Value("\${spring.mail.port}")
private val port: Int = 0

@Value("\${spring.mail.protocol}")
private val protocol: String? = null

@Value("\${mail.debug}")
private val debug: String? = null

@Bean
fun getMailSender(): JavaMailSender {
    val mailSender = JavaMailSenderImpl()
    mailSender.host = host
    mailSender.port = port
    mailSender.username = username
    mailSender.password = password
    val properties = mailSender.javaMailProperties
    properties.setProperty("mail.transport.protocol", protocol)
    properties.setProperty("mail.debug", debug)
    return mailSender
}

}

Using the @Value annotation, we pull data from configs. And in Kotlin, it looks a bit ugly, but there is no way out. Or fill in the configs manually, and we may want to change them depending on the environment.

And the class whose task will be to send letters. Let's call it MailSender.

import org.springframework.beans.factory.annotation.Value
import org.springframework.mail.SimpleMailMessage
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.stereotype.Service
@Service
class MailSender(
private  val mailSender: JavaMailSender
) {
@Value("\${spring.mail.username}")
private val username: String? = null

fun send(emailTo: String, subject: String, message: String) {
    val mailMessage = SimpleMailMessage()
    mailMessage.from = username
    mailMessage.setTo(emailTo)
    mailMessage.subject = subject
    mailMessage.text = message
    mailSender.send(mailMessage)
}

}

That's it. With these simple steps we have created a class that can send mail to users on our behalf. It's free, simple and effective. We can successfully solve such tasks as mail notifications or user mail confirmation.

Thank you for reading to the end. I hope it was useful.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *