feat(email): add email service and configuration for sending emails

This commit is contained in:
Constantin Simonis 2025-05-07 18:11:54 +02:00
commit 110dacb6dd
7 changed files with 114 additions and 1 deletions

View file

@ -8,6 +8,10 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.client.RestTemplate;
import java.math.BigDecimal;
@ -26,6 +30,11 @@ public class CasinoApplication {
return new RestTemplate();
}
@Bean
public static JavaMailSenderImpl javaMailSenderImpl() {
return new JavaMailSenderImpl();
}
@Bean
public CommandLineRunner initData(LootBoxRepository lootBoxRepository, RewardRepository rewardRepository) {
return _ -> {

View file

@ -1,5 +1,8 @@
package de.szut.casino.health;
import de.szut.casino.security.service.EmailService;
import de.szut.casino.user.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@ -8,8 +11,17 @@ import java.util.Map;
@RestController
public class HealthController {
@Autowired
private EmailService emailService;
@GetMapping("/health")
public Map<String, String> healthCheck() {
UserEntity user = new UserEntity();
user.setEmail("user@mail.com");
user.setUsername("user");
this.emailService.sendRegistrationEmail(user);
return Map.of("status", "UP");
}
}

View file

@ -0,0 +1,48 @@
package de.szut.casino.security.service;
import de.szut.casino.user.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmailService {
private JavaMailSenderImpl mailSender;
private MailConfig mailConfig;
public EmailService(JavaMailSenderImpl mailSender, MailConfig mailConfig) {
try {
this.mailSender = mailSender;
this.mailConfig = mailConfig;
this.mailSender.setHost(mailConfig.host);
this.mailSender.setPort(mailConfig.port);
if (mailConfig.authenticationEnabled) {
this.mailSender.setUsername(mailConfig.username);
this.mailSender.setPassword(mailConfig.password);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void sendRegistrationEmail(UserEntity user) {
String body = "Hallo " + user.getUsername() + ",\n\nA Willkommen bei Trustworthy Casino©! Wir freuen uns, Sie an Bord zu haben.\n\n";
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom(mailConfig.fromAddress);
mail.setSubject("Willkommen bei Trustworthy Casino©");
mail.setTo(user.getEmail());
mail.setText(body);
sendEmail(mail);
}
private void sendEmail(SimpleMailMessage mail) {
this.mailSender.send(mail);
}
}

View file

@ -0,0 +1,25 @@
package de.szut.casino.security.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class MailConfig {
@Value("${app.mail.host}")
public String host;
@Value("${app.mail.port}")
public Integer port;
@Value("${app.mail.authentication}")
public Boolean authenticationEnabled;
@Value("${app.mail.username}")
public String username;
@Value("${app.mail.password}")
public String password;
@Value("${app.mail.from-address}")
public String fromAddress;
}