Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @EnableScheduling
- @Controller
- public class MailController {
- @Autowired
- LicenseRepository licenseRepository;
- @Autowired
- InsuranceRepository insuranceRepository;
- @Autowired
- EmailService emailService;
- @Scheduled(cron = "0 15 10 15 * ?")
- public void sendReminder(){
- License license = new License();
- Insurance insurance = new Insurance();
- Mail mail = new Mail();
- mail.setFrom("no-reply@gmail.com");
- mail.setTo(new String[]{"myemail@gmail.com"});
- mail.setSubject("Policy Renewal Notice");
- Map<String, Object> mailModel = new HashMap<String, Object>();
- mail.setModel(mailModel);
- try {
- emailService.sendSimpleMessage(mail, license, insurance);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @RequestMapping(value="/email")
- public String email(){
- return "emailMessage";
- }
- }
- import java.util.List;
- import java.util.Map;
- public class Mail {
- private String from;
- private String[] to;
- private String subject;
- private List<Object> attachments;
- private Map<String, Object> model;
- public Mail() {
- }
- public String getFrom() {
- return from;
- }
- public void setFrom(String from) {
- this.from = from;
- }
- public String[] getTo() {
- return to;
- }
- public void setTo(String[] to) {
- this.to = to;
- }
- public String getSubject() {
- return subject;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public List<Object> getAttachments() {
- return attachments;
- }
- public void setAttachments(List<Object> attachments) {
- this.attachments = attachments;
- }
- public Map<String, Object> getModel() {
- return model;
- }
- public void setModel(Map<String, Object> model) {
- this.model = model;
- }
- }
- @Service
- public class EmailService{
- private JavaMailSender javaMailSender;
- @Autowired
- public EmailService(JavaMailSender javaMailSender){
- this.javaMailSender = javaMailSender;
- }
- @Autowired
- private SpringTemplateEngine templateEngine;
- public void sendSimpleMessage(Mail mail, License license, Insurance insurance) throws MessagingException, IOException {
- MimeMessage message = javaMailSender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(message,
- MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
- StandardCharsets.UTF_8.name());
- helper.addAttachment("Mail_Icon.png", new ClassPathResource("static/images/Mail_Icon.png"));
- Context context = new Context();
- context.setVariables(mail.getModel());
- context.setVariable("license",license);
- context.setVariable("insurance",insurance);
- String html = templateEngine.process("emailMessage", context);
- helper.setTo(mail.getTo());
- helper.setText(html, true);
- helper.setSubject(mail.getSubject());
- helper.setFrom(mail.getFrom());
- javaMailSender.send(message);
- }
- }
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns:th="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>HTML Reminder Email</title>
- <style type="text/css">
- lots of styling here removed for easier reading
- </style>
- </head>
- <body bgcolor="#f6f8f1">
- <table width="100%" bgcolor="#f6f8f1" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td>
- <!--[if (gte mso 9)|(IE)]>
- <table width="600" align="center" cellpadding="0" cellspacing="0" border="0">
- <tr>
- <td>
- <![endif]-->
- <table bgcolor="#ffffff" class="content" align="center" cellpadding="0" cellspacing="0" border="0">
- <tr>
- <td bgcolor="#6435c9" class="header">
- <table width="70" align="left" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td height="70" style="padding: 0 20px 20px 0;">
- <img class="fix" src="cid:Mail_Icon.png" width="70" height="70" border="0" alt="" />
- </td>
- </tr>
- </table>
- <!--[if (gte mso 9)|(IE)]>
- <table width="425" align="left" cellpadding="0" cellspacing="0" border="0">
- <tr>
- <td>
- <![endif]-->
- <table class="col425" align="left" border="0" cellpadding="0" cellspacing="0" style="width: 100%; max-width: 425px;">
- <tr>
- <td height="70">
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td class="subhead" style="padding: 0">
- Policy Renewal Expiration Reminder
- </td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- <!--[if (gte mso 9)|(IE)]>
- </td>
- </tr>
- </table>
- <![endif]-->
- </td>
- </tr>
- <tr>
- <td class="innerpadding borderbottom">
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr class="emailRow">
- <p>AFFILIATE NAME HERE</p>
- </tr>
- <tr class="emailRow">
- <p>Our Records indicate that your policy is up for renewal in 30 days. Please provide the updated proof of insurance to the Director of Operations by email: <a href="mailto:myemail@gmail.com">myemail@gmail.com</a>. We appreciate your compliance. </p>
- <p>Thank you,</p>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </td>
- </tr>
- </table>
- </body>
- </html>
Add Comment
Please, Sign In to add comment