|
|
@ -4,6 +4,8 @@ import entgra.mailsender.DAO.MailDAO;
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
import entgra.mailsender.Service.MailQueueService;
|
|
|
|
import entgra.mailsender.Service.MailQueueService;
|
|
|
|
import entgra.mailsender.Service.MailService;
|
|
|
|
import entgra.mailsender.Service.MailService;
|
|
|
|
|
|
|
|
import entgra.mailsender.exception.BadRequestException;
|
|
|
|
|
|
|
|
import entgra.mailsender.exception.MailException;
|
|
|
|
import entgra.mailsender.util.PriorityQueueHolder;
|
|
|
|
import entgra.mailsender.util.PriorityQueueHolder;
|
|
|
|
import jakarta.annotation.PreDestroy;
|
|
|
|
import jakarta.annotation.PreDestroy;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@ -11,6 +13,7 @@ import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
|
@ -38,15 +41,66 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void sendEmail(MailModel emailModel){
|
|
|
|
public void sendEmail(MailModel emailModel) throws MailException {
|
|
|
|
|
|
|
|
if (shutdownRequested) {
|
|
|
|
|
|
|
|
String msg = "Server is shutting down. SMS requests will not be accepted.";
|
|
|
|
|
|
|
|
logger.warning(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.warning("email address : "+emailModel.getEmailAddress());
|
|
|
|
|
|
|
|
if (isValidEmailAddress(emailModel.getEmailAddress())){
|
|
|
|
if (isValidEmailAddress(emailModel.getEmailAddress())){
|
|
|
|
throw new RuntimeException("Invalid Email address");
|
|
|
|
String msg = "Invalid Email address";
|
|
|
|
|
|
|
|
logger.warning(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mailDAO.addMail(emailModel); // save the mail details in the database
|
|
|
|
int emailid = mailDAO.addMail(emailModel); // save the mail details in the database and get the auto generated id
|
|
|
|
syncMailWithDB(); //here we need to check the unsent mail details
|
|
|
|
|
|
|
|
|
|
|
|
emailModel.setEmailId(emailid);
|
|
|
|
|
|
|
|
emailModel.setFilename(emailModel.getAttachment().getOriginalFilename());
|
|
|
|
|
|
|
|
//add to the priority queue
|
|
|
|
|
|
|
|
List<MailModel> mailModels = new ArrayList<>();
|
|
|
|
|
|
|
|
mailModels.add(emailModel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
mailQueueService.enqueMails(mailModels);
|
|
|
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//send the high priority mail
|
|
|
|
|
|
|
|
while (PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null){
|
|
|
|
|
|
|
|
MailModel prioritizedMail =mailQueueService.getHighPriorityMail();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
javaMailSender.send(mimeMessage -> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
|
|
|
|
mimeMessageHelper.setTo(prioritizedMail.getEmailAddress());
|
|
|
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
|
|
|
|
emailBody.append(prioritizedMail.getMsgTemplate()).append("\n");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append parameters to email body
|
|
|
|
|
|
|
|
List<MailModel.Parameter> parameters = prioritizedMail.getParameters();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
|
|
|
|
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append text to email body
|
|
|
|
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append attachments to email body
|
|
|
|
|
|
|
|
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getAttachment());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Store the sent mail to the table
|
|
|
|
|
|
|
|
mailDAO.addToSentMail(prioritizedMail.getEmailId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -66,22 +120,19 @@ public class MailServiceImpl implements MailService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
|
|
|
|
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
|
|
|
|
MailModel prioritizedMail = mailQueueService.getHighPriorityMail();
|
|
|
|
MailModel prioritizedMail = mailDAO.getMailDetails(mailQueueService.getHighPriorityMail().getEmailId());
|
|
|
|
MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId());
|
|
|
|
|
|
|
|
logger.info("High priority mail : " + updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
javaMailSender.send(mimeMessage -> {
|
|
|
|
javaMailSender.send(mimeMessage -> {
|
|
|
|
|
|
|
|
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
mimeMessageHelper.setTo(updatedModel.getEmailAddress());
|
|
|
|
mimeMessageHelper.setTo(prioritizedMail.getEmailAddress());
|
|
|
|
logger.info(updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
emailBody.append(updatedModel.getMsgTemplate()).append("\n");
|
|
|
|
emailBody.append(prioritizedMail.getMsgTemplate()).append("\n");
|
|
|
|
|
|
|
|
|
|
|
|
// Append parameters to email body
|
|
|
|
// Append parameters to email body
|
|
|
|
List<MailModel.Parameter> parameters = updatedModel.getParameters();
|
|
|
|
List<MailModel.Parameter> parameters = prioritizedMail.getParameters();
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
System.out.println("Parameter name: " + parameter.getKey());
|
|
|
|
System.out.println("Parameter name: " + parameter.getKey());
|
|
|
|
System.out.println("Parameter value: " + parameter.getValue());
|
|
|
|
System.out.println("Parameter value: " + parameter.getValue());
|
|
|
@ -95,12 +146,10 @@ public class MailServiceImpl implements MailService {
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
|
|
|
|
|
|
|
|
// Append attachments to email body
|
|
|
|
// Append attachments to email body
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile());
|
|
|
|
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getFile());
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Store the sent mail to the table
|
|
|
|
//Store the sent mail to the table
|
|
|
|
mailDAO.addToSentMail(updatedModel.getEmailId());
|
|
|
|
mailDAO.addToSentMail(prioritizedMail.getEmailId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|