|
|
|
@ -2,13 +2,14 @@ package entgra.mailsender.Service;
|
|
|
|
|
|
|
|
|
|
import entgra.mailsender.DAO.MailDAO;
|
|
|
|
|
import entgra.mailsender.DTO.MailModel;
|
|
|
|
|
import entgra.mailsender.util.PriorityQueueHolder;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
|
|
import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.PriorityQueue;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -21,9 +22,12 @@ public class MailServiceImpl implements MailService {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private MailDAO mailDAO;
|
|
|
|
|
@Autowired
|
|
|
|
|
private MailQueueService mailQueueService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void sendEmail(MailModel emailModel){
|
|
|
|
|
|
|
|
|
|
logger.warning("email address : "+emailModel.getEmailAddress());
|
|
|
|
|
if (isValidEmailAddress(emailModel.getEmailAddress())){
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//here we need to check the unsent mail details
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PriorityQueue<MailModel> mailwithHighPriority = mailDAO.getMailwithHighPriority();
|
|
|
|
|
MailModel mailModel = mailwithHighPriority.poll();
|
|
|
|
|
assert mailModel != null;
|
|
|
|
|
Integer mail_id = mailModel.getEmailId();
|
|
|
|
|
//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(updatedModel.getEmailAddress());
|
|
|
|
|
// MailModel updatedModel = mailDAO.getMailDetails(mail_id);
|
|
|
|
|
// 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
|
|
|
|
|
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());
|
|
|
|
|
public void syncMailWithDB(){
|
|
|
|
|
try {
|
|
|
|
|
List<MailModel> mailModels = mailDAO.getUnsentMessages();
|
|
|
|
|
if (!mailModels.isEmpty()){
|
|
|
|
|
mailQueueService.enqueMails(mailModels);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (MailModel.Parameter parameter : parameters) {
|
|
|
|
|
emailBody.append("\n").append(parameter.getKey()).append(": ").append(parameter.getValue());
|
|
|
|
|
}
|
|
|
|
|
while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) {
|
|
|
|
|
MailModel prioritizedMail = mailQueueService.getHighPriorityMail();
|
|
|
|
|
MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId());
|
|
|
|
|
logger.info("updated mail" + updatedModel.getEmailAddress());
|
|
|
|
|
javaMailSender.send(mimeMessage -> {
|
|
|
|
|
|
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
|
logger.info(updatedModel.getFilename());
|
|
|
|
|
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
|
|
|
|
|
mimeMessageHelper.setTo(updatedModel.getEmailAddress());
|
|
|
|
|
logger.info(updatedModel.getEmailAddress());
|
|
|
|
|
mimeMessageHelper.setSubject("Bill details for March ");
|
|
|
|
|
|
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename() ,updatedModel.getFile());
|
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
|
StringBuilder emailBody = new StringBuilder();
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
mimeMessageHelper.setText(emailBody.toString(), false);
|
|
|
|
|
logger.info(updatedModel.getFilename());
|
|
|
|
|
|
|
|
|
|
mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile());
|
|
|
|
|
logger.info("Mail sent successfully");
|
|
|
|
|
|
|
|
|
|
mailDAO.addToSentMail(updatedModel.getEmailId());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|