Guest User

Untitled

a guest
Jan 11th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. @EnableScheduling
  2. @Controller
  3. public class MailController {
  4. @Autowired
  5. LicenseRepository licenseRepository;
  6.  
  7. @Autowired
  8. InsuranceRepository insuranceRepository;
  9.  
  10. @Autowired
  11. EmailService emailService;
  12.  
  13. @Scheduled(cron = "0 15 10 15 * ?")
  14. public void sendReminder(){
  15. License license = new License();
  16. Insurance insurance = new Insurance();
  17. Mail mail = new Mail();
  18. mail.setFrom("no-reply@gmail.com");
  19. mail.setTo(new String[]{"myemail@gmail.com"});
  20. mail.setSubject("Policy Renewal Notice");
  21.  
  22. Map<String, Object> mailModel = new HashMap<String, Object>();
  23. mail.setModel(mailModel);
  24.  
  25. try {
  26. emailService.sendSimpleMessage(mail, license, insurance);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29.  
  30. }
  31.  
  32. }
  33.  
  34. @RequestMapping(value="/email")
  35. public String email(){
  36. return "emailMessage";
  37. }
  38. }
  39.  
  40. import java.util.List;
  41. import java.util.Map;
  42.  
  43. public class Mail {
  44.  
  45. private String from;
  46. private String[] to;
  47. private String subject;
  48. private List<Object> attachments;
  49. private Map<String, Object> model;
  50.  
  51. public Mail() {
  52.  
  53. }
  54.  
  55. public String getFrom() {
  56. return from;
  57. }
  58.  
  59. public void setFrom(String from) {
  60. this.from = from;
  61. }
  62.  
  63. public String[] getTo() {
  64. return to;
  65. }
  66.  
  67. public void setTo(String[] to) {
  68. this.to = to;
  69. }
  70.  
  71. public String getSubject() {
  72. return subject;
  73. }
  74.  
  75. public void setSubject(String subject) {
  76. this.subject = subject;
  77. }
  78.  
  79. public List<Object> getAttachments() {
  80. return attachments;
  81. }
  82.  
  83. public void setAttachments(List<Object> attachments) {
  84. this.attachments = attachments;
  85. }
  86.  
  87.  
  88. public Map<String, Object> getModel() {
  89. return model;
  90. }
  91.  
  92. public void setModel(Map<String, Object> model) {
  93. this.model = model;
  94. }
  95.  
  96. }
  97.  
  98. @Service
  99. public class EmailService{
  100.  
  101. private JavaMailSender javaMailSender;
  102.  
  103. @Autowired
  104. public EmailService(JavaMailSender javaMailSender){
  105. this.javaMailSender = javaMailSender;
  106. }
  107.  
  108. @Autowired
  109. private SpringTemplateEngine templateEngine;
  110.  
  111. public void sendSimpleMessage(Mail mail, License license, Insurance insurance) throws MessagingException, IOException {
  112. MimeMessage message = javaMailSender.createMimeMessage();
  113. MimeMessageHelper helper = new MimeMessageHelper(message,
  114. MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
  115. StandardCharsets.UTF_8.name());
  116.  
  117. helper.addAttachment("Mail_Icon.png", new ClassPathResource("static/images/Mail_Icon.png"));
  118.  
  119. Context context = new Context();
  120. context.setVariables(mail.getModel());
  121. context.setVariable("license",license);
  122. context.setVariable("insurance",insurance);
  123. String html = templateEngine.process("emailMessage", context);
  124.  
  125. helper.setTo(mail.getTo());
  126. helper.setText(html, true);
  127. helper.setSubject(mail.getSubject());
  128. helper.setFrom(mail.getFrom());
  129.  
  130. javaMailSender.send(message);
  131. }
  132.  
  133.  
  134.  
  135. }
  136.  
  137. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  138. <html xmlns:th="http://www.w3.org/1999/xhtml">
  139.  
  140. <head>
  141. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  142. <title>HTML Reminder Email</title>
  143. <style type="text/css">
  144. lots of styling here removed for easier reading
  145. </style>
  146. </head>
  147.  
  148. <body bgcolor="#f6f8f1">
  149. <table width="100%" bgcolor="#f6f8f1" border="0" cellpadding="0" cellspacing="0">
  150. <tr>
  151. <td>
  152. <!--[if (gte mso 9)|(IE)]>
  153. <table width="600" align="center" cellpadding="0" cellspacing="0" border="0">
  154. <tr>
  155. <td>
  156. <![endif]-->
  157. <table bgcolor="#ffffff" class="content" align="center" cellpadding="0" cellspacing="0" border="0">
  158. <tr>
  159. <td bgcolor="#6435c9" class="header">
  160. <table width="70" align="left" border="0" cellpadding="0" cellspacing="0">
  161. <tr>
  162. <td height="70" style="padding: 0 20px 20px 0;">
  163. <img class="fix" src="cid:Mail_Icon.png" width="70" height="70" border="0" alt="" />
  164. </td>
  165. </tr>
  166. </table>
  167. <!--[if (gte mso 9)|(IE)]>
  168. <table width="425" align="left" cellpadding="0" cellspacing="0" border="0">
  169. <tr>
  170. <td>
  171. <![endif]-->
  172. <table class="col425" align="left" border="0" cellpadding="0" cellspacing="0" style="width: 100%; max-width: 425px;">
  173. <tr>
  174. <td height="70">
  175. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  176. <tr>
  177. <td class="subhead" style="padding: 0">
  178. Policy Renewal Expiration Reminder
  179. </td>
  180. </tr>
  181. </table>
  182. </td>
  183. </tr>
  184. </table>
  185. <!--[if (gte mso 9)|(IE)]>
  186. </td>
  187. </tr>
  188. </table>
  189. <![endif]-->
  190. </td>
  191. </tr>
  192. <tr>
  193. <td class="innerpadding borderbottom">
  194. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  195. <tr class="emailRow">
  196. <p>AFFILIATE NAME HERE</p>
  197. </tr>
  198. <tr class="emailRow">
  199. <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>
  200. <p>Thank you,</p>
  201. </tr>
  202. </table>
  203. </td>
  204. </tr>
  205. </table>
  206. </td>
  207. </tr>
  208. </table>
  209. </body>
  210. </html>
Add Comment
Please, Sign In to add comment