Docker + SMTP + Java

Not long ago I had a need to make registration with email confirmation on several projects, I also wanted to minimize costs and eliminate unnecessary body movements, so to speak, using the ctrl+c ctrl+v method, without dancing with a tambourine. Also, we do not need to receive mail and so on, just sending, nothing more

P.s. For seasoned development fathers there will be nothing new here

So, let's go 😉

VPS rental

The most important thing is that port 465 is open (Many either close them completely, or you need to go through 7 circles of hell)

Also resources, empirically it was found that 2 CPU cores and 2 GB RAM are needed, on smaller resources it freezes even in idle mode, and you should forget about minimal mailings altogether

You can take it from one well-known German hosting provider like this (of course there are problems with payment from the Russian Federation, but if you bother, it is more profitable than overpaying resellers x3-x4)

OS and SMTP setup

  • Pristine Ubuntu latest versions out of the box

  • Docker

  • Install Mailu, they have a cool one configurator

How to configure?

  • Add A record ala mail.example.com A your_ip (otherwise letsencrypt will not pull the certificate)

  • At the host, add a PTR record to the IP address that will refer to mail.example.com

  • Further

We only need shipping, nothing more.

We only need shipping, nothing more.

That's all, the configurator will then give us a set of commands

We execute the commands, go to the admin panel https://mail.example.com

The only thing that matters is that at the last stage instead of PASSWORD you can specify your_passwordso that you don’t have to change it later via the UI

Setting up Mailu

Log in to the admin panel by specifying admin@mail.example.com And your_password

In the admin panel itself, go to the tab – Mail domains – Actions

What's important to us here is Generate keys And download all settings (Download zonefile)

Next, import the zonefile into your domain.

And the last step is to create a user – specify a login (for example, noreply) and password

The output will be noreply@mail.example.com And password

In fact, that's it, the mail server is set up and ready to use, all that remains is to use it in the application

Java

We use Spring Boot, depending on which we add

implementation 'org.springframework.boot:spring-boot-starter-mail'

In application.yml

spring:
  mail:
    host: mail.example.com
    port: 465
    username: noreply@mail.example.com
    password: qwer1234
    properties:
      mail:
        smtp:
          auth: true
          ssl:
            enable: true

And the use itself

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
public class MailService {

    private final MailProperties mailProperties;
    private final JavaMailSender mailSender;

    public Mono<Boolean> send(String email, String title, String text) {
        return Mono.fromCallable(() -> {
            try {
                MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
                mimeMessageHelper.setFrom("BestCompany <" + mailProperties.getUsername() + ">");
                mimeMessageHelper.setTo(email);
                mimeMessageHelper.setSubject(title);
                mimeMessageHelper.setText(text, true);
                mailSender.send(mimeMessageHelper.getMimeMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        });
    }
}

Go

Let's add a package

go get gopkg.in/gomail.v2

Initialize the client

	mailDialer := gomail.NewDialer(
		"mail.exmaple.com",
		465,
		"noreply@mail.exmaple.com",
		"qwer1234",
	)
	mailDialer.SSL = true

We will send a letter

	m := gomail.NewMessage()
	m.SetHeader("From", "noreply@mail.exmaple.com")
	m.SetHeader("To", to)
	m.SetHeader("Subject", subject)
	m.SetBody("text/plain", body)

	err := mailDialer.DialAndSend(m)

Profit 😉


My Telegram channel — IT-Syndrome

Similar Posts

Leave a Reply

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