diff --git a/src/main/java/entgra/mailsender/Config/AppConfig.java b/src/main/java/entgra/mailsender/Config/AppConfig.java index ec85b1f..c411609 100644 --- a/src/main/java/entgra/mailsender/Config/AppConfig.java +++ b/src/main/java/entgra/mailsender/Config/AppConfig.java @@ -1,15 +1,12 @@ package entgra.mailsender.Config; -import entgra.mailsender.DAO.MailDAO; -import entgra.mailsender.DTO.MailModel; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; - import java.util.Properties; @Configuration diff --git a/src/main/java/entgra/mailsender/Controller/MailController.java b/src/main/java/entgra/mailsender/Controller/MailController.java index fb2248c..790ef4d 100644 --- a/src/main/java/entgra/mailsender/Controller/MailController.java +++ b/src/main/java/entgra/mailsender/Controller/MailController.java @@ -22,15 +22,16 @@ public class MailController { @PostMapping public ResponseEntity sendEmail(@ModelAttribute MailModel emailModel) { + String msg; try { mailService.sendEmail(emailModel); - String msg = "Email sent successfully"; + msg = "Email sent successfully"; return ResponseEntity.ok(msg); } catch (RuntimeException e) { - String msg = "Invalid email address 12112121211"; + msg = "Invalid email address"; return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(msg); } catch (MessagingException | IOException exception) { - String msg = "Failed to send email"; + msg = "Failed to send email"; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(msg + exception); } } diff --git a/src/main/java/entgra/mailsender/DAO/MailDAOImpl.java b/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java similarity index 81% rename from src/main/java/entgra/mailsender/DAO/MailDAOImpl.java rename to src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java index ffd6b4e..68ad99b 100644 --- a/src/main/java/entgra/mailsender/DAO/MailDAOImpl.java +++ b/src/main/java/entgra/mailsender/DAO/Impl/MailDAOImpl.java @@ -1,7 +1,8 @@ -package entgra.mailsender.DAO; +package entgra.mailsender.DAO.Impl; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DTO.MailModel; import org.springframework.stereotype.Service; @@ -9,15 +10,24 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.ResultSet; +import java.sql.Blob; +import java.sql.Date; + import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import java.util.function.Supplier; import java.util.logging.Logger; @Service -public class MailDAOImpl implements MailDAO{ +public class MailDAOImpl implements MailDAO { Logger logger = Logger.getLogger(String.valueOf(MailDAOImpl.class)); @@ -54,41 +64,7 @@ public void addMail(MailModel mailModel){ } } -//@Override -//public List insertInPriorityQueue(){ -// logger.info("came into the function"); -// -// -// try{ -// Connection conn = this.getConnection(); -// -// try (PreparedStatement stmt = conn.prepareStatement(sql); -// ResultSet rs = stmt.executeQuery()) { -// while (rs.next()) { -// int priority = rs.getInt("priority"); -// int mail_id = rs.getInt("email_id"); -// priorityQueue.add(new MailModel(mail_id,null,null,priority,null,null,null,null,null,0)); -// } -// -//// MailModel email = priorityQueue.poll(); -// //assert email != null; -// for (Object element: priorityQueue){ -// System.out.println("element" + element); -// } -// return priorityQueue; -// -// } catch (SQLException e) { -// logger.info("Error processing result set: " + e.getMessage()); -// throw new RuntimeException(e); -// } -// -// -// } catch (Exception e) { -// logger.info("Error in preparing statement" + e); -// throw new RuntimeException(e); -// } -// -//} + @Override public List getUnsentMessages(){ @@ -137,7 +113,6 @@ public MailModel getMailDetails(Integer mail_id){ String filename = rs.getString("filename"); mailModel.setFilename(filename); -// logger.info(filename); Blob blob = rs.getBlob("attachment"); logger.info(blob.toString()); diff --git a/src/main/java/entgra/mailsender/DAO/MailDAO.java b/src/main/java/entgra/mailsender/DAO/MailDAO.java index c51881f..d77b2f0 100644 --- a/src/main/java/entgra/mailsender/DAO/MailDAO.java +++ b/src/main/java/entgra/mailsender/DAO/MailDAO.java @@ -3,14 +3,11 @@ package entgra.mailsender.DAO; import entgra.mailsender.DTO.MailModel; import java.util.List; -import java.util.PriorityQueue; public interface MailDAO { void addMail(MailModel mailModel); - // List insertInPriorityQueue(); - MailModel getMailDetails(Integer mail_id); void addToSentMail(Integer mail_id); diff --git a/src/main/java/entgra/mailsender/DTO/MailModel.java b/src/main/java/entgra/mailsender/DTO/MailModel.java index 7d9f3f4..a717d84 100644 --- a/src/main/java/entgra/mailsender/DTO/MailModel.java +++ b/src/main/java/entgra/mailsender/DTO/MailModel.java @@ -7,7 +7,6 @@ import lombok.Setter; import org.springframework.web.multipart.MultipartFile; import java.io.File; -import java.util.Date; import java.util.List; @Getter diff --git a/src/main/java/entgra/mailsender/MailsenderApplication.java b/src/main/java/entgra/mailsender/MailsenderApplication.java index bbb9179..482cdcb 100644 --- a/src/main/java/entgra/mailsender/MailsenderApplication.java +++ b/src/main/java/entgra/mailsender/MailsenderApplication.java @@ -1,7 +1,6 @@ package entgra.mailsender; import entgra.mailsender.Service.MailService; -import entgra.mailsender.Service.MailServiceImpl; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; diff --git a/src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java b/src/main/java/entgra/mailsender/Service/Impl/MailQueueServiceImpl.java similarity index 86% rename from src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java rename to src/main/java/entgra/mailsender/Service/Impl/MailQueueServiceImpl.java index d396f82..844bddb 100644 --- a/src/main/java/entgra/mailsender/Service/MailQueueServiceImpl.java +++ b/src/main/java/entgra/mailsender/Service/Impl/MailQueueServiceImpl.java @@ -1,7 +1,8 @@ -package entgra.mailsender.Service; +package entgra.mailsender.Service.Impl; import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DTO.MailModel; +import entgra.mailsender.Service.MailQueueService; import entgra.mailsender.util.PriorityQueueHolder; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; @@ -15,7 +16,7 @@ import java.util.logging.Logger; @Service @RequiredArgsConstructor -public class MailQueueServiceImpl implements MailQueueService{ +public class MailQueueServiceImpl implements MailQueueService { Logger logger = Logger.getLogger(String.valueOf(MailQueueServiceImpl.class)); @Autowired @@ -23,7 +24,6 @@ public class MailQueueServiceImpl implements MailQueueService{ private volatile Thread processingThread; private volatile boolean isProcessingMails; -// private final MailDAO mailDAO; @Override public void enqueMails(List mailModelList){ @@ -38,11 +38,7 @@ public class MailQueueServiceImpl implements MailQueueService{ uniqueMails.add(mailModel); } } -// assert priorityQueue.peek() != null; -// logger.info(String.valueOf(priorityQueue.peek().getEmailId())); -// Integer id = priorityQueue.peek().getEmailId(); -// logger.info(mailDAO.getMailDetails(id).getEmailAddress()); -// logger.info((Supplier) uniqueMails); + } public MailModel getHighPriorityMail(){ diff --git a/src/main/java/entgra/mailsender/Service/MailServiceImpl.java b/src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java similarity index 80% rename from src/main/java/entgra/mailsender/Service/MailServiceImpl.java rename to src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java index d0b45af..0ddbe6e 100644 --- a/src/main/java/entgra/mailsender/Service/MailServiceImpl.java +++ b/src/main/java/entgra/mailsender/Service/Impl/MailServiceImpl.java @@ -1,7 +1,9 @@ -package entgra.mailsender.Service; +package entgra.mailsender.Service.Impl; import entgra.mailsender.DAO.MailDAO; import entgra.mailsender.DTO.MailModel; +import entgra.mailsender.Service.MailQueueService; +import entgra.mailsender.Service.MailService; import entgra.mailsender.util.PriorityQueueHolder; import jakarta.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; @@ -21,8 +23,6 @@ public class MailServiceImpl implements MailService { @Autowired private MailQueueService mailQueueService; -// private final mailQueueService mailQueueService; - @PreDestroy public void shutdown() { @@ -42,29 +42,14 @@ public class MailServiceImpl implements MailService { logger.warning("email address : "+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 - syncMailWithDB(); - - - //here we need to check the unsent mail details - - - //PriorityQueue 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()); - - - + syncMailWithDB(); //here we need to check the unsent mail details } + 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); @@ -83,7 +68,8 @@ public class MailServiceImpl implements MailService { while(PriorityQueueHolder.getInstance().getPriorityQueue().peek() != null) { MailModel prioritizedMail = mailQueueService.getHighPriorityMail(); MailModel updatedModel = mailDAO.getMailDetails(prioritizedMail.getEmailId()); - logger.info("updated mail" + updatedModel.getEmailAddress()); + logger.info("High priority mail : " + updatedModel.getEmailAddress()); + javaMailSender.send(mimeMessage -> { 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()); } + // Append text to email body mimeMessageHelper.setText(emailBody.toString(), false); - logger.info(updatedModel.getFilename()); + // Append attachments to email body mimeMessageHelper.addAttachment(updatedModel.getFilename(), updatedModel.getFile()); + logger.info("Mail sent successfully"); + //Store the sent mail to the table mailDAO.addToSentMail(updatedModel.getEmailId()); diff --git a/src/main/java/entgra/mailsender/Service/MailService.java b/src/main/java/entgra/mailsender/Service/MailService.java index 032f14e..7d6e87a 100644 --- a/src/main/java/entgra/mailsender/Service/MailService.java +++ b/src/main/java/entgra/mailsender/Service/MailService.java @@ -1,15 +1,14 @@ package entgra.mailsender.Service; import entgra.mailsender.DTO.MailModel; -import org.springframework.web.multipart.MultipartFile; import javax.mail.MessagingException; import java.io.IOException; -import java.sql.Blob; public interface MailService { void sendEmail(MailModel emailModel) throws MessagingException, IOException; + boolean isValidEmailAddress(String email); void syncMailWithDB();