List of usage examples for org.springframework.mail.javamail MimeMessageHelper setTo
public void setTo(String[] to) throws MessagingException
From source file:business.services.MailService.java
public void notifyHubuser(User hubUser, List<LabRequestRepresentation> labRequests) { if (!hubUser.isHubUser()) { log.warn("The user is no hub user: " + hubUser.getUsername()); return;/*from w ww . j a v a2 s. c om*/ } List<String> codes = new ArrayList<>(); List<String> snippets = new ArrayList<>(); for (LabRequestRepresentation labRequest : labRequests) { codes.add(labRequest.getLabRequestCode()); String link = getLink("/#/lab-request/view/" + labRequest.getId()); String snippet = String.format(hubUserNotificationLabSnippet, link, // %1 labRequest.getLabRequestCode(), // %2 labRequest.getRequest().getTitle(), // %3 labRequest.getLab().getNumber(), // %4 labRequest.getLab().getName(), // %5 labRequest.getRequesterName(), // %6 labRequest.getRequest().getPathologistName() == null ? "" : labRequest.getRequest().getPathologistName(), // %7 labRequest.getRequesterLab().getNumber(), // %8 labRequest.getRequesterLab().getName() // %9 ); snippets.add(snippet); } String labRequestCodes = String.join(", ", codes); String labRequestSnippets = String.join("\n", snippets); log.info("Notify hub user " + hubUser.getUsername() + " for lab requests " + labRequestCodes + "."); if (hubUser.getContactData() == null || hubUser.getContactData().getEmail() == null || hubUser.getContactData().getEmail().trim().isEmpty()) { log.warn("No email address set for hub user " + hubUser.getUsername()); return; } log.info("Sending notification to " + hubUser.getContactData().getEmail()); try { MimeMessageHelper message = new MimeMessageHelper(mailSender.createMimeMessage()); message.setTo(hubUser.getContactData().getEmail()); message.setFrom(getFrom(), fromName); message.setReplyTo(replyAddress, replyName); message.setSubject( String.format("PALGA-verzoek aan laboratoria, aanvraagnummers: %s", labRequestCodes)); String body = String.format(hubUserNotificationTemplate, labRequestSnippets /* %1 */); message.setText(body); mailSender.send(message.getMimeMessage()); } catch (MessagingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } }
From source file:eu.trentorise.smartcampus.citizenportal.service.EmailService.java
public void sendMailWithInline(final String recipientName, final String recipientEmail, final String imageResourceName, final byte[] imageBytes, final String imageContentType, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); message.setSubject("Example HTML email with inline image"); message.setFrom("thymeleaf@example.com"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-inlineimage.html", ctx); message.setText(htmlContent, true /* isHtml */); // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}" final InputStreamSource imageSource = new ByteArrayResource(imageBytes); message.addInline(imageResourceName, imageSource, imageContentType); // Send mail/*ww w .j a va 2 s. com*/ this.mailSender.send(mimeMessage); }
From source file:mx.edu.um.mateo.rh.web.EstudiosEmpleadoController.java
private void enviaCorreo(String tipo, List<EstudiosEmpleado> estudiosEmpleados, HttpServletRequest request) throws JRException, MessagingException { log.debug("Enviando correo {}", tipo); byte[] archivo = null; String tipoContenido = null;/*from www. j a v a 2 s .c o m*/ switch (tipo) { case "PDF": archivo = generaPdf(estudiosEmpleados); tipoContenido = "application/pdf"; break; case "CSV": archivo = generaCsv(estudiosEmpleados); tipoContenido = "text/csv"; break; case "XLS": archivo = generaXls(estudiosEmpleados); tipoContenido = "application/vnd.ms-excel"; } MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(ambiente.obtieneUsuario().getUsername()); String titulo = messageSource.getMessage("estudiosEmpleado.lista.label", null, request.getLocale()); helper.setSubject(messageSource.getMessage("envia.correo.titulo.message", new String[] { titulo }, request.getLocale())); helper.setText(messageSource.getMessage("envia.correo.contenido.message", new String[] { titulo }, request.getLocale()), true); helper.addAttachment(titulo + "." + tipo, new ByteArrayDataSource(archivo, tipoContenido)); mailSender.send(message); }
From source file:gr.abiss.calipso.service.EmailService.java
@Async public void sendMailWithAttachment(final String recipientName, final String recipientEmail, final String attachmentFileName, final byte[] attachmentBytes, final String attachmentContentType, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); message.setSubject("Example HTML email with attachment"); message.setFrom("thymeleaf@example.com"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-withattachment.html", ctx); LOGGER.info("Sending email body: " + htmlContent); message.setText(htmlContent, true /* isHtml */); // Add the attachment final InputStreamSource attachmentSource = new ByteArrayResource(attachmentBytes); message.addAttachment(attachmentFileName, attachmentSource, attachmentContentType); // Send mail/* w ww .ja va 2s . c o m*/ this.mailSender.send(mimeMessage); }
From source file:gr.abiss.calipso.service.EmailService.java
@Async public void sendMailWithInline(final String recipientName, final String recipientEmail, final String imageResourceName, final byte[] imageBytes, final String imageContentType, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); ctx.setVariable("imageResourceName", imageResourceName); // so that we can reference it from HTML // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); message.setSubject("Example HTML email with inline image"); message.setFrom("thymeleaf@example.com"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-inlineimage.html", ctx); LOGGER.info("Sending email body: " + htmlContent); message.setText(htmlContent, true /* isHtml */); // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}" final InputStreamSource imageSource = new ByteArrayResource(imageBytes); message.addInline(imageResourceName, imageSource, imageContentType); // Send mail/* ww w . j a v a 2s.c o m*/ this.mailSender.send(mimeMessage); }
From source file:business.services.MailService.java
public void sendActivationEmail(@NotNull ActivationLink link) { // Send email to user try {/*w w w . j a va 2 s . c om*/ MimeMessageHelper message = new MimeMessageHelper(mailSender.createMimeMessage()); String recipient = link.getUser().getUsername(); message.setTo(recipient); message.setFrom(getFrom(), fromName); message.setReplyTo(replyAddress, replyName); message.setSubject("PALGA-account activeren / Activate PALGA account"); String activationLink = getLink("/#/activate/" + link.getToken()); message.setText(String.format(activationEmailTemplate, activationLink)); mailSender.send(message.getMimeMessage()); log.info("Activation link token generated for " + recipient + ": " + link.getToken()); } catch (MessagingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } }
From source file:cz.zcu.kiv.eegdatabase.data.service.SpringJavaMailService.java
protected MimeMessage createMimeMessage(String from, String to, String subject, String emailBody) throws MailException { try {/*from w w w .j a v a 2 s . c o m*/ MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage); message.setFrom(from); // message.setContent("text/html"); message.setTo(to); message.setSubject(subject); message.setText(emailBody, true); log.debug("Message " + message); return mimeMessage; } catch (MessagingException e) {// rethrow as MailException throw new MailPreparationException(e.getMessage(), e); } }
From source file:business.services.MailService.java
public void sendPasswordRecoveryToken(NewPasswordRequest npr) { try {//from www . j a v a 2 s .c om MimeMessageHelper message = new MimeMessageHelper(mailSender.createMimeMessage()); String recipient = npr.getUser().getContactData().getEmail(); message.setTo(recipient); message.setFrom(getFrom(), fromName); message.setReplyTo(replyAddress, replyName); message.setSubject(passwordRecoverySubject); String passwordRecoveryLink = getLink("/#/login/reset-password/" + npr.getToken()); message.setText(String.format(passwordRecoveryTemplate, passwordRecoveryLink)); log.info("Sending password recovery token to " + recipient + "."); mailSender.send(message.getMimeMessage()); } catch (MessagingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } catch (UnsupportedEncodingException e) { log.error(e.getMessage()); throw new EmailError("Email error: " + e.getMessage()); } }
From source file:eu.trentorise.smartcampus.citizenportal.service.EmailService.java
public String sendSimpleMail(final String recipientName, final String recipientEmail, final String subject, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); //ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); ctx.setVariable("text", subject); // Prepare message using a Spring helper final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8"); message.setSubject("Graduatoria Edilizia Abitativa"); //message.setFrom("thymeleaf@example.com"); message.setFrom("myweb.edilizia@comunitadellavallagarina.tn.it"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = this.templateEngine.process("email-simple.html", ctx); message.setText(htmlContent, true /* isHtml */); // Send email this.mailSender.send(mimeMessage); return recipientName; }
From source file:MailSender.java
public void sendUserPassword(User user, String clearText) { // major: well, sender is null, so no email is going out...silently. Just a debug message. // This means that the initial configuration is not able to create a sender. // This control should no be here and the methods that handle jndi or configFile should throw a // ConfigurationException if the sender is not created. if (sender == null) { logger.debug("mail sender is null, not sending new user / password change notification"); return;// w w w . j a v a 2s. c om } logger.debug("attempting to send mail for user password"); String localeString = user.getLocale(); Locale locale = null; if (localeString == null) { locale = defaultLocale; } else { locale = StringUtils.parseLocaleString(localeString); } // major: there is a bit of code duplication with the method send. // Apparently it seems that just the body is changing but the rest is // really similar. As suggested above the way a message is created should be // refactored in a collaborator. This method should just send a message. MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8"); try { helper.setTo(user.getEmail()); helper.setSubject(prefix + " " + fmt("loginMailSubject", locale)); StringBuffer sb = new StringBuffer(); sb.append("<p>" + fmt("loginMailGreeting", locale) + " " + user.getName() + ",</p>"); sb.append("<p>" + fmt("loginMailLine1", locale) + "</p>"); sb.append("<table class='jtrac'>"); sb.append("<tr><th style='background: #CCCCCC'>" + fmt("loginName", locale) + "</th><td style='border: 1px solid black'>" + user.getLoginName() + "</td></tr>"); sb.append("<tr><th style='background: #CCCCCC'>" + fmt("password", locale) + "</th><td style='border: 1px solid black'>" + clearText + "</td></tr>"); sb.append("</table>"); sb.append("<p>" + fmt("loginMailLine2", locale) + "</p>"); sb.append("<p><a href='" + url + "'>" + url + "</a></p>"); helper.setText(addHeaderAndFooter(sb), true); helper.setSentDate(new Date()); // helper.setCc(from); helper.setFrom(from); sendInNewThread(message); // major: generic exception, method too long, I don't understand what could fail. } catch (Exception e) { logger.error("failed to prepare e-mail", e); } }