|
|
@ -2,13 +2,14 @@ package entgra.mailsender.Service;
|
|
|
|
|
|
|
|
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
|
|
|
|
import entgra.mailsender.util.PriorityQueueHolder;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
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.List;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.PriorityQueue;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -21,9 +22,12 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
private MailDAO mailDAO;
|
|
|
|
private MailDAO mailDAO;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private MailQueueService mailQueueService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void sendEmail(MailModel emailModel){
|
|
|
|
public void sendEmail(MailModel emailModel){
|
|
|
|
|
|
|
|
|
|
|
|
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 161616161");
|
|
|
@ -31,53 +35,75 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
|
|
mailDAO.addMail(emailModel); // save the mail details in the database
|
|
|
|
mailDAO.addMail(emailModel); // save the mail details in the database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//here we need to check the unsent mail details
|
|
|
|
//here we need to check the unsent mail details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PriorityQueue<MailModel> mailwithHighPriority = mailDAO.getMailwithHighPriority();
|
|
|
|
//PriorityQueue<MailModel> mailwithHighPriority = mailDAO.getMailwithHighPriority();
|
|
|
|
MailModel mailModel = mailwithHighPriority.poll();
|
|
|
|
// MailModel mailModel = mailQueueService.getHighPriorityMail();
|
|
|
|
assert mailModel != null;
|
|
|
|
// assert mailModel != null;
|
|
|
|
Integer mail_id = mailModel.getEmailId();
|
|
|
|
// Integer mail_id = Integer.valueOf(mailModel.getEmailAddress());
|
|
|
|
|
|
|
|
// logger.info(mailModel.getEmailAddress());
|
|
|
|
|
|
|
|
|
|
|
|
MailModel updatedModel = mailDAO.getMailDetails(mail_id);
|
|
|
|
// MailModel updatedModel = mailDAO.getMailDetails(mail_id);
|
|
|
|
logger.info(updatedModel.getEmailAddress());
|
|
|
|
// logger.info("mail id" + updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
javaMailSender.send(mimeMessage -> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
|
|
|
|
mimeMessageHelper.setTo(updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
logger.info(updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
}
|
|
|
|
emailBody.append(updatedModel.getMsgTemplate()).append("\n");
|
|
|
|
public boolean isValidEmailAddress(String email){
|
|
|
|
|
|
|
|
String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
|
|
|
|
|
|
|
return !email.matches(regex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Append parameters to email body
|
|
|
|
public void syncMailWithDB(){
|
|
|
|
List<MailModel.Parameter> parameters = updatedModel.getParameters();
|
|
|
|
try {
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
List<MailModel> mailModels = mailDAO.getUnsentMessages();
|
|
|
|
System.out.println("Parameter name: " + parameter.getKey());
|
|
|
|
if (!mailModels.isEmpty()){
|
|
|
|
System.out.println("Parameter value: " + parameter.getValue());
|
|
|
|
mailQueueService.enqueMails(mailModels);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
|
|
|
|
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
|
|
|
|
MailModel prioritizedMail = mailQueueService.getHighPriorityMail();
|
|
|
|
}
|
|
|
|
MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId());
|
|
|
|
|
|
|
|
logger.info("updated mail" + updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
javaMailSender.send(mimeMessage -> {
|
|
|
|
|
|
|
|
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
logger.info(updatedModel.getFilename());
|
|
|
|
mimeMessageHelper.setTo(updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
logger.info(updatedModel.getEmailAddress());
|
|
|
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
|
|
|
|
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename() ,updatedModel.getFile());
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
emailBody.append(updatedModel.getMsgTemplate()).append("\n");
|
|
|
|
|
|
|
|
|
|
|
|
mailDAO.addToSentMail(updatedModel.getEmailId());
|
|
|
|
// Append parameters to email body
|
|
|
|
|
|
|
|
List<MailModel.Parameter> parameters = updatedModel.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());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
}
|
|
|
|
logger.info(updatedModel.getFilename());
|
|
|
|
public boolean isValidEmailAddress(String email){
|
|
|
|
|
|
|
|
String regex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile());
|
|
|
|
return !email.matches(regex);
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mailDAO.addToSentMail(updatedModel.getEmailId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|