@Override public void sendMail(String emailRecipient, String message) { try { /*Should use subservice to get administrative resource resolver instead of this code*/ ResourceResolver resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null); Resource templateResource = resourceResolver.getResource(RabbitMQUtil.EMAIL_TEMPLATE_PATH); if (templateResource.getChild("file") != null) { templateResource = templateResource.getChild("file"); } ArrayList<InternetAddress> emailRecipients = new ArrayList<InternetAddress>(); final MailTemplate mailTemplate = MailTemplate.create( templateResource.getPath(), templateResource.getResourceResolver().adaptTo(Session.class)); HtmlEmail email = new HtmlEmail(); Map<String, String> mailTokens = new HashMap<String, String>(); mailTokens.put("message", message); mailTokens.put("subject", "Dummy Subject"); mailTokens.put("email", emailRecipient); if (mailTemplate != null) { emailRecipients.add(new InternetAddress(emailRecipient)); email.setTo(emailRecipients); email.setSubject("Dummy Mail"); email.setTextMsg(message); messageGateway = messageGatewayService.getGateway(HtmlEmail.class); messageGateway.send(email); } } catch (Exception e) { /*Put message in queue again in case of exception.. Based on type of exception, it can be decided whether it has to be put in queue again or not*/ RabbitMQUtil.addMessageToQueue(scrService); e.printStackTrace(System.out); } }
static HtmlEmail removeRecipient(HtmlEmail email, String emailToRemove) throws EmailException { List<String> to = Utils.noGenericTypeToStringType(email.getToAddresses()), cc = Utils.noGenericTypeToStringType(email.getCcAddresses()), bcc = Utils.noGenericTypeToStringType(email.getBccAddresses()); if (to.contains(emailToRemove)) { to.remove(emailToRemove); email.setTo(to); } else if (cc.contains(emailToRemove)) { cc.remove(emailToRemove); email.setCc(cc); } else if (bcc.contains(emailToRemove)) { bcc.remove(emailToRemove); email.setBcc(bcc); } return email; }
@Signature public PHtmlEmail setTo(List<InternetAddress> addresses) throws AddressException, EmailException { htmlEmail.setTo(addresses); return this; }