List of usage examples for org.springframework.mail.javamail MimeMessageHelper getMimeMessage
public final MimeMessage getMimeMessage()
From source file:org.apereo.portal.portlets.account.EmailPasswordResetNotificationImpl.java
@Override public void sendNotification(URL resetUrl, ILocalAccountPerson account, Locale locale) { log.debug("Sending password reset instructions to user with url {}", resetUrl.toString()); try {/*from w w w.j av a 2 s . c o m*/ MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); String email = (String) account.getAttributeValue(ILocalAccountPerson.ATTR_MAIL); String subject = messageSource.getMessage(subjectMessageKey, new Object[] {}, locale); String body = formatBody(resetUrl, account, locale); helper.addTo(email); helper.setText(body, true); helper.setSubject(subject); helper.setFrom(portalEmailAddress, messageSource.getMessage("portal.name", new Object[] {}, locale)); log.debug("Sending message to {} from {} subject {}", email, Arrays.toString(message.getFrom()), message.getSubject()); this.mailSender.send(helper.getMimeMessage()); } catch (Exception e) { log.error("Unable to send password reset email", e); } }
From source file:org.craftercms.commons.mail.impl.EmailFactoryImpl.java
protected MimeMessage createMessage(String from, String[] to, String[] cc, String[] bcc, String replyTo, String subject, String body, boolean html, File... attachments) throws EmailException { boolean addAttachments = ArrayUtils.isNotEmpty(attachments); MimeMessageHelper messageHelper; try {//from w ww .j a v a 2 s . c o m if (addAttachments) { messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true); } else { messageHelper = new MimeMessageHelper(mailSender.createMimeMessage()); } messageHelper.setFrom(from); if (to != null) { messageHelper.setTo(to); } if (cc != null) { messageHelper.setCc(cc); } if (bcc != null) { messageHelper.setBcc(bcc); } if (replyTo != null) { messageHelper.setReplyTo(replyTo); } messageHelper.setSubject(subject); messageHelper.setText(body, html); if (addAttachments) { for (File attachment : attachments) { messageHelper.addAttachment(attachment.getName(), attachment); } } } catch (AddressException e) { throw new EmailAddressException(e); } catch (MessagingException e) { throw new EmailPreparationException(e); } logger.debug(LOG_KEY_MIME_MSG_CREATED, from, StringUtils.join(to, ','), StringUtils.join(cc, ','), StringUtils.join(bcc, ','), subject, body); return messageHelper.getMimeMessage(); }
From source file:org.emmanet.jobs.standAloneMailer.java
public void onSubmit() { /* String[] euCountriesList = {"Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Denmark", "Estonia", "Finland", "France", "Germany", "Greece", "Hungary", "Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg", "Malta", "Netherlands", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", "Spain", "Sweden", "United Kingdom"}; String[] assocCountriesList = {"Albania", "Croatia", "Iceland", "Israel", "Liechtenstein", "Macedonia", "Montenegro", "Norway", "Serbia", "Switzerland", "Turkey"}; Arrays.sort(euCountriesList);/*from w w w. ja v a 2 s . c o m*/ Arrays.sort(assocCountriesList);*/ //read from file try { BufferedReader in = new BufferedReader(new FileReader(Configuration.get("MAILCONTENT"))); String str; while ((str = in.readLine()) != null) { //content = content + str; content = (new StringBuilder()).append(content).append(str).toString(); } in.close(); } catch (IOException e) { e.printStackTrace(); } subject = "EMMAservice TA / impact assessment";//New Cre driver mouse lines" System.out.println("Subject: " + subject + "\n\nContent: " + content); //iterate over database email results adding to bcc use map keys ae address to prevent dups setCc(new HashMap()); //getCc().put(new String("emma@infrafrontier.eu"), ""); //getCc().put(new String("emma@infrafrontier.eu"), ""); // getCc().put(new String("sabine.fessele@helmholtz-muenchen.de"), ""); getCc().put(new String("michael.hagn@helmholtz-muenchen.de"), ""); getCc().put(new String("philw@ebi.ac.uk"), ""); setBcc(new HashMap()); //PeopleManager pm = new PeopleManager(); WebRequests wr = new WebRequests(); //List Bccs1 = wr.sciMails("sci_e_mail"); //List Bccs2 = wr.sciMails("sci_e_mail"); List Bccs = wr.sciMails("nullfield");//ListUtils.union(Bccs1,Bccs2); int BccSize = Bccs.size(); System.out.println("Size of list is: " + BccSize); //user asked to be removed,don't want to remove from database as details for email needed //Bccs1.remove("kgroden@interchange.ubc.ca"); //Bccs2.remove("kgroden@interchange.ubc.ca"); for (it = Bccs.listIterator(); it.hasNext();) { // Object[] o = (Object[]) it.next(); //System.out.println("object is:: " + o); String element = it.next().toString(); //String country = o[1].toString(); if (!Bcc.containsKey(it)) { // int index = Arrays.binarySearch(euCountriesList, country); // int index1 = Arrays.binarySearch(euCountriesList, country); // if (index >= 0 || index1 >= 0) { // System.out.println("Country OK :- " + country); System.out.println("element is: " + element); Bcc.put(element, ""); // } } } MimeMessage message = getJavaMailSender().createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); //helper.setValidateAddresses(false); helper.setReplyTo("emma@infrafrontier.eu"); helper.setFrom("emma@infrafrontier.eu"); System.out.println("BCC SIZE -- " + Bcc.size()); Iterator it1 = Bcc.keySet().iterator(); while (it1.hasNext()) { String BccAddress = (String) it1.next(); System.out.println("BccADDRESS===== " + BccAddress); if (BccAddress == null || BccAddress.trim().length() < 1 || !patternMatch(EMAIL_PATTERN, BccAddress)) { System.out .println("The Scientists Email address field appears to have no value or is incorrect"); BccSize = BccSize - 1; } else { //~~ helper.addBcc(BccAddress); } } System.out.println("CC SIZE -- " + Cc.size()); Iterator i = Cc.keySet().iterator(); while (i.hasNext()) { String CcAddress = (String) i.next(); System.out.println("ccADDRESS===== " + CcAddress); helper.addCc(CcAddress); } helper.setTo("emma@infrafrontier.eu");//info@emmanet.org //helper.setCc("webmaster.emmanet.org"); //helper.setBcc("philw@ebi.ac.uk"); helper.setText(content, true); helper.setSubject(subject); String filePath = Configuration.get("TMPFILES"); //String fileName = "PhenotypingSurveyCombinedNov2009.doc"; //String fileName2 = "EMPReSSslimpipelines-1.pdf"; //FileSystemResource file = new FileSystemResource(new File(filePath + fileName)); // FileSystemResource file2 = new FileSystemResource(new File(filePath + fileName2)); //helper.addAttachment(fileName, file); //helper.addAttachment(fileName2, file2); System.out.println(message); getJavaMailSender().send(message); try { BufferedWriter out = new BufferedWriter(new FileWriter(Configuration.get("FINALMAILCOUNT"))); out.write("FINAL BCC SIZE IS::" + BccSize); out.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println(helper.getMimeMessage()); } catch (MessagingException ex) { ex.printStackTrace(); } }
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'"); }/*from ww w. j a v a 2s .co 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.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 . j a v a 2s . co m*/ 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*")); }/*from w ww .ja v a2 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 {/*w ww . ja va2s . c om*/ 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/*w ww . j a v 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:org.yes.cart.service.mail.impl.MailComposerImplTest.java
@Test public void testComposeMimeMessageInternalTextAndHtmlVersion() throws MessagingException, IOException, ClassNotFoundException { // of course you would use DI in any real-world cases JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("localhost"); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); String textTemplate = "$name lives in theme park with <% with.each{ out.print(it + ' ');}%>"; String htmlTemplate = "<h2>$name</h2> lives in theme park with:<br> <% with.each{ out.print(it + '<br>');}%>"; MailComposerImpl mailComposer = new MailComposerImpl(null); mailComposer.composeMessage(helper, textTemplate, htmlTemplate, Collections.EMPTY_LIST, "SHOP10", "en", "test", createModel()); assertTrue(helper.isMultipart());/* w w w . j a v a 2 s .co m*/ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); helper.getMimeMessage().writeTo(byteArrayOutputStream); String str = byteArrayOutputStream.toString("UTF-8"); assertNotNull(str); // html and text present in mail message assertTrue(str.contains("Bender lives in theme park with blackjack poetess")); assertTrue(str.contains("<h2>Bender</h2> lives in theme park with:<br> blackjack<br>poetess<br>")); }
From source file:org.yes.cart.service.mail.impl.MailComposerImplTest.java
/** * Text template only//from www . j a va2 s .c o m */ @Test public void testComposeMimeMessageInternalTextVersionOnly() throws MessagingException, IOException, ClassNotFoundException { // of course you would use DI in any real-world cases JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("localhost"); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); String textTemplate = "$name lives in theme park with <% with.each{ out.print(it + ' ');}%>"; String htmlTemplate = null; MailComposerImpl mailComposer = new MailComposerImpl(null); mailComposer.composeMessage(helper, textTemplate, htmlTemplate, Collections.EMPTY_LIST, "SHOP10", "en", "test", createModel()); assertTrue(helper.isMultipart()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); helper.getMimeMessage().writeTo(byteArrayOutputStream); String str = byteArrayOutputStream.toString("UTF-8"); assertNotNull(str); // html and text present in mail message assertTrue(str.contains("Bender lives in theme park with blackjack poetess")); }