feat(email): add email service and configuration for sending emails
This commit is contained in:
parent
7f2aeefba2
commit
110dacb6dd
7 changed files with 114 additions and 1 deletions
|
@ -54,6 +54,7 @@ dependencies {
|
||||||
implementation("io.jsonwebtoken:jjwt-api:0.11.5")
|
implementation("io.jsonwebtoken:jjwt-api:0.11.5")
|
||||||
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5")
|
runtimeOnly("io.jsonwebtoken:jjwt-impl:0.11.5")
|
||||||
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")
|
runtimeOnly("io.jsonwebtoken:jjwt-jackson:0.11.5")
|
||||||
|
compileOnly("org.springframework.boot:spring-boot-starter-mail")
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType<Test> {
|
tasks.withType<Test> {
|
||||||
|
|
|
@ -8,6 +8,10 @@ import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.annotation.Bean;
|
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 org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
@ -26,6 +30,11 @@ public class CasinoApplication {
|
||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public static JavaMailSenderImpl javaMailSenderImpl() {
|
||||||
|
return new JavaMailSenderImpl();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public CommandLineRunner initData(LootBoxRepository lootBoxRepository, RewardRepository rewardRepository) {
|
public CommandLineRunner initData(LootBoxRepository lootBoxRepository, RewardRepository rewardRepository) {
|
||||||
return _ -> {
|
return _ -> {
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package de.szut.casino.health;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@ -8,8 +11,17 @@ import java.util.Map;
|
||||||
@RestController
|
@RestController
|
||||||
public class HealthController {
|
public class HealthController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private EmailService emailService;
|
||||||
|
|
||||||
@GetMapping("/health")
|
@GetMapping("/health")
|
||||||
public Map<String, String> healthCheck() {
|
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");
|
return Map.of("status", "UP");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
|
@ -5,8 +5,16 @@ server.port=${HTTP_PORT:8080}
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
stripe.secret.key=${STRIPE_SECRET_KEY:sk_test_51QrePYIvCfqz7ANgqam8rEwWcMeKiLOof3j6SCMgu2sl4sESP45DJxca16mWcYo1sQaiBv32CMR6Z4AAAGQPCJo300ubuZKO8I}
|
stripe.secret.key=${STRIPE_SECRET_KEY:sk_test_51QrePYIvCfqz7ANgqam8rEwWcMeKiLOof3j6SCMgu2sl4sESP45DJxca16mWcYo1sQaiBv32CMR6Z4AAAGQPCJo300ubuZKO8I}
|
||||||
stripe.webhook.secret=${STRIPE_WEBHOOK_SECRET:whsec_746b6a488665f6057118bdb4a2b32f4916f16c277109eeaed5e8f8e8b81b8c15}
|
stripe.webhook.secret=${STRIPE_WEBHOOK_SECRET:whsec_746b6a488665f6057118bdb4a2b32f4916f16c277109eeaed5e8f8e8b81b8c15}
|
||||||
|
|
||||||
app.frontend-host=${FE_URL:http://localhost:4200}
|
app.frontend-host=${FE_URL:http://localhost:4200}
|
||||||
|
|
||||||
|
app.mail.authentication=${MAIL_AUTHENTICATION:false}
|
||||||
|
app.mail.host=${MAIL_HOST:localhost}
|
||||||
|
app.mail.port=${MAIL_PORT:1025}
|
||||||
|
app.mail.username=${MAIL_USER:null}
|
||||||
|
app.mail.password=${MAIL_PASS:null}
|
||||||
|
app.mail.from-address=${MAIL_FROM:casino@localhost}
|
||||||
|
|
||||||
spring.application.name=casino
|
spring.application.name=casino
|
||||||
|
|
||||||
# JWT Configuration
|
# JWT Configuration
|
||||||
|
|
12
compose.yml
12
compose.yml
|
@ -15,4 +15,14 @@ services:
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: "exit 0"
|
test: "exit 0"
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
|
mailpit:
|
||||||
|
image: axllent/mailpit
|
||||||
|
container_name: casino-mailpit
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- 8025:8025
|
||||||
|
- 1025:1025
|
||||||
|
environment:
|
||||||
|
MP_SMTP_AUTH_ACCEPT_ANY: 1
|
||||||
|
MP_SMTP_AUTH_ALLOW_INSECURE: 1
|
||||||
|
|
Reference in a new issue