List of usage examples for org.springframework.mail.javamail MimeMessageHelper setFrom
public void setFrom(String from) throws MessagingException
From source file:org.kuali.kra.service.impl.KcEmailServiceImpl.java
public void sendEmailWithAttachments(String from, Set<String> toAddresses, String subject, Set<String> ccAddresses, Set<String> bccAddresses, String body, boolean htmlMessage, List<EmailAttachment> attachments) { JavaMailSender sender = createSender(); if (sender != null) { MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = null; try {//from w ww .j a v a 2s . co m helper = new MimeMessageHelper(message, true, DEFAULT_ENCODING); helper.setFrom(from); if (CollectionUtils.isNotEmpty(toAddresses)) { for (String toAddress : toAddresses) { helper.addTo(toAddress); } } if (StringUtils.isNotBlank(subject)) { helper.setSubject(subject); } else { LOG.warn("Sending message with empty subject."); } helper.setText(body, htmlMessage); if (CollectionUtils.isNotEmpty(ccAddresses)) { for (String ccAddress : ccAddresses) { helper.addCc(ccAddress); } } if (CollectionUtils.isNotEmpty(bccAddresses)) { for (String bccAddress : bccAddresses) { helper.addBcc(bccAddress); } } if (CollectionUtils.isNotEmpty(attachments)) { for (EmailAttachment attachment : attachments) { helper.addAttachment(attachment.getFileName(), new ByteArrayResource(attachment.getContents()), attachment.getMimeType()); } } sender.send(message); } catch (MessagingException ex) { LOG.error("Failed to create mime message helper.", ex); } catch (Exception e) { LOG.error("Failed to send email.", e); } } else { LOG.info( "Failed to send email due to inability to obtain valid email sender, please check your configuration."); } }
From source file:org.mifosplatform.infrastructure.reportmailingjob.service.ReportMailingJobEmailServiceImpl.java
@Override public void sendEmailWithAttachment(ReportMailingJobEmailData reportMailingJobEmailData) { try {/*ww w . j av a2 s. c om*/ // get all ReportMailingJobConfiguration objects from the database this.reportMailingJobConfigurationDataCollection = this.reportMailingJobConfigurationReadPlatformService .retrieveAllReportMailingJobConfigurations(); JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl(); javaMailSenderImpl.setHost(this.getReportSmtpServer()); javaMailSenderImpl.setPort(this.getRerportSmtpPort()); javaMailSenderImpl.setUsername(this.getReportSmtpUsername()); javaMailSenderImpl.setPassword(this.getReportSmtpPassword()); javaMailSenderImpl.setJavaMailProperties(this.getJavaMailProperties()); MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage(); // use the true flag to indicate you need a multipart message MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setTo(reportMailingJobEmailData.getTo()); mimeMessageHelper.setFrom(this.getReportSmtpFromAddress()); mimeMessageHelper.setText(reportMailingJobEmailData.getText()); mimeMessageHelper.setSubject(reportMailingJobEmailData.getSubject()); if (reportMailingJobEmailData.getAttachment() != null) { mimeMessageHelper.addAttachment(reportMailingJobEmailData.getAttachment().getName(), reportMailingJobEmailData.getAttachment()); } javaMailSenderImpl.send(mimeMessage); } catch (MessagingException e) { // handle the exception e.printStackTrace(); } }
From source file:org.openremote.modeler.service.impl.UserServiceImpl.java
/** * {@inheritDoc}//from w w w. j a va 2s . c om */ public boolean sendRegisterActivationEmail(final User user) { if (user == null || user.getOid() == 0 || StringUtils.isEmpty(user.getEmail()) || StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword())) { return false; } MimeMessagePreparator preparator = new MimeMessagePreparator() { @SuppressWarnings("unchecked") public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); message.setSubject("OpenRemote Boss 2.0 Account Registration Confirmation"); message.setTo(user.getEmail()); message.setFrom(mailSender.getUsername()); Map model = new HashMap(); model.put("user", user); String rpwd = user.getRawPassword(); StringBuffer maskPwd = new StringBuffer(); maskPwd.append(rpwd.substring(0, 1)); for (int i = 0; i < rpwd.length() - 2; i++) { maskPwd.append("*"); } maskPwd.append(rpwd.substring(rpwd.length() - 1)); model.put("maskPassword", maskPwd.toString()); model.put("webapp", configuration.getWebappServerRoot()); model.put("aid", new Md5PasswordEncoder().encodePassword(user.getUsername(), user.getPassword())); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, Constants.REGISTRATION_ACTIVATION_EMAIL_VM_NAME, "UTF-8", model); message.setText(text, true); } }; try { this.mailSender.send(preparator); log.info("Sent 'Modeler Account Registration Confirmation' email to " + user.getEmail()); return true; } catch (MailException e) { log.error("Can't send 'Modeler Account Registration Confirmation' email", e); return false; } }
From source file:org.openremote.modeler.service.impl.UserServiceImpl.java
public boolean sendInvitation(final User invitee, final User currentUser) { if (invitee == null || invitee.getOid() == 0 || StringUtils.isEmpty(invitee.getEmail())) { return false; }//from w w w .j a v a 2 s. c o m MimeMessagePreparator preparator = new MimeMessagePreparator() { @SuppressWarnings("unchecked") public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); message.setSubject("Invitation to Share an OpenRemote Boss 2.0 Account"); message.setTo(invitee.getEmail()); message.setFrom(mailSender.getUsername()); Map model = new HashMap(); model.put("uid", invitee.getOid()); model.put("role", invitee.getRole()); model.put("cid", currentUser.getOid()); model.put("host", currentUser.getEmail()); model.put("webapp", configuration.getWebappServerRoot()); model.put("aid", new Md5PasswordEncoder().encodePassword(invitee.getEmail(), currentUser.getPassword())); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, Constants.REGISTRATION_INVITATION_EMAIL_VM_NAME, "UTF-8", model); message.setText(text, true); } }; try { this.mailSender.send(preparator); log.info("Sent 'Modeler Account Invitation' email to " + invitee.getEmail()); return true; } catch (MailException e) { log.error("Can't send 'Modeler Account Invitation' email", e); return false; } }
From source file:org.openremote.modeler.service.impl.UserServiceImpl.java
public User forgetPassword(String username) { final User user = genericDAO.getByNonIdField(User.class, "username", username); final String passwordToken = UUID.randomUUID().toString(); MimeMessagePreparator preparator = new MimeMessagePreparator() { @SuppressWarnings("unchecked") public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); message.setSubject("OpenRemote Password Assistance"); message.setTo(user.getEmail()); message.setFrom(mailSender.getUsername()); Map model = new HashMap(); model.put("webapp", configuration.getWebappServerRoot()); model.put("username", user.getUsername()); model.put("uid", user.getOid()); model.put("aid", passwordToken); String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, Constants.FORGET_PASSWORD_EMAIL_VM_NAME, "UTF-8", model); message.setText(text, true); }/* www .j a v a 2 s . c om*/ }; try { this.mailSender.send(preparator); log.info("Sent 'Reset password' email to " + user.getEmail()); user.setToken(passwordToken); updateUser(user); return user; } catch (MailException e) { log.error("Can't send 'Reset password' email", e); return null; } }
From source file:org.openvpms.web.component.error.ErrorReporter.java
/** * Reports an error.//from w ww .java2s.com * * @param report the error report * @param replyTo the reply-to email address. May be {@code null} */ public void report(final ErrorReport report, String replyTo) { try { JavaMailSender sender = ServiceHelper.getMailSender(); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); String subject = report.getVersion() + ": " + report.getMessage(); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); if (!StringUtils.isEmpty(replyTo)) { helper.setReplyTo(replyTo); } String text = getText(report); if (text != null) { helper.setText(text); } InputStreamSource source = new InputStreamSource() { public InputStream getInputStream() { return new ByteArrayInputStream(report.toXML().getBytes()); } }; helper.addAttachment("error-report.xml", source, DocFormats.XML_TYPE); sender.send(message); } catch (Throwable exception) { log.error(exception, exception); ErrorDialog.show(Messages.get("errorreportdialog.senderror")); } }
From source file:org.openvpms.web.component.mail.AbstractMailer.java
/** * Populates the mail message.// w w w . j a v a 2 s . c om * * @param helper the message helper * @throws MessagingException for any messaging error * @throws UnsupportedEncodingException if the character encoding is not supported */ protected void populateMessage(MimeMessageHelper helper) throws MessagingException, UnsupportedEncodingException { helper.setFrom(getFrom()); String[] to = getTo(); if (to != null && to.length != 0) { helper.setTo(to); } String[] cc = getCc(); if (cc != null && cc.length != 0) { helper.setCc(cc); } String[] bcc = getBcc(); if (bcc != null && bcc.length != 0) { helper.setBcc(bcc); } helper.setSubject(getSubject()); if (body != null) { helper.setText(body); } else { helper.setText(""); } for (Document attachment : attachments) { addAttachment(helper, attachment); } }
From source file:org.osaf.cosmo.scheduler.EmailNotifier.java
protected void sendEmail(final String[] emailAddresses, final String subject, final String text, final boolean isHtml) { if (log.isDebugEnabled()) { log.debug("sending email to:"); for (String s : emailAddresses) log.debug(s);// w w w. j a va 2 s . co m log.debug(subject); log.debug(text); } for (final String address : emailAddresses) { try { mailSender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException { String fromAddr = properties.get(PROPERTY_FROM_ADDRESS); String fromHandle = properties.get(PROPERTY_FROM_HANDLE); MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom("\"" + fromHandle + "\" <" + fromAddr + ">"); message.setTo(address.trim()); message.setSubject(subject); message.setText(text, isHtml); } }); } catch (MailAuthenticationException e) { log.error(e.getMessage()); } catch (MailException e) { log.info("failed to send email to " + address + ": " + e.getMessage()); } } }
From source file:org.osaf.cosmo.service.account.EmailAccountActivator.java
public void sendActivationMessage(final User user, final ActivationContext activationContext) { mailSender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException { Locale locale = activationContext.getLocale(); User sender = activationContext.getSender(); String fromAddr = sender.getEmail(); String fromHandle = messageSource.getMessage(MSG_ACTIVATION_HANDLE, new Object[] {}, locale); String subject = messageSource.getMessage(MSG_ACTIVATION_SUBJECT, new Object[] { user.getUsername() }, locale); String text = messageSource.getMessage(MSG_ACTIVATION_TEXT, new Object[] { user.getUsername(), activationContext.getHostname(), activationContext.getActivationLink(user.getActivationId()) }, locale);//from w w w.ja v a 2 s .co m MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom("\"" + fromHandle + "\" <" + fromAddr + ">"); message.setTo(user.getEmail()); message.setSubject(subject); message.setText(text); } }); log.info("Account activation link sent to " + user.getEmail()); }
From source file:org.osaf.cosmo.service.account.EmailPasswordRecoverer.java
public void sendRecovery(final PasswordRecovery passwordRecovery, final PasswordRecoveryMessageContext context) { // Create final variable so it is available in message preparator below final User user = passwordRecovery.getUser(); mailSender.send(new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws MessagingException { Locale locale = context.getLocale(); User sender = context.getSender(); String fromAddr = sender.getEmail(); String fromHandle = messageSource.getMessage(MSG_PASSWORD_RECOVERY_HANDLE, new Object[] {}, locale); String subject = messageSource.getMessage(MSG_PASSWORD_RECOVERY_SUBJECT, new Object[] { user.getUsername() }, locale); String text = messageSource.getMessage(MSG_PASSWORD_RECOVERY_TEXT, new Object[] { user.getUsername(), context.getHostname(), context.getRecoveryLink() }, locale);/* ww w . j a v a 2 s. c o m*/ MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom("\"" + fromHandle + "\" <" + fromAddr + ">"); message.setTo(user.getEmail()); message.setSubject(subject); message.setText(text); } }); log.info("Password recovery link sent to " + user.getEmail() + " with key " + passwordRecovery.getKey()); }