[3fc9e50] | 1 | package it.finki.charitable.services;
|
---|
| 2 |
|
---|
[b8dc761] | 3 | import it.finki.charitable.entities.EmailMessage;
|
---|
[3fc9e50] | 4 | import org.springframework.mail.SimpleMailMessage;
|
---|
| 5 | import org.springframework.mail.javamail.JavaMailSender;
|
---|
[b8dc761] | 6 | import org.springframework.scheduling.annotation.Async;
|
---|
[3fc9e50] | 7 | import org.springframework.stereotype.Component;
|
---|
| 8 |
|
---|
| 9 | @Component
|
---|
| 10 | public class EmailService {
|
---|
| 11 |
|
---|
| 12 | private final JavaMailSender javaMailSender;
|
---|
[b8dc761] | 13 | private final EmailMessageService emailMessageService;
|
---|
[3fc9e50] | 14 |
|
---|
[b8dc761] | 15 | public EmailService(JavaMailSender javaMailSender, EmailMessageService emailMessageService) {
|
---|
[3fc9e50] | 16 | this.javaMailSender = javaMailSender;
|
---|
[b8dc761] | 17 | this.emailMessageService = emailMessageService;
|
---|
[3fc9e50] | 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" +
|
---|
[b8dc761] | 26 | "http://localhost:9091/validate?token=" + token;
|
---|
[3fc9e50] | 27 | message.setText(text);
|
---|
[b8dc761] | 28 | sendMail(message);
|
---|
[3fc9e50] | 29 | }
|
---|
[5306751] | 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 |
|
---|
[b8dc761] | 36 | String text = "Your post has been approved\n" + "http://localhost:9091/post?postid=" + postId;
|
---|
[5306751] | 37 | message.setText(text);
|
---|
[b8dc761] | 38 | sendMail(message);
|
---|
[5306751] | 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 |
|
---|
[ab49338] | 46 | String text = "Sorry, your post hasn't been approved\n" +
|
---|
| 47 | "Moderator:\n" + description;
|
---|
| 48 | message.setText(text);
|
---|
[b8dc761] | 49 | sendMail(message);
|
---|
[ab49338] | 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" +
|
---|
[5306751] | 58 | "Moderator:\n" + description;
|
---|
| 59 | message.setText(text);
|
---|
[b8dc761] | 60 | sendMail(message);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Async
|
---|
| 64 | public void sendMail(SimpleMailMessage message) {
|
---|
| 65 | try {
|
---|
| 66 | javaMailSender.send(message);
|
---|
| 67 | } catch (Exception e) {
|
---|
| 68 | emailMessageService.save(new EmailMessage(message.getTo()[0], message.getSubject(), message.getText()));
|
---|
| 69 | }
|
---|
[5306751] | 70 | }
|
---|
[3fc9e50] | 71 | }
|
---|