Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/main/java/backend/fullstack/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package backend.fullstack.auth.invite;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
* Sends emails on a background thread so SMTP hangs never block HTTP responses.
*/
@Component
public class AsyncMailSender {

private static final Logger logger = LoggerFactory.getLogger(AsyncMailSender.class);

private final Optional<JavaMailSender> mailSender;

public AsyncMailSender(Optional<JavaMailSender> mailSender) {
this.mailSender = mailSender;
}

@Async
public void send(SimpleMailMessage message) {
if (mailSender.isEmpty()) {
return;
}
try {
mailSender.get().send(message);
} catch (Exception ex) {
logger.error("Failed to send email to {}: {}", message.getTo(), ex.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class UserInviteService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final Optional<JavaMailSender> mailSender;
private final AsyncMailSender asyncMailSender;
private final InviteProperties inviteProperties;
private final SecureRandom secureRandom = new SecureRandom();

Expand All @@ -43,12 +44,14 @@ public UserInviteService(
UserRepository userRepository,
PasswordEncoder passwordEncoder,
Optional<JavaMailSender> mailSender,
AsyncMailSender asyncMailSender,
InviteProperties inviteProperties
) {
this.userInviteTokenRepository = userInviteTokenRepository;
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.mailSender = mailSender;
this.asyncMailSender = asyncMailSender;
this.inviteProperties = inviteProperties;
}

Expand Down Expand Up @@ -113,13 +116,8 @@ private void sendInviteEmail(User user, String token) {
return;
}

try {
mailSender.get().send(message);
logger.info("Invite email sent to userId={} email={}", user.getId(), user.getEmail());
} catch (Exception ex) {
logger.error("Failed to send invite email to userId={} email={}: {}",
user.getId(), user.getEmail(), ex.getMessage());
}
logger.info("Sending invite email to userId={} email={}", user.getId(), user.getEmail());
asyncMailSender.send(message);
}

/**
Expand Down
Loading