List of usage examples for org.springframework.mail.javamail JavaMailSender createMimeMessage
MimeMessage createMimeMessage();
From source file:com.pamarin.income.component.MailSenderImpl.java
@Override public void send(MailCallback callback) { try {//from www. jav a 2 s. co m JavaMailSender sender = senderSetup(); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); callback.execute(helper); sender.send(message); } catch (Exception ex) { throw new UncheckedMailException(ex); } }
From source file:com.github.dbourdette.otto.service.mail.Mailer.java
public void send(Mail mail) throws MessagingException, UnsupportedEncodingException { MailConfiguration configuration = findConfiguration(); JavaMailSender javaMailSender = mailSender(configuration); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "UTF-8"); helper.setSubject(mail.getSubject()); helper.setFrom(configuration.getSender()); for (String name : StringUtils.split(mail.getTo(), ",")) { helper.addTo(name);//from w w w .j a v a 2 s . co m } helper.setText(mail.getHtml(), true); javaMailSender.send(mimeMessage); }
From source file:ar.com.zauber.commons.message.impl.mail.MimeEmailNotificationStrategy.java
/** @see NotificationStrategy#execute(NotificationAddress[], Message) */ public final void execute(final NotificationAddress[] addresses, final Message message) { try {/*ww w . j a v a2s . co m*/ final MailSender mailSender = getMailSender(); //This is ugly....but if it is not a JavaMailSender it will //fail (for instance during tests). And the only way to //create a Multipartemail is through JavaMailSender if (mailSender instanceof JavaMailSender) { final JavaMailSender javaMailSender = (JavaMailSender) mailSender; final MimeMessage mail = javaMailSender.createMimeMessage(); final MimeMessageHelper helper = new MimeMessageHelper(mail, true); helper.setFrom(getFromAddress().getEmailStr()); helper.setTo(SimpleEmailNotificationStrategy.getEmailAddresses(addresses)); helper.setReplyTo(getEmailAddress(message.getReplyToAddress())); helper.setSubject(message.getSubject()); setContent(helper, (MultipartMessage) message); addHeaders(message, mail); javaMailSender.send(mail); } else { final SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom(getFromAddress().getEmailStr()); mail.setTo(getEmailAddresses(addresses)); mail.setReplyTo(getEmailAddress(message.getReplyToAddress())); mail.setSubject(message.getSubject()); mail.setText(message.getContent()); mailSender.send(mail); } } catch (final MessagingException e) { throw new RuntimeException("Could not send message", e); } catch (final UnsupportedEncodingException e) { throw new RuntimeException("Could not send message", e); } }
From source file:de.blizzy.documentr.mail.SubscriptionMailer.java
private void sendMail(String subject, String text, String senderEmail, String senderName, Set<String> subscriberEmails, JavaMailSender sender) { for (String subscriberEmail : subscriberEmails) { try {// w w w .j av a 2 s.c o m MimeMessage msg = sender.createMimeMessage(); msg.setFrom(createAddress(senderEmail, senderName)); msg.setRecipient(RecipientType.TO, createAddress(subscriberEmail, null)); msg.setSubject(subject, Charsets.UTF_8.name()); msg.setText(text, Charsets.UTF_8.name()); sender.send(msg); } catch (MessagingException e) { log.error("error while sending mail", e); //$NON-NLS-1$ } } }
From source file:com.github.dactiv.fear.service.service.message.MessageService.java
/** * ??//from w w w. ja v a 2 s . c o m * * @param nickname ?? null * @param mail */ private void doSendMail(String nickname, Mail mail) { try { JavaMailSender mailSender = getJavaMailSender(); if (mailSender == null) { throw new ServiceException("???"); } MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper; if (mailSender instanceof JavaMailSenderImpl) { JavaMailSenderImpl jmsi = (JavaMailSenderImpl) mailSender; helper = new MimeMessageHelper(msg, true, jmsi.getDefaultEncoding()); } else { helper = new MimeMessageHelper(msg, true); } helper.setTo(mail.getTo()); helper.setFrom(getSendForm(nickname, mailSender)); helper.setSubject(mail.getTitle()); helper.setText(mail.getContent(), mail.getHtml()); if (!MapUtils.isEmpty(mail.getAttachment())) { for (Map.Entry<String, File> entry : mail.getAttachment().entrySet()) { helper.addAttachment(entry.getKey(), entry.getValue()); } } mailSender.send(msg); LOGGER.info("???"); } catch (Exception e) { LOGGER.error("??", e); } }
From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobMailNotificationImpl.java
public void sendMailNotification(ReportExecutionJob job, ReportJob jobDetails, List reportOutputs) throws JobExecutionException { ReportJobMailNotification mailNotification = jobDetails.getMailNotification(); if (mailNotification != null) { try {/*from w w w. j a v a2 s. co m*/ // skip mail notification when job fails if (mailNotification.isSkipNotificationWhenJobFails() && (!job.exceptions.isEmpty())) { return; } JavaMailSender mailSender = job.getMailSender(); String fromAddress = job.getFromAddress(); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, job.getCharacterEncoding()); messageHelper.setFrom(fromAddress); messageHelper.setSubject(mailNotification.getSubject()); StringBuffer messageText = new StringBuffer(); addMailRecipients(mailNotification, messageHelper); boolean isEmailBodyOccupied = false; if (reportOutputs != null && !reportOutputs.isEmpty()) { byte resultSendType = jobDetails.getMailNotification().getResultSendType(); if ((resultSendType == ReportJobMailNotification.RESULT_SEND_EMBED) || (resultSendType == ReportJobMailNotification.RESULT_SEND_EMBED_ZIP_ALL_OTHERS)) { List attachmentReportList = new ArrayList(); for (Iterator it = reportOutputs.iterator(); it.hasNext();) { ReportOutput output = (ReportOutput) it.next(); if ((!isEmailBodyOccupied) && output.getFileType().equals(ContentResource.TYPE_HTML) && (job.exceptions.isEmpty())) { // only embed html output embedOutput(messageHelper, output); isEmailBodyOccupied = true; } else if (resultSendType == ReportJobMailNotification.RESULT_SEND_EMBED) { // save the rest of the output as attachments attachOutput(job, messageHelper, output, (resultSendType == ReportJobMailNotification.RESULT_SEND_ATTACHMENT)); } else { // RESULT_SEND_EMBED_ZIP_ALL_OTHERS attachmentReportList.add(output); } } if (attachmentReportList.size() > 0) { // put the rest of attachments in 1 zip file attachOutputs(job, messageHelper, attachmentReportList); } } else if (resultSendType == ReportJobMailNotification.RESULT_SEND_ATTACHMENT || resultSendType == ReportJobMailNotification.RESULT_SEND_ATTACHMENT_NOZIP) { for (Iterator it = reportOutputs.iterator(); it.hasNext();) { ReportOutput output = (ReportOutput) it.next(); attachOutput(job, messageHelper, output, (resultSendType == ReportJobMailNotification.RESULT_SEND_ATTACHMENT)); } } else if (resultSendType == ReportJobMailNotification.RESULT_SEND_ATTACHMENT_ZIP_ALL) { // put all the attachments in 1 zip file attachOutputs(job, messageHelper, reportOutputs); } else { appendRepositoryLinks(job, messageText, reportOutputs); } } if (mailNotification.isIncludingStackTraceWhenJobFails()) { if (!job.exceptions.isEmpty()) { for (Iterator it = job.exceptions.iterator(); it.hasNext();) { ExceptionInfo exception = (ExceptionInfo) it.next(); messageText.append("\n"); messageText.append(exception.getMessage()); attachException(messageHelper, exception); } } } String text = mailNotification.getMessageText(); if (!job.exceptions.isEmpty()) { if (mailNotification.getMessageTextWhenJobFails() != null) text = mailNotification.getMessageTextWhenJobFails(); else text = job.getMessage("report.scheduling.job.default.mail.notification.message.on.fail", null); } if (!isEmailBodyOccupied) messageHelper.setText(text + "\n" + messageText.toString()); mailSender.send(message); } catch (MessagingException e) { log.error("Error while sending report job result mail", e); throw new JSExceptionWrapper(e); } } }
From source file:com.glaf.mail.MailSenderImpl.java
public void send(JavaMailSender javaMailSender, MailMessage mailMessage) throws Exception { if (StringUtils.isEmpty(mailMessage.getMessageId())) { mailMessage.setMessageId(UUID32.getUUID()); }//ww w. j av a 2 s . c o m mailHelper = new MxMailHelper(); MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true); if (StringUtils.isNotEmpty(mailMessage.getFrom())) { messageHelper.setFrom(mailMessage.getFrom()); mailFrom = mailMessage.getFrom(); } else { if (StringUtils.isEmpty(mailFrom)) { mailFrom = MailProperties.getString("mail.mailFrom"); } messageHelper.setFrom(mailFrom); } logger.debug("mailFrom:" + mailFrom); if (mailMessage.getTo() != null) { messageHelper.setTo(mailMessage.getTo()); } if (mailMessage.getCc() != null) { messageHelper.setCc(mailMessage.getCc()); } if (mailMessage.getBcc() != null) { messageHelper.setBcc(mailMessage.getBcc()); } if (mailMessage.getReplyTo() != null) { messageHelper.setReplyTo(mailMessage.getReplyTo()); } String mailSubject = mailMessage.getSubject(); if (mailSubject == null) { mailSubject = ""; } if (mailSubject != null) { // mailSubject = MimeUtility.encodeText(new // String(mailSubject.getBytes(), encoding), encoding, "B"); mailSubject = MimeUtility.encodeWord(mailSubject); } mimeMessage.setSubject(mailSubject); Map<String, Object> dataMap = mailMessage.getDataMap(); if (dataMap == null) { dataMap = new java.util.HashMap<String, Object>(); } String serviceUrl = SystemConfig.getServiceUrl(); logger.debug("mailSubject:" + mailSubject); logger.debug("serviceUrl:" + serviceUrl); if (serviceUrl != null) { String loginUrl = serviceUrl + "/mx/login"; String mainUrl = serviceUrl + "/mx/main"; logger.debug("loginUrl:" + loginUrl); dataMap.put("loginUrl", loginUrl); dataMap.put("mainUrl", mainUrl); } mailMessage.setDataMap(dataMap); if (StringUtils.isEmpty(mailMessage.getContent())) { Template template = TemplateContainer.getContainer().getTemplate(mailMessage.getTemplateId()); if (template != null) { String templateType = template.getTemplateType(); logger.debug("templateType:" + templateType); // logger.debug("content:" + template.getContent()); if (StringUtils.equals(templateType, "eml")) { if (template.getContent() != null) { Mail m = mailHelper.getMail(template.getContent().getBytes()); String content = m.getContent(); if (StringUtils.isNotEmpty(content)) { template.setContent(content); try { Writer writer = new StringWriter(); TemplateUtils.evaluate(mailMessage.getTemplateId(), dataMap, writer); String text = writer.toString(); writer.close(); writer = null; mailMessage.setContent(text); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } } } else { try { String text = TemplateUtils.process(dataMap, template.getContent()); mailMessage.setContent(text); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } } } if (StringUtils.isNotEmpty(mailMessage.getContent())) { String text = mailMessage.getContent(); if (StringUtils.isNotEmpty(callbackUrl)) { String href = callbackUrl + "?messageId=" + mailMessage.getMessageId(); text = mailHelper.embedCallbackScript(text, href); mailMessage.setContent(text); logger.debug(text); messageHelper.setText(text, true); } messageHelper.setText(text, true); } logger.debug("mail body:" + mailMessage.getContent()); Collection<Object> files = mailMessage.getFiles(); if (files != null && !files.isEmpty()) { Iterator<Object> iterator = files.iterator(); while (iterator.hasNext()) { Object object = iterator.next(); if (object instanceof java.io.File) { java.io.File file = (java.io.File) object; FileSystemResource resource = new FileSystemResource(file); String name = file.getName(); name = MailTools.chineseStringToAscii(name); messageHelper.addAttachment(name, resource); logger.debug("add attachment:" + name); } else if (object instanceof DataSource) { DataSource dataSource = (DataSource) object; String name = dataSource.getName(); name = MailTools.chineseStringToAscii(name); messageHelper.addAttachment(name, dataSource); logger.debug("add attachment:" + name); } else if (object instanceof DataFile) { DataFile dataFile = (DataFile) object; if (StringUtils.isNotEmpty(dataFile.getFilename())) { String name = dataFile.getFilename(); name = MailTools.chineseStringToAscii(name); InputStreamSource inputStreamSource = new MxMailInputSource(dataFile); messageHelper.addAttachment(name, inputStreamSource); logger.debug("add attachment:" + name); } } } } mimeMessage.setSentDate(new java.util.Date()); javaMailSender.send(mimeMessage); logger.info("-----------------------------------------"); logger.info("????"); logger.info("-----------------------------------------"); }
From source file:com.jaspersoft.jasperserver.api.engine.scheduling.quartz.ReportExecutionJobAlertImpl.java
public void sendAlertMail(Job job, ReportJob jobDetails, List<ExceptionInfo> exceptions, JavaMailSender mailSender, String fromAddress, String[] toAddresses, String characterEncoding) throws JobExecutionException { ReportJobAlert alert = jobDetails.getAlert(); boolean isSucceed = exceptions.isEmpty(); switch (alert.getJobState()) { case FAIL_ONLY: if (isSucceed) return; break;//from w ww . j av a 2s . c o m case SUCCESS_ONLY: if (!isSucceed) return; break; case NONE: return; } if (alert != null) { try { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, characterEncoding); messageHelper.setFrom(fromAddress); String subject = alert.getSubject(); if ((subject == null) && (job instanceof ReportExecutionJob)) subject = ((ReportExecutionJob) job).getMessage("report.scheduling.job.default.alert.subject", null); messageHelper.setSubject(subject); StringBuffer messageText = new StringBuffer(); String text = (isSucceed ? alert.getMessageText() : alert.getMessageTextWhenJobFails()); if (text != null) { messageText.append(text); } messageHelper.setTo(toAddresses); if (alert.isIncludingReportJobInfo()) { messageText.append("\n"); messageText.append("ReportJob Info:").append("\n"); messageText.append("Label = ").append(jobDetails.getLabel()).append("\n"); messageText.append("ID = ").append(jobDetails.getId()).append("\n"); messageText.append("Description = ").append(jobDetails.getDescription()).append("\n"); messageText.append("Status = ").append(exceptions.isEmpty() ? "PASS" : "FAIL").append("\n"); } if (alert.isIncludingStackTrace()) { if (!exceptions.isEmpty()) { for (Iterator it = exceptions.iterator(); it.hasNext();) { ExceptionInfo exception = (ExceptionInfo) it.next(); messageText.append("\n"); messageText.append(exception.getMessage()); attachException(messageHelper, exception); } } } messageHelper.setText(messageText.toString()); mailSender.send(message); } catch (MessagingException e) { log.error("Error while sending report job alert notification", e); throw new JSExceptionWrapper(e); } } }
From source file:org.toobsframework.email.SmtpMailSender.java
public void sendEmail(EmailBean email) throws MailException, MessagingException { JavaMailSender javaMailSender = (JavaMailSender) javaMailSenders.get(email.getMailSenderKey()); if (javaMailSender == null) { throw new MessagingException(email.getMailSenderKey() + " is an invalid mailSenderKey"); }/*from www. j a va2s .c o m*/ if (this.getMailProperties() != null) { ((JavaMailSenderImpl) javaMailSender).setJavaMailProperties(this.getMailProperties()); } try { String[] recipients = this.processRecipients(email.getRecipients()); String[] bccs = new String[email.getBccs().size()]; for (int i = 0; i < recipients.length; i++) { MimeMessage message = null; MimeMessageHelper helper = null; String thisRecipient = recipients[i]; switch (email.getType()) { case EmailBean.MESSAGE_TYPE_TEXT: message = javaMailSender.createMimeMessage(); helper = new MimeMessageHelper(message, false, "us-ascii"); helper.setSubject(email.getEmailSubject()); helper.setFrom(email.getEmailSender()); helper.setTo(thisRecipient); helper.setBcc((String[]) email.getBccs().toArray(bccs)); helper.setText(email.getMessageText(), false); log.info("Sending message " + message.toString()); javaMailSender.send(message); break; case EmailBean.MESSAGE_TYPE_HTML: message = javaMailSender.createMimeMessage(); helper = new MimeMessageHelper(message, true, "us-ascii"); helper.setSubject(email.getEmailSubject()); helper.setFrom(email.getEmailSender()); helper.setTo(thisRecipient); helper.setBcc((String[]) email.getBccs().toArray(bccs)); helper.setText(email.getMessageText(), email.getMessageHtml()); log.info("Sending message " + message.toString()); javaMailSender.send(message); break; } } } catch (Exception e) { log.error("Mail exception " + e.getMessage(), e); throw new MessagingException(e.getMessage()); } }