@Override public void sendWithAttachment(String to, String subject, String templateName, Map<String, String> data, InputStream attachment, String attachmentName) { LOG.debug("Sending email from {} to {} with subject {}", from, to, subject); MimeMessagePreparator messagePreparator = message -> { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setFrom(from); helper.setSubject(subject); helper.setText(templateService.createTemplate(templateName, data)); helper.addAttachment(attachmentName, new ByteArrayResource(IOUtils.toByteArray(attachment))); }; mailSender.send(messagePreparator); IOUtils.closeQuietly(attachment); }
@Override public void sendEmail(final UserDTO user, String url) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(user.getEmail()); message.setSubject(SUBJECT); message.setFrom(EMAIL_FROM); // could be parameterized... Map model = new HashMap(); model.put("user", user); model.put("url", url); String text = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, "org/enricogiurin/sushibar/registration-confirmation.vm", model); message.setText(text, true); } }; this.emailSender.send(preparator); }
@Override public void sendMailWithTemplate(final TemplateMailTO mailParameters) throws MailException { MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(mailParameters.getToMailAddress()); message.setFrom(mailParameters.getFromMailAddress()); message.setSubject(mailParameters.getSubject()); if (mailParameters.hasCcMailAddress()) { message.setCc(mailParameters.getCcMailAddress()); } if (mailParameters.hasReplyTo()) { message.setReplyTo(mailParameters.getReplyTo()); } String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mailParameters.getTemplateLocation(), mailParameters.getTemplateProperties()); log.debug("*** TEST text='" + text + "'"); message.setText(text, true); message.setValidateAddresses(true); } }; this.mailSender.send(preparator); }
@Override public boolean sendEmail(final List<String> emails, final String subject, final String body) { if (emails == null || emails.isEmpty()) { return false; } asyncExecute(new Runnable() { public void run() { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper msg = new MimeMessageHelper(mimeMessage); msg.setTo(emails.toArray(new String[0])); msg.setSubject(subject); msg.setFrom(Config.SUPPORT_EMAIL); msg.setText(body, true); // body is assumed to be HTML } }; try { mailSender.send(preparator); logger.debug("Email sent to {}, {}", emails, subject); } catch (MailException ex) { logger.error("Failed to send email. {}", ex.getMessage()); } } }); return true; }
@Override public void sendActivation(User user) throws InvalidTokenException { if (user.getActivationToken() == null) { throw new InvalidTokenException("No activation token found for " + user.toString()); } Map<String, Object> model = getBaseModel(user); model.put("url", hostname + "#/activation/" + user.getUsername() + "/" + user.getActivationToken().getToken()); final String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/sendActivation.vm", "UTF-8", model); MimeMessagePreparator preparator = mimeMessage -> { mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); mimeMessage.setFrom(new InternetAddress(mailFromAddress)); mimeMessage.setSubject("Kanbanboard WGM Accountaktivierung"); mimeMessage.setText(body, "UTF-8", "html"); }; try { mailSender.send(preparator); log.info("Activation Mail sent to "+ user.getEmail()); } catch (MailException e) { log.error("Could not send activation mail to " + user.getEmail() + ". The error was :", e); } }
@Override public void sendPasswordReset(User user) throws InvalidTokenException { if (user.getPasswordResetToken() == null) { throw new InvalidTokenException("No password reset token found for " + user.toString()); } final Map<String, Object> model = getBaseModel(user); model.put("url", hostname + "#/reset/" + user.getUsername() + "/" + user.getPasswordResetToken().getToken()); final String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/sendPasswordReset.vm", "UTF-8", model); MimeMessagePreparator preparator = mimeMessage -> { mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); mimeMessage.setFrom(new InternetAddress(mailFromAddress)); mimeMessage.setSubject("Kanbanboard WGM Passwort Reset"); mimeMessage.setText(body, "UTF-8", "html"); }; try { mailSender.send(preparator); log.info("Reset Mail sent to {}", user.getEmail()); } catch (MailException e) { log.error("Could not send mail to " + user.getEmail() + ". The error was :", e); } }
/** * Sends e-mail using Velocity template for the body and the properties * passed in as Velocity variables. * * @param msg The e-mail message to be sent, except for the body. * @param hTemplateVariables Variables to use when processing the template. */ protected void send(SimpleMailMessage msg, String language, String template, Map<String, Object> hTemplateVariables) { LOG.info("Send email ..."); MimeMessagePreparator preparator = (MimeMessage mimeMessage) -> { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8"); message.setTo(msg.getTo()); message.setFrom(msg.getFrom()); message.setSubject(msg.getSubject()); String body = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, "/" + template + "." + language + ".vm", "UTF-8", hTemplateVariables); LOG.log(Level.INFO, "Body: {0}", body); message.setText(body, true); }; mailSender.send(preparator); LOG.log(Level.INFO, "Sender {0}", msg.getFrom()); LOG.log(Level.INFO, "Recipient {0}", msg.getTo()); }
private void senderEmail(final Integer uid, final String username, final String to, final String resetKey, final String resetPwd, final EmailSender email, final MessageTemplate tpl) { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(email.getHost()); sender.setPort(email.getPort()); sender.setUsername(email.getUsername()); sender.setPassword(email.getPassword()); sender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException, UnsupportedEncodingException { MimeMessageHelper msg = new MimeMessageHelper(mimeMessage, false, email.getEncoding()); msg.setSubject(tpl.getForgotPasswordSubject()); msg.setTo(to); msg.setFrom(email.getUsername(), email.getPersonal()); String text = tpl.getForgotPasswordText(); text = StringUtils.replace(text, "${uid}", String.valueOf(uid)); text = StringUtils.replace(text, "${username}", username); text = StringUtils.replace(text, "${resetKey}", resetKey); text = StringUtils.replace(text, "${resetPwd}", resetPwd); msg.setText(text); } }); }
@Override public void send(final SimpleMailMessage msg, final Map<String, Object> hTemplateVariables, final String templateFileName) { MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(msg.getTo()); message.setFrom(msg.getFrom()); message.setSubject(msg.getSubject()); String body = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, templateFileName, hTemplateVariables); //logger.info("body={}", body); message.setText(body, true); } }; mailSender.send(preparator); }
public MimeMessagePreparator mimeMessagePreparator(final EmailData emailData,final ByteArrayOutputStream attachmentAsByteArrResource){ MimeMessagePreparator mailMsg = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper msgHelper = new MimeMessageHelper(mimeMessage, true); msgHelper.setSubject(emailData.getSubject()); msgHelper.setTo(emailData.getTo().toArray(new String[emailData.getTo().size()])); msgHelper.setCc(emailData.getCc().toArray(new String[emailData.getCc().size()])); if(!CollectionUtils.isEmpty(emailData.getBcc())){ msgHelper.setBcc(emailData.getBcc().toArray(new String[emailData.getBcc().size()])); } msgHelper.setText(emailData.getBody(), true); if (attachmentAsByteArrResource != null){ saveHtmlToPdf(emailData.getBody(), attachmentAsByteArrResource); if(attachmentAsByteArrResource.size() > 0){ msgHelper.addAttachment("Ticket.pdf", new ByteArrayResource(attachmentAsByteArrResource.toByteArray())); } } msgHelper.setFrom(emailData.getFrom()); } }; return mailMsg; }
public void sendMailWithTemplate(final TemplateMailTO mailParameters) throws MailException { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(mailParameters.getToMailAddress()); message.setFrom(mailParameters.getFromMailAddress()); message.setSubject(mailParameters.getSubject()); if (mailParameters.hasCcMailAddress()) { message.setCc(mailParameters.getCcMailAddress()); } if (mailParameters.hasReplyTo()) { message.setReplyTo(mailParameters.getReplyTo()); } String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, mailParameters.getTemplateLocation(), mailParameters.getTemplateProperties()); log.debug("*** TEST text='" + text + "'"); message.setText(text, true); message.setValidateAddresses(true); } }; this.mailSender.send(preparator); }
/** * * @param toEmailAddress * @param ccEmailAdress * @param emailSubject * @param emailHtmlBodyText */ public void sendEmail(final String toEmailAddress, final String ccEmailAdress, final String emailSubject, final String emailHtmlBodyText) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(toEmailAddress); message.setCc(ccEmailAdress); message.setSubject(emailSubject); message.setText(emailHtmlBodyText, true); } }; mailSender.send(preparator); }
/** * List of cc required */ public void sendEmail(final String toEmailAddress, final String[] ccEmailAdress, final String emailSubject, final String emailHtmlBodyText) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(toEmailAddress); message.setCc(ccEmailAdress); message.setSubject(emailSubject); message.setText(emailHtmlBodyText, true); } }; mailSender.send(preparator); }
/** * 发送电子邮件到指定收件人. * @param recipient - 收件人的电子邮件地址 * @param subject - 邮件的主题 * @param body - 邮件的内容 */ public void sendMail(final String recipient, final String subject, final String body) { final MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException, UnsupportedEncodingException { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(recipient); message.setFrom(senderMail, senderName); message.setSubject(subject); message.setText(body, true); } }; new Thread(new Runnable() { public void run() { mailSender.send(preparator); LOGGER.info(String.format("An Email{Recipient: %s, Subject: %s} has been sent.", new Object[] {recipient, subject})); } }).start(); }
@Override public void sendTplEmail(final String from, final List<String> to, final List<String> cc, final String subject, final String tpl, final Map<String, Object> model) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(to.toArray(new String[to.size()])); message.setCc(cc.toArray(new String[cc.size()])); message.setFrom(from); message.setSubject(subject); VelocityContext context = new VelocityContext(model); StringWriter w = new StringWriter(); Velocity.evaluate(context, w, "emailString", tpl); message.setText(w.toString(), true); } }; JavaMailSenderImpl javaMailSenderImpl = (JavaMailSenderImpl) mailSender; javaMailSenderImpl.send(preparator); }
@Override public void sendTplLocationEmail(final String from, final List<String> to, final List<String> cc, final String subject, final String tplLocation, final Map<String, Object> model) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setTo(to.toArray(new String[to.size()])); message.setCc(cc.toArray(new String[cc.size()])); message.setFrom(from); message.setSubject(subject); String tpl; tpl = VelocityEngineUtils.mergeTemplateIntoString( velocityEngine, tplLocation, "utf-8", model); message.setText(tpl, true); } }; JavaMailSenderImpl javaMailSenderImpl = (JavaMailSenderImpl) mailSender; javaMailSenderImpl.send(preparator); }
private void send(final SimpleMailMessage msg, final AttachmentPreparator preparator) { if (preparator == null) { mailSender.send(msg); } else { mailSender.send(new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setTo(msg.getTo()); helper.setCc(msg.getCc()); helper.setBcc(msg.getBcc()); helper.setFrom(msg.getFrom()); helper.setSubject(msg.getSubject()); helper.setText(msg.getText()); preparator.prepare(new Attachment(helper)); } }); } }
private void sendText(final String subject, final String nonHtml) { MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setValidateAddresses(true); String[] split = to_commaseparated.split(","); for (String emailAddress : split) { emailAddress = emailAddress.trim(); try { log.trace("adding " + emailAddress); messageHelper.addTo(emailAddress); log.trace("added " + emailAddress); } catch (MessagingException e) { log.error("unable to parse email address from " + emailAddress, e); } } messageHelper.setFrom(from); messageHelper.setSubject(subject); messageHelper.setText(nonHtml, false); } }; javaMailSender.send(preparator); }
private void send(final SimpleMailMessage msg, final AttachmentPreparator preparator) { if (preparator == null) { mailSender.send(msg); } else { mailSender.send(new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(msg.getFrom()); helper.setTo(msg.getTo()); helper.setCc(msg.getCc()); helper.setBcc(msg.getBcc()); helper.setReplyTo(msg.getReplyTo()); helper.setSubject(msg.getSubject()); helper.setText(msg.getText()); preparator.prepare(new Attachment(helper)); } }); } }
private EmailServiceTokenModel sendVerificationEmail(final EmailServiceTokenModel emailVerificationModel, final String emailSubject, final String velocityModel, final Map<String, String> resources) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, MimeMessageHelper.MULTIPART_MODE_RELATED, "UTF-8"); messageHelper.setTo(emailVerificationModel.getEmailAddress()); messageHelper.setFrom(emailFromAddress); messageHelper.setReplyTo(emailReplyToAddress); messageHelper.setSubject(emailSubject); Map model = new HashMap(); model.put("model", emailVerificationModel); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, velocityModel, model); messageHelper.setText(new String(text.getBytes(), "UTF-8"), true); for(String resourceIdentifier: resources.keySet()) { addInlineResource(messageHelper, resources.get(resourceIdentifier), resourceIdentifier); } } }; LOG.debug("Sending {} token to : {}",emailVerificationModel.getTokenType().toString(), emailVerificationModel.getEmailAddress()); this.mailSender.send(preparator); return emailVerificationModel; }
private void senderEmail(final Integer uid, final String username, final String to, final String resetKey, final String resetPwd, final EmailSender email, final MessageTemplate tpl) { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(email.getHost()); sender.setUsername(email.getUsername()); sender.setPassword(email.getPassword()); sender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException, UnsupportedEncodingException { MimeMessageHelper msg = new MimeMessageHelper(mimeMessage, false, email.getEncoding()); msg.setSubject(tpl.getForgotPasswordSubject()); msg.setTo(to); msg.setFrom(email.getUsername(), email.getPersonal()); String text = tpl.getForgotPasswordText(); text = StringUtils.replace(text, "${uid}", String.valueOf(uid)); text = StringUtils.replace(text, "${username}", username); text = StringUtils.replace(text, "${resetKey}", resetKey); text = StringUtils.replace(text, "${resetPwd}", resetPwd); msg.setText(text); } }); }
private void setExpectReplyToField(final String replyToAddress) { Mockito.doAnswer(invocation -> { Class<?> aClass = invocation.getArguments()[0].getClass(); aClass.getDeclaredFields(); Field field = aClass.getDeclaredField("val$replyTo"); field.setAccessible(true); Object value = field.get(invocation.getArguments()[0]); if(!replyToAddress.equals(value)) { fail(); } return null; }).when(mailSender).send(any(MimeMessagePreparator.class)); }
/** * Send error email. * * @param errorMessage the error message */ private void sendErrorEmail(final String errorMessage) { final MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(final MimeMessage mimeMessage) throws Exception { final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8"); message.setTo(EmailServiceImpl.this.environment.getRequiredProperty(ADMINISTRATOR)); message.setFrom(EmailServiceImpl.this.environment.getRequiredProperty(DAILY_EMAIL_ADDRESS)); message.setSubject("Error - Daily News & Weather"); message.setText(errorMessage, true); } }; this.sender.send(preparator); this.logger.info("Error Message sent!"); }
@Override public void send(final MimeMessagePreparator mimeMessagePreparator) throws MailException { final MimeMessage mimeMessage = createMimeMessage(); try { mimeMessagePreparator.prepare(mimeMessage); final String content = (String) mimeMessage.getContent(); final Properties javaMailProperties = getJavaMailProperties(); javaMailProperties.setProperty("mailContent", content); } catch (final Exception e) { throw new MailPreparationException(e); } }
/** * Send an email using the default mail node. If no mail is configured, * nothing happens. */ private void sendMail(final MimeMessagePreparator preparator) { try { final String node = configurationResource.get(MAIL_NODE); Optional.ofNullable(servicePluginLocator.getResource(node, MailServicePlugin.class)) .map(p -> p.send(node, preparator)); } catch (Exception e) { log.error(e.getMessage()); } }
@SuppressWarnings("rawtypes") public void send(String to, String subject, String templatePath, List<File> attachmentList, Map variableMap) { // logging LOG.info("Sending mail from '{}' to address '{}' with subject '{}'", from, to, subject); // really send mail if (send) { // create mime message preparator MimeMessagePreparator preparator = new MimeMessagePreparator() { @SuppressWarnings("unchecked") @Override public void prepare(MimeMessage mimeMessage) throws Exception { // get mail text by substituting variables in mail template with velocity String text = velocityService.evaluate(templatePath, variableMap); // create message helper MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); messageHelper.setFrom(from); messageHelper.setTo(to); messageHelper.setSubject(subject); messageHelper.setText(text, true); // add attachments if available for (File file : attachmentList) { messageHelper.addAttachment(file.getName(), file); } } }; // send HTML mail mailSender.send(preparator); } }
private void sendSimpleMailWithAttachment(final String fromAddress, final List<String> toAddress, final List<String> ccAddress, final String subject, final String mailContent, final List<MultipartFile> attachFiles) { mailSender.send(new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); messageHelper.setFrom(fromAddress); messageHelper.setTo(toAddress.toArray(new String[toAddress.size()])); if (ccAddress != null && ccAddress.size() > 0) { messageHelper.setCc(ccAddress.toArray(new String[ccAddress.size()])); } messageHelper.setSubject(subject); messageHelper.setText(mailContent); for (final MultipartFile attachFile : attachFiles) { // determines if there is an upload file, attach it to the e-mail if (attachFile != null) { String attachName = attachFile.getOriginalFilename(); messageHelper.addAttachment(attachName, new InputStreamSource() { @Override public InputStream getInputStream() throws IOException { return attachFile.getInputStream(); } }); } else { log.info("Attached file is Empty. Skipping the file " + attachFile + " in mail."); } } } }); }
private void sendVelocityTemplateMailWithoutAttachment(final String fromAddress, final List<String> toAddress, final List<String> ccAddress, final String subject, final Map<String, Object> modelForMailContent, final String templateName, final boolean isTemplateHtml) throws Exception { MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject(subject); helper.setFrom(fromAddress); helper.setTo(toAddress.toArray(new String[toAddress.size()])); if (ccAddress != null && ccAddress.size() > 0) { helper.setCc(ccAddress.toArray(new String[ccAddress.size()])); } String text = geVelocityTemplateContent(modelForMailContent, templateName); log.debug("Template Name :" + templateName + "Template content : " + text); // use the true flag to indicate you need a multipart message helper.setText(text, isTemplateHtml); } }; try { mailSender.send(preparator); log.debug("Template Mail without attachment successfully sent !! \nFrom Addres: " + fromAddress + " To Address: " + toAddress + " CC Address: " + ccAddress + "\n Subject: " + subject); log.debug(subject); } catch (MailException ex) { log.error("Error Sending Velocity Template mail: " + ex.getMessage()); log.error("Sending mail failed for \nFrom Addres: " + fromAddress + " To Address: " + toAddress + " CC Address: " + ccAddress + "\n Subject: " + subject + " Template Name: " + templateName + " isTemplateHtml: " + isTemplateHtml); throw new Exception("Error Sending Velocity Template mail. " + ex.getMessage(), ex); } }
@Override public void sendMimeMailWithoutAttachment(final String fromAddress, final List<String> toAddress, final List<String> ccAddress, final String subject, final String mailContent, final boolean isMailHtml) throws Exception { MimeMessagePreparator preparator = new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject(subject); helper.setFrom(fromAddress); helper.setTo(toAddress.toArray(new String[toAddress.size()])); if (ccAddress != null && ccAddress.size() > 0) { helper.setCc(ccAddress.toArray(new String[ccAddress.size()])); } // use the true flag to indicate you need a multipart message helper.setText(mailContent, isMailHtml); } }; try { mailSender.send(preparator); log.debug("EMail without attachment successfully sent !! \nFrom Addres: " + fromAddress + " To Address: " + toAddress + " CC Address: " + ccAddress + "\n Subject: " + subject); log.debug(subject); } catch (MailException ex) { log.error("Error Sending email: " + ex.getMessage()); log.error("Sending mail failed for \nFrom Addres: " + fromAddress + " To Address: " + toAddress + " CC Address: " + ccAddress + "\n Subject: " + subject + " Content: " + mailContent + " isTemplateHtml: " + isMailHtml); throw new Exception("Error Sending Velocity Template mail. " + ex.getMessage(), ex); } }