1 | package it.finki.charitable.services;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.EmailMessage;
|
---|
4 | import org.springframework.mail.SimpleMailMessage;
|
---|
5 | import org.springframework.mail.javamail.JavaMailSender;
|
---|
6 | import org.springframework.scheduling.annotation.Async;
|
---|
7 | import org.springframework.stereotype.Component;
|
---|
8 |
|
---|
9 | @Component
|
---|
10 | public class EmailService {
|
---|
11 |
|
---|
12 | private final JavaMailSender javaMailSender;
|
---|
13 | private final EmailMessageService emailMessageService;
|
---|
14 |
|
---|
15 | public EmailService(JavaMailSender javaMailSender, EmailMessageService emailMessageService) {
|
---|
16 | this.javaMailSender = javaMailSender;
|
---|
17 | this.emailMessageService = emailMessageService;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public void sendValidationEmail(String to, String subject, String token) {
|
---|
21 | SimpleMailMessage message = new SimpleMailMessage();
|
---|
22 | message.setTo(to);
|
---|
23 | message.setSubject(subject);
|
---|
24 |
|
---|
25 | String text = "Verify your account on the following link\n" +
|
---|
26 | "http://localhost:9091/validate?token=" + token;
|
---|
27 | message.setText(text);
|
---|
28 | sendMail(message);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public void sendApprovalEmail(String to, String subject, Long postId) {
|
---|
32 | SimpleMailMessage message = new SimpleMailMessage();
|
---|
33 | message.setTo(to);
|
---|
34 | message.setSubject(subject);
|
---|
35 |
|
---|
36 | String text = "Your post has been approved\n" + "http://localhost:9091/post?postid=" + postId;
|
---|
37 | message.setText(text);
|
---|
38 | sendMail(message);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void sendNoApprovalEmail(String to, String subject, String description) {
|
---|
42 | SimpleMailMessage message = new SimpleMailMessage();
|
---|
43 | message.setTo(to);
|
---|
44 | message.setSubject(subject);
|
---|
45 |
|
---|
46 | String text = "Sorry, your post hasn't been approved\n" +
|
---|
47 | "Moderator:\n" + description;
|
---|
48 | message.setText(text);
|
---|
49 | sendMail(message);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void sendDeletionEmail(String to, String subject, String description) {
|
---|
53 | SimpleMailMessage message = new SimpleMailMessage();
|
---|
54 | message.setTo(to);
|
---|
55 | message.setSubject(subject);
|
---|
56 |
|
---|
57 | String text = "Sorry, your post has been deleted\n" +
|
---|
58 | "Moderator:\n" + description;
|
---|
59 | message.setText(text);
|
---|
60 | sendMail(message);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Async
|
---|
64 | public void sendMail(SimpleMailMessage message) {
|
---|
65 | try {
|
---|
66 | javaMailSender.send(message);
|
---|
67 |
|
---|
68 | } catch (Exception e) {
|
---|
69 | emailMessageService.save(new EmailMessage(message.getTo()[0], message.getSubject(), message.getText()));
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|