Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Integrating SMTP Email Delivery into Apollo Portal

Tech Aug 11 21

To let Apollo Portal send transactional messages—such as password-reset links, account-activation tokens, or system alerts—you can wire an SMTP relay into the application. The walkthrough below assumes Spring Boot 3.x and uses the JavaMail abstraction provided by spring-boot-starter-mail.

1. Bring in the Mail Starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. Externalize Connection Settings

Place the following keys in application.yml (or application.properties if you prefer):

spring:
  mail:
    host: smtp.acme.io
    port: 587
    username: noreply@acme.io
    password: ${MAIL_PASSWORD}           # Prefer an env variable or vault
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
          ssl:
            trust: smtp.acme.io

3. Programmatic Mail-Sender Bean

Create a dedicated configuration clas sothat the mail client is assembled once and reused everywhere:

@Configuration
public class SmtpConfig {

    @Bean
    public JavaMailSender mailSender(Environment env) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setHost(env.getRequiredProperty("spring.mail.host"));
        sender.setPort(env.getRequiredProperty("spring.mail.port", Integer.class));
        sender.setUsername(env.getRequiredProperty("spring.mail.username"));
        sender.setPassword(env.getRequiredProperty("spring.mail.password"));

        Properties extra = sender.getJavaMailProperties();
        extra.put("mail.smtp.auth", "true");
        extra.put("mail.smtp.starttls.enable", "true");
        extra.put("mail.debug", "false");   // flip to true while debugging
        return sender;
    }
}

4. Reusable Mail Dispatch Service

Encapsulate the sending logic in a single component so controllers and schedulers can trigger emails without repeating boilerplate:

@Service
public class PortalNotifier {

    private final JavaMailSender mailSender;

    public PortalNotifier(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void sendPlain(String recipient, String title, String body) {
        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo(recipient);
        msg.setSubject(title);
        msg.setText(body);
        mailSender.send(msg);
    }

    public void sendHtml(String recipient, String title, String html) throws MessagingException {
        MimeMessage mime = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mime, "utf-8");
        helper.setTo(recipient);
        helper.setSubject(title);
        helper.setText(html, true);
        mailSender.send(mime);
    }
}

5. Hooking It into Apollo Portal

Whenever a controller needs to notify a user—e.g., after a successful password-reset request—inject PortalNotifier and call:

notifier.sendPlain(user.getEmail(),
                   "Reset your Apollo Portal password",
                   "Click the link: " + resetUrl);

The same service can be extended to support attachments, inline images, or templated HTML bodies by leveraging MimeMessageHelper and a template engine such as Thymeleaf or FreeMarker.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.