|
|
@ -1,7 +1,9 @@
|
|
|
|
package entgra.mailsender.Service;
|
|
|
|
package entgra.mailsender.Service.Impl;
|
|
|
|
|
|
|
|
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
|
|
|
|
import entgra.mailsender.Service.MailQueueService;
|
|
|
|
|
|
|
|
import entgra.mailsender.Service.MailService;
|
|
|
|
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;
|
|
|
@ -21,8 +23,6 @@ public class MailServiceImpl implements MailService {
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private MailQueueService mailQueueService;
|
|
|
|
private MailQueueService mailQueueService;
|
|
|
|
|
|
|
|
|
|
|
|
// private final mailQueueService mailQueueService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PreDestroy
|
|
|
|
@PreDestroy
|
|
|
|
public void shutdown() {
|
|
|
|
public void shutdown() {
|
|
|
@ -42,29 +42,14 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
|
|
logger.warning("email address : "+emailModel.getEmailAddress());
|
|
|
|
logger.warning("email address : "+emailModel.getEmailAddress());
|
|
|
|
if (isValidEmailAddress(emailModel.getEmailAddress())){
|
|
|
|
if (isValidEmailAddress(emailModel.getEmailAddress())){
|
|
|
|
throw new RuntimeException("Invalid Email address 161616161");
|
|
|
|
throw new RuntimeException("Invalid Email address");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mailDAO.addMail(emailModel); // save the mail details in the database
|
|
|
|
mailDAO.addMail(emailModel); // save the mail details in the database
|
|
|
|
syncMailWithDB();
|
|
|
|
syncMailWithDB(); //here we need to check the unsent mail details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//here we need to check the unsent mail details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//PriorityQueue<MailModel> mailwithHighPriority = mailDAO.getMailwithHighPriority();
|
|
|
|
|
|
|
|
// MailModel mailModel = mailQueueService.getHighPriorityMail();
|
|
|
|
|
|
|
|
// assert mailModel != null;
|
|
|
|
|
|
|
|
// Integer mail_id = Integer.valueOf(mailModel.getEmailAddress());
|
|
|
|
|
|
|
|
// logger.info(mailModel.getEmailAddress());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MailModel updatedModel = mailDAO.getMailDetails(mail_id);
|
|
|
|
|
|
|
|
// logger.info("mail id" + updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isValidEmailAddress(String email){
|
|
|
|
public boolean isValidEmailAddress(String email){
|
|
|
|
String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
|
|
|
String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
|
|
|
return !email.matches(regex);
|
|
|
|
return !email.matches(regex);
|
|
|
@ -83,7 +68,8 @@ public class MailServiceImpl implements MailService {
|
|
|
|
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
|
|
|
|
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
|
|
|
|
MailModel prioritizedMail = mailQueueService.getHighPriorityMail();
|
|
|
|
MailModel prioritizedMail = mailQueueService.getHighPriorityMail();
|
|
|
|
MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId());
|
|
|
|
MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId());
|
|
|
|
logger.info("updated mail" + updatedModel.getEmailAddress());
|
|
|
|
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);
|
|
|
@ -105,12 +91,15 @@ public class MailServiceImpl implements MailService {
|
|
|
|
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
|
|
|
|
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append text to email body
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
logger.info(updatedModel.getFilename());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Append attachments to email body
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile());
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile());
|
|
|
|
|
|
|
|
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Store the sent mail to the table
|
|
|
|
mailDAO.addToSentMail(updatedModel.getEmailId());
|
|
|
|
mailDAO.addToSentMail(updatedModel.getEmailId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|