Skip to content

Commit

Permalink
Restructure EmailService
Browse files Browse the repository at this point in the history
  • Loading branch information
adilk0728 committed Mar 5, 2024
1 parent 568be0b commit 9623691
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
11 changes: 10 additions & 1 deletion src/main/java/com/ad/markalive/MarkaliveApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.ad.markalive.batch.processor.BookmarkTableRemindColProcessor;
import com.ad.markalive.model.Bookmark;
import com.ad.markalive.repository.mappers.BookmarkRowMapper;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
Expand All @@ -18,6 +20,8 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
Expand All @@ -36,6 +40,7 @@ public MarkaliveApplication(DataSource dataSource, JobRepository jobRepository,
this.dataSource = dataSource;
this.jobRepository = jobRepository;
this.platformTransactionManager = platformTransactionManager;
this.javaMailSender = javaMailSender;
}

public static void main(String[] args) {
Expand All @@ -57,7 +62,6 @@ public Job checkRemindJob(){
return new JobBuilder("checkRemindJob", this.jobRepository)
.start(processBookmarkRemind())
.build();

}

@Bean
Expand Down Expand Up @@ -96,4 +100,9 @@ public JobLauncher jobLauncherConfigured() throws Exception {
jobLauncher.afterPropertiesSet();
return jobLauncher;
}

@Bean
public MimeMessage defaultMimeMessage() throws MessagingException {
return new MimeMessageHelper(javaMailSender.createMimeMessage(), false, "UTF-8").getMimeMessage();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.ad.markalive.service.email;

import com.ad.markalive.model.Bookmark;
import com.ad.markalive.repository.BookmarkRepository;
import com.ad.markalive.service.BookmarkService;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
Expand All @@ -18,34 +18,30 @@
@Component
public class EmailSenderService {
private JavaMailSender mailSender;
private MimeMessage mimeMessage;
private TemplateEngine templateEngine;
private BookmarkRepository bookmarkRepository;
private BookmarkService bookmarkService;

public EmailSenderService(JavaMailSender mailSender, TemplateEngine templateEngine, BookmarkRepository bookmarkRepository) {
public EmailSenderService(JavaMailSender mailSender, TemplateEngine templateEngine, BookmarkService bookmarkService, MimeMessage mimeMessage) {
this.mailSender = mailSender;
this.templateEngine = templateEngine;
this.bookmarkRepository = bookmarkRepository;
this.bookmarkService = bookmarkService;
this.mimeMessage = mimeMessage;
}

public void sendEmail(String toAddress, String fromAddress, String subject) throws MessagingException {
SimpleMailMessage templateMessage = new SimpleMailMessage();
public void sendEmail(String toAddress, String fromAddress, String subject, String templateName) throws MessagingException {
final Context ctx = new Context();
List<Bookmark> bookmarkList = new ArrayList<>();
this.bookmarkRepository.findAll().forEach(bookmarkList::add);
List<Bookmark> bookmarkList = new ArrayList<>(this.bookmarkService.getAllBookmarks());
ctx.setVariable("name", "Adithya");
ctx.setVariable("bookmarks", bookmarkList);
final MimeMessage mimeMessage= this.mailSender.createMimeMessage();
final MimeMessageHelper mimeMessageHelper= new MimeMessageHelper(mimeMessage, false, "UTF-8");
mimeMessageHelper.setFrom(fromAddress);
mimeMessageHelper.setTo(toAddress);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(this.templateEngine.process("bookmark-list.html", ctx), true);
mimeMessage.setFrom(fromAddress);
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
mimeMessage.setSubject(subject);
mimeMessage.setText(this.templateEngine.process(templateName, ctx), "UTF-8", "html");
try{
this.mailSender.send(mimeMessage);
} catch (MailException ex) {
// simply log it and go on...
System.err.println(ex.getMessage());
}

}
}

0 comments on commit 9623691

Please sign in to comment.