|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
package entgra.mailsender.Service.Impl;
|
|
|
|
|
|
|
|
|
|
import entgra.mailsender.Controller.MailController;
|
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
|
import entgra.mailsender.Service.MailQueueService;
|
|
|
|
@ -9,6 +10,8 @@ import entgra.mailsender.exception.MailException;
|
|
|
|
|
import entgra.mailsender.exception.QueueException;
|
|
|
|
|
import entgra.mailsender.util.PriorityQueueHolder;
|
|
|
|
|
import jakarta.annotation.PreDestroy;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
@ -17,12 +20,11 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class MailServiceImpl implements MailService {
|
|
|
|
|
Logger logger = Logger.getLogger(String.valueOf(MailServiceImpl.class));
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);
|
|
|
|
|
private volatile boolean shutdownRequested = false;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@ -43,20 +45,29 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
public void sendEmail(MailModel emailModel) throws MailException, SQLException {
|
|
|
|
|
if (shutdownRequested) {
|
|
|
|
|
String msg = "Server is shutting down. SMS requests will not be accepted.";
|
|
|
|
|
logger.warning(msg);
|
|
|
|
|
logger.info(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isValidEmailAddress(emailModel.getEmailAddress())) {
|
|
|
|
|
String msg = "Invalid Email address";
|
|
|
|
|
logger.warning(msg);
|
|
|
|
|
logger.warn(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (emailModel.getMsgTemplate().isBlank()) {
|
|
|
|
|
String msg = "Empty message template";
|
|
|
|
|
logger.warn(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int emailid = mailDAO.addMail(emailModel); // save the mail details in the database and get the auto generated id
|
|
|
|
|
|
|
|
|
|
emailModel.setEmailId(emailid);
|
|
|
|
|
emailModel.setFilename(emailModel.getAttachment().getOriginalFilename());
|
|
|
|
|
emailModel.setEmailId(emailid); //uuid
|
|
|
|
|
if (emailModel.getAttachment() != null) {
|
|
|
|
|
emailModel.setFilename(emailModel.getAttachment().getOriginalFilename());
|
|
|
|
|
}
|
|
|
|
|
//add to the priority queue
|
|
|
|
|
List<MailModel> mailModels = new ArrayList<>();
|
|
|
|
|
mailModels.add(emailModel);
|
|
|
|
@ -76,7 +87,7 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
|
mimeMessageHelper.setTo(prioritizedMail.getEmailAddress());
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");//input as a param key
|
|
|
|
|
|
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
|
emailBody.append(prioritizedMail.getMsgTemplate()).append("\n");
|
|
|
|
@ -92,7 +103,9 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
|
|
|
|
|
|
// Append attachments to email body
|
|
|
|
|
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getAttachment());
|
|
|
|
|
if (prioritizedMail.getAttachment() != null) {
|
|
|
|
|
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getAttachment());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Store the sent mail to the table
|
|
|
|
|
mailDAO.addToSentMail(prioritizedMail.getEmailId());
|
|
|
|
@ -133,11 +146,6 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
// Append parameters to email body
|
|
|
|
|
List<MailModel.Parameter> parameters = prioritizedMail.getParameters();
|
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
|
System.out.println("Parameter name: " + parameter.getKey());
|
|
|
|
|
System.out.println("Parameter value: " + parameter.getValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
|
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
|
|
|
|
|
}
|
|
|
|
@ -146,7 +154,10 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
|
|
|
|
|
|
// Append attachments to email body
|
|
|
|
|
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getFile());
|
|
|
|
|
if (prioritizedMail.getAttachment() != null) {
|
|
|
|
|
mimeMessageHelper.addAttachment(prioritizedMail.getFilename(), prioritizedMail.getFile());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Store the sent mail to the table
|
|
|
|
|
mailDAO.addToSentMail(prioritizedMail.getEmailId());
|
|
|
|
|