List of usage examples for org.springframework.mail.javamail MimeMessageHelper MimeMessageHelper
public MimeMessageHelper(MimeMessage mimeMessage, int multipartMode) throws MessagingException
From source file:org.patientview.monitoring.ImportMonitor.java
public static void sendEmail(String from, String[] to, String[] bcc, String subject, String body) { if (StringUtils.isBlank(from)) { throw new IllegalArgumentException("Cannot send mail missing 'from'"); }// w w w . ja v a 2 s . c o m if ((to == null || to.length == 0) && (bcc == null || bcc.length == 0)) { throw new IllegalArgumentException("Cannot send mail missing recipients"); } if (StringUtils.isBlank(subject)) { throw new IllegalArgumentException("Cannot send mail missing 'subject'"); } if (StringUtils.isBlank(body)) { throw new IllegalArgumentException("Cannot send mail missing 'body'"); } ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "classpath*:context-standalone.xml" }); JavaMailSender javaMailSender = (JavaMailSender) context.getBean("javaMailSender"); MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper; try { messageHelper = new MimeMessageHelper(message, true); messageHelper.setTo(to); if (bcc != null && bcc.length > 0) { Address[] bccAddresses = new Address[bcc.length]; for (int i = 0; i < bcc.length; i++) { bccAddresses[i] = new InternetAddress(bcc[i]); } message.addRecipients(Message.RecipientType.BCC, bccAddresses); } messageHelper.setFrom(from); messageHelper.setSubject(subject); messageHelper.setText(body, false); // Note: the second param indicates to send plaintext javaMailSender.send(messageHelper.getMimeMessage()); LOGGER.info("Sent an email about Importer issues. From: {} To: {}", from, Arrays.toString(to)); } catch (Exception e) { LOGGER.error("Could not send email: {}", e); } }
From source file:org.sipfoundry.sipxconfig.admin.mail.MailSenderContextImpl.java
public void sendMail(String to, String from, String subject, String body, File... files) { if (files == null || files.length == 0) { sendMail(to, from, subject, body); return;// ww w . j av a 2s . c o m } try { MimeMessage msg = m_mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true); helper.setTo(getFullAddress(to)); helper.setFrom(getFullAddress(from)); helper.setSubject(subject); helper.setText(body); for (File f : files) { helper.addAttachment(f.getName(), f); } m_mailSender.send(msg); } catch (MailException e) { LOG.error(e); } catch (MessagingException e) { LOG.error(e); } }
From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java
/** * This test will create a Mime Message that contains an Attachment, send it * to an SMTP Server (Using Wiser) and retrieve and process the Mime Message. * * This test verifies that the parsing of the retrieved Mime Message is * successful and that the correct number of {@link EmailFragment}s is created. */// w ww . j a v a2 s.c o m @Test public void testProcessingOfEmailAttachments() { final JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setPort(this.wiserPort); final MimeMessage message = mailSender.createMimeMessage(); final String pictureName = "picture.png"; final ByteArrayResource byteArrayResource = getFileData(pictureName); try { final MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("testfrom@springintegration.org"); helper.setTo("testto@springintegration.org"); helper.setSubject("Parsing of Attachments"); helper.setText("Spring Integration Rocks!"); helper.addAttachment(pictureName, byteArrayResource, "image/png"); } catch (MessagingException e) { throw new MailParseException(e); } mailSender.send(message); final List<WiserMessage> wiserMessages = wiser.getMessages(); Assert.assertTrue(wiserMessages.size() == 1); boolean foundTextMessage = false; boolean foundPicture = false; for (WiserMessage wiserMessage : wiserMessages) { final List<EmailFragment> emailFragments = new ArrayList<EmailFragment>(); try { final MimeMessage mailMessage = wiserMessage.getMimeMessage(); EmailParserUtils.handleMessage(null, mailMessage, emailFragments); } catch (MessagingException e) { throw new IllegalStateException("Error while retrieving Mime Message."); } Assert.assertTrue(emailFragments.size() == 2); for (EmailFragment emailFragment : emailFragments) { if ("picture.png".equals(emailFragment.getFilename())) { foundPicture = true; } if ("message.txt".equals(emailFragment.getFilename())) { foundTextMessage = true; } } Assert.assertTrue(foundPicture); Assert.assertTrue(foundTextMessage); } }
From source file:org.springframework.integration.samples.mailattachments.MimeMessageParsingTest.java
/** * This test will create a Mime Message that in return contains another * mime message. The nested mime message contains an attachment. * * The root message consist of both HTML and Text message. * *//*from w ww . j av a 2s . com*/ @Test public void testProcessingOfNestedEmailAttachments() { final JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setPort(this.wiserPort); final MimeMessage rootMessage = mailSender.createMimeMessage(); try { final MimeMessageHelper messageHelper = new MimeMessageHelper(rootMessage, true); messageHelper.setFrom("testfrom@springintegration.org"); messageHelper.setTo("testto@springintegration.org"); messageHelper.setSubject("Parsing of Attachments"); messageHelper.setText("Spring Integration Rocks!", "Spring Integration <b>Rocks</b>!"); final String pictureName = "picture.png"; final ByteArrayResource byteArrayResource = getFileData(pictureName); messageHelper.addInline("picture12345", byteArrayResource, "image/png"); } catch (MessagingException e) { throw new MailParseException(e); } mailSender.send(rootMessage); final List<WiserMessage> wiserMessages = wiser.getMessages(); Assert.assertTrue(wiserMessages.size() == 1); boolean foundTextMessage = false; boolean foundPicture = false; boolean foundHtmlMessage = false; for (WiserMessage wiserMessage : wiserMessages) { List<EmailFragment> emailFragments = new ArrayList<EmailFragment>(); try { final MimeMessage mailMessage = wiserMessage.getMimeMessage(); EmailParserUtils.handleMessage(null, mailMessage, emailFragments); } catch (MessagingException e) { throw new IllegalStateException("Error while retrieving Mime Message."); } Assert.assertTrue(emailFragments.size() == 3); for (EmailFragment emailFragment : emailFragments) { if ("<picture12345>".equals(emailFragment.getFilename())) { foundPicture = true; } if ("message.txt".equals(emailFragment.getFilename())) { foundTextMessage = true; } if ("message.html".equals(emailFragment.getFilename())) { foundHtmlMessage = true; } } Assert.assertTrue(foundPicture); Assert.assertTrue(foundTextMessage); Assert.assertTrue(foundHtmlMessage); } }
From source file:org.thingsboard.rule.engine.mail.TbSendEmailNode.java
private void sendEmail(TbContext ctx, EmailPojo email) throws Exception { if (this.config.isUseSystemSmtpSettings()) { ctx.getMailService().send(email.getFrom(), email.getTo(), email.getCc(), email.getBcc(), email.getSubject(), email.getBody()); } else {/*from w w w. ja v a 2 s . c om*/ MimeMessage mailMsg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8"); helper.setFrom(email.getFrom()); helper.setTo(email.getTo().split("\\s*,\\s*")); if (!StringUtils.isBlank(email.getCc())) { helper.setCc(email.getCc().split("\\s*,\\s*")); } if (!StringUtils.isBlank(email.getBcc())) { helper.setBcc(email.getBcc().split("\\s*,\\s*")); } helper.setSubject(email.getSubject()); helper.setText(email.getBody()); mailSender.send(helper.getMimeMessage()); } }
From source file:org.thingsboard.server.service.mail.DefaultMailService.java
@Override public void send(String from, String to, String cc, String bcc, String subject, String body) throws MessagingException { MimeMessage mailMsg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8"); helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from); helper.setTo(to.split("\\s*,\\s*")); if (!StringUtils.isBlank(cc)) { helper.setCc(cc.split("\\s*,\\s*")); }// w w w . jav a 2 s . c o m if (!StringUtils.isBlank(bcc)) { helper.setBcc(bcc.split("\\s*,\\s*")); } helper.setSubject(subject); helper.setText(body); mailSender.send(helper.getMimeMessage()); }
From source file:org.thingsboard.server.service.mail.DefaultMailService.java
private void sendMail(JavaMailSenderImpl mailSender, String mailFrom, String email, String subject, String message) throws ThingsboardException { try {/*from w w w .j a va 2s . c o m*/ MimeMessage mimeMsg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, UTF_8); helper.setFrom(mailFrom); helper.setTo(email); helper.setSubject(subject); helper.setText(message, true); mailSender.send(helper.getMimeMessage()); } catch (Exception e) { throw handleException(e); } }
From source file:org.wso2.security.tools.automation.manager.handler.MailHandler.java
/** * Send an email with an attachment//from w ww. jav a2 s.c o m * * @param to To whom the email is sent * @param subject Email subject * @param body Email body * @param inputStream Input stream of an attachment * @param attachmentFileName Attachment file name * @throws MessagingException Exceptions thrown by the Messaging classes * @throws IOException Signals that an I/O exception of some sort has occurred */ public void sendMail(String to, String subject, String body, InputStream inputStream, String attachmentFileName) throws MessagingException, IOException { LOGGER.info("Sending email"); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setTo(to); mimeMessageHelper.setText(body); mimeMessageHelper.addAttachment(attachmentFileName, new ByteArrayResource(IOUtils.toByteArray(inputStream))); mailSender.send(mimeMessageHelper.getMimeMessage()); }
From source file:siddur.solidtrust.scrape.ScrapeMonitor.java
private void send(boolean alert1, boolean alert2, int marktplaats, int autoscout) { MimeMessage message = mailSender.createMimeMessage(); try {/*from w ww. j a v a 2 s . c om*/ MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("gbg1_spsms_gtrak@pactera-pgs-mail.chinacloudapp.cn", "Solidtrust Admin"); if (alert1 || alert2) { log4j.info("Send email to alert that data scraped very little"); helper.setTo(SolidtrustConstants.BEN_EMAIL); helper.setCc(SolidtrustConstants.MY_EMAIL); } else { helper.setTo(SolidtrustConstants.MY_EMAIL); } helper.setSubject("Little data scraped alert"); StringBuilder sb = new StringBuilder(); sb.append( "<html><body><table border='1' cellspacing='0'><tr><td>Source</td><td>Amount Today</td><td>Normal amount daily</td></tr>"); sb.append("<tr><td>Martplaats</td><td>{0}</td><td>9000</td></tr>"); sb.append("<tr><td>AutoscoutNl</td><td>{1}</td><td>250</td></tr>"); sb.append("</table></body></html>"); helper.setText(MessageFormat.format(sb.toString(), alert1 ? "<font color='red'>" + marktplaats + "</font>" : marktplaats, alert2 ? "<font color='red'>" + autoscout + "</font>" : autoscout), true); mailSender.send(message); } catch (Exception e) { log4j.error(e.getMessage(), e); } }
From source file:siddur.solidtrust.wok.WokController.java
@Scheduled(cron = "0 5 1 2 * ?") //01:05 2th monthly @RequestMapping("notify") public void sendMail() throws Exception { Calendar today = Calendar.getInstance(); int year = today.get(Calendar.YEAR); int month = today.get(Calendar.MONTH); String filename = year + "-" + month + ".xlsx"; File file = new File(FileSystemUtil.getWokDir(), filename); generateFile(year, month, file);//w w w. j a v a 2 s .c o m log4j.info("Send email for WOK records: " + file.getName()); MimeMessage message = mailSender.createMimeMessage(); // use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom("gbg1_spsms_gtrak@pactera-pgs-mail.chinacloudapp.cn", "Solidtrust Admin"); helper.setTo(SolidtrustConstants.WOK_EMAIL); helper.addCc(SolidtrustConstants.BEN_EMAIL); helper.addCc(SolidtrustConstants.MY_EMAIL); helper.setSubject(MessageFormat.format("WOK[{0}]", (year + "-" + month))); // use the true flag to indicate the text included is HTML helper.setText("<html><body>Here is WOK data of last month. Thanks.</body></html>", true); // let's include the infamous windows Sample file (this time copied to c:/) FileSystemResource res = new FileSystemResource(file); helper.addAttachment(file.getName(), res); mailSender.send(message); }