forked from Spurthi-Ravula/Group2-Student-Info-Exchange-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailServiceImpl.java
73 lines (56 loc) · 2.65 KB
/
EmailServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.example.SMS.service;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.example.SMS.dto.CertificateAssistanceForm;
@Service
@Transactional
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender javaMailSender;
public void sendOtpMessage(String to, int OTP) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setTo(to);
String subject = "Here's your One Time Password (OTP)";
String content = "<p>Hello " + to + "</p>"
+ "<p>For security reason, you're required to use the following "
+ "One Time Password to login:</p>"
+ "<p><b>" + OTP + "</b></p>";
helper.setSubject(subject);
helper.setText(content, true);
javaMailSender.send(msg);
}
public void sendCertificateMessage(CertificateAssistanceForm certificateAssistanceForm, MultipartFile file) throws Exception {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setTo(certificateAssistanceForm.getEmailId());
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattedDate = currentDate.format(formatter);
String subject = "New Certification Assistance is added!";
String content = "<html><body>"
+ "<p>New Certification Assistance request for type "
+ "<b>" + certificateAssistanceForm.getRequestType() + "</b>"
+ " was added successfully on "
+ formattedDate + "</p>"
+ "<p> Description: "+ certificateAssistanceForm.getDescription() + "</p>"
+ "</body></html>";
helper.setSubject(subject);
helper.setText(content, true);
// Attach the file
if (file != null && !file.isEmpty()) {
ByteArrayResource fileResource = new ByteArrayResource(file.getBytes());
helper.addAttachment(file.getOriginalFilename(), fileResource);
}
javaMailSender.send(msg);
}
}