bootsaas logo

Emails

Email sending in bootsaas is implemented using org.springframework.mail package, which under the hood uses JavaMailSender

This means you can send emails in a 'native' way over SMTP, and not rely on specific API integration.

You will likely still choose to use some service like Mailgun or Postmark or whatever else, but you're not locked in to any of them, if they support SMTP you can use it.

Just provide the smtp credentials into your application.properties file and you're good to go.

There is also support for defining your own email templates within the application.

Templates are placed in /resources/mail-templates and implemented with thymeleaf.

There are 3 basic templates defined already:

welcome-emailSent when user registers, contains a verification link.

password-resetSent when user requests a password reset link.

invite-emailSent when to a user that is invited to an organization.

Here is a code example of sending an email:

OrganizationService#sendInvite
... SendInviteEmailJob job = new SendInviteEmailJob(invite.getId()); ...
SendInviteEmailJob
public class SendInviteEmailJob implements JobRequest { private Long inviteId; @Override public Class<? extends JobRequestHandler> getJobRequestHandler() { return SendInviteEmailJobHandler.class; } }
SendInviteEmailJobHandler
public class SendInviteEmailJobHandler implements JobRequestHandler<SendInviteEmailJob> { private final SendInviteEmailMessageFactory messageFactory; private final OrganizationInviteRepository organizationInviteRepository; @Transactional @Override public void run(SendInviteEmailJob sendInviteEmailJob) throws Exception { OrganizationInvite invite = organizationInviteRepository.findById(sendInviteEmailJob.getInviteId()).orElseThrow(); if (!invite.isEmailSent()) { messageFactory.sendInviteEmail(invite); invite.setEmailSent(true); organizationInviteRepository.save(invite); } } }
SendInviteEmailMessageFactory
public class SendInviteEmailMessageFactory { private final EmailService emailService; private final ApplicationProperties applicationProperties; private final SpringTemplateEngine templateEngine; @Transactional public void sendInviteEmail(OrganizationInvite invite) { String invitelink = applicationProperties.getBaseUrl() + "/auth/register?token=" + invite.getToken(); Context context = new Context(); context.setVariable("organizationName", invite.getOrganization().getName()); context.setVariable("inviteLink", invitelink); String htmlBody = templateEngine.process("invite-email", context); emailService.sendHtmlMessage(List.of(invite.getEmail()), "You have been invited to join " + invite.getOrganization().getName(), htmlBody); } }
bootsaas logoCopyright © 2024 - All rights reserved