List of usage examples for javax.mail.internet MimeMessage getContent
@Override public Object getContent() throws IOException, MessagingException
From source file:org.apache.oozie.action.email.TestEmailActionExecutor.java
public void testHDFSFileAttachment() throws Exception { String file1 = "file1"; Path path1 = new Path(getFsTestCaseDir(), file1); String content1 = "this is attachment content in file1"; String file2 = "file2"; Path path2 = new Path(getFsTestCaseDir(), file2); String content2 = "this is attachment content in file2"; FileSystem fs = getFileSystem(); Writer writer = new OutputStreamWriter(fs.create(path1, true)); writer.write(content1);/*from w ww .j a v a 2 s . c om*/ writer.close(); writer = new OutputStreamWriter(fs.create(path2, true)); writer.write(content2); writer.close(); StringBuilder tag = new StringBuilder(); tag.append(path1.toString()).append(",").append(path2.toString()); assertAttachment(tag.toString(), 2, content1, content2); //test case when email attachment support set to false ConfigurationService.setBoolean(EmailActionExecutor.EMAIL_ATTACHMENT_ENABLED, false); sendAndReceiveEmail(tag.toString()); MimeMessage retMeg = server.getReceivedMessages()[1]; String msgBody = GreenMailUtil.getBody(retMeg); assertEquals(msgBody.indexOf("This is a test mail"), 0); assertNotSame(msgBody.indexOf(EmailActionExecutor.EMAIL_ATTACHMENT_ERROR_MSG), -1); // email content is not multi-part since not attaching files assertFalse(retMeg.getContent() instanceof Multipart); assertTrue(retMeg.getContentType().contains("text/plain")); }
From source file:org.apache.oozie.action.email.TestEmailActionExecutor.java
private void assertAttachment(String attachtag, int attachCount, String content1, String content2) throws Exception { sendAndReceiveEmail(attachtag);// ww w. j av a 2 s . c o m MimeMessage retMeg = server.getReceivedMessages()[0]; Multipart retParts = (Multipart) (retMeg.getContent()); int numAttach = 0; for (int i = 0; i < retParts.getCount(); i++) { BodyPart bp = retParts.getBodyPart(i); String disp = bp.getDisposition(); String retValue = IOUtils.toString(bp.getInputStream()); if (disp != null && (disp.equals(BodyPart.ATTACHMENT))) { assertTrue(retValue.equals(content1) || retValue.equals(content2)); numAttach++; } else { assertEquals("This is a test mail", retValue); } } assertEquals(attachCount, numAttach); }
From source file:org.apache.synapse.transport.mail.MailUtils.java
public InputStream getInputStream(Object message) { try {/* www .j a v a 2 s . co m*/ if (message instanceof MimeMessage) { MimeMessage msg = (MimeMessage) message; if (msg.getContent() instanceof Multipart) { MimeBodyPart firstTextPart = null; Multipart mp = (Multipart) msg.getContent(); for (int i = 0; i < mp.getCount(); i++) { MimeBodyPart mbp = (MimeBodyPart) mp.getBodyPart(i); String contType = mbp.getContentType(); if (contType != null && (contType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE) != -1 || contType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE) != -1)) { // this part is a SOAP 11 or 12 payload, treat this as the message return mbp.getInputStream(); } else if (mbp == null && contType.indexOf("plain/text") != -1) { firstTextPart = mbp; } } // if a soap 11 or soap12 payload was not found, treat first text part as message return firstTextPart.getInputStream(); } else { return ((Message) message).getInputStream(); } } } catch (Exception e) { handleException("Error creating an input stream to : " + ((Message) message).getMessageNumber(), e); } return null; }
From source file:org.asqatasun.emailsender.EmailSender.java
/** * * @param emailFrom/*from w w w .ja va 2s . c om*/ * @param emailToSet * @param emailBccSet (can be null) * @param replyTo (can be null) * @param emailSubject * @param emailContent */ public void sendEmail(String emailFrom, Set<String> emailToSet, Set<String> emailBccSet, String replyTo, String emailSubject, String emailContent) { boolean debug = false; // Set the host smtp address Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // create some properties and get the default Session Session session = Session.getInstance(props); session.setDebug(debug); try { Transport t = session.getTransport("smtp"); t.connect(smtpHost, userName, password); // create a message MimeMessage msg = new MimeMessage(session); // set the from and to address InternetAddress addressFrom; try { // Used default from address is passed one is null or empty or // blank addressFrom = (StringUtils.isNotBlank(emailFrom)) ? new InternetAddress(emailFrom) : new InternetAddress(from); msg.setFrom(addressFrom); Address[] recipients = new InternetAddress[emailToSet.size()]; int i = 0; for (String emailTo : emailToSet) { recipients[i] = new InternetAddress(emailTo); i++; } msg.setRecipients(Message.RecipientType.TO, recipients); if (CollectionUtils.isNotEmpty(emailBccSet)) { Address[] bccRecipients = new InternetAddress[emailBccSet.size()]; i = 0; for (String emailBcc : emailBccSet) { bccRecipients[i] = new InternetAddress(emailBcc); i++; } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } if (StringUtils.isNotBlank(replyTo)) { Address[] replyToRecipients = { new InternetAddress(replyTo) }; msg.setReplyTo(replyToRecipients); } // Setting the Subject msg.setSubject(emailSubject, CHARSET_KEY); // Setting content and charset (warning: both declarations of // charset are needed) msg.setHeader(CONTENT_TYPE_KEY, FULL_CHARSET_KEY); LOGGER.debug("emailContent " + emailContent); msg.setContent(emailContent, FULL_CHARSET_KEY); try { LOGGER.debug("emailContent from message object " + msg.getContent().toString()); } catch (IOException ex) { LOGGER.error(ex.getMessage()); } catch (MessagingException ex) { LOGGER.error(ex.getMessage()); } for (Address addr : msg.getAllRecipients()) { LOGGER.debug("addr " + addr); } t.sendMessage(msg, msg.getAllRecipients()); } catch (AddressException ex) { LOGGER.warn("AddressException " + ex.getMessage()); LOGGER.warn("AddressException " + ex.getStackTrace()); } } catch (NoSuchProviderException e) { LOGGER.warn("NoSuchProviderException " + e.getMessage()); LOGGER.warn("NoSuchProviderException " + e.getStackTrace()); } catch (MessagingException e) { LOGGER.warn("MessagingException " + e.getMessage()); LOGGER.warn("MessagingException " + e.getStackTrace()); } }
From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java
@Test public void sendEmailWithPlainMessage() throws Exception { final Map<String, Object> parameters = getBasicSettings(); parameters.put("message", PLAINMESSAGE); parameters.put("from", ADDRESSMARK); executeConnector(parameters);// www .jav a2 s . co m final List<WiserMessage> messages = server.getMessages(); assertEquals(1, messages.size()); final WiserMessage message = messages.get(0); assertEquals(ADDRESSMARK, message.getEnvelopeSender()); assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver()); final MimeMessage mime = message.getMimeMessage(); assertEquals(SUBJECT, mime.getSubject()); assertTrue(mime.getContentType().contains(TEXT_PLAIN)); assertEquals(PLAINMESSAGE, mime.getContent()); }
From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java
@Test public void sendEmailWithHtmlMessage() throws Exception { final Map<String, Object> parameters = getBasicSettings(); parameters.put("html", true); parameters.put("message", HTMLMESSAGE); parameters.put("from", ADDRESSMARK); executeConnector(parameters);/* w w w. j a v a 2s . c o m*/ final List<WiserMessage> messages = server.getMessages(); assertEquals(1, messages.size()); final WiserMessage message = messages.get(0); assertEquals(ADDRESSMARK, message.getEnvelopeSender()); assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver()); final MimeMessage mime = message.getMimeMessage(); assertEquals(SUBJECT, mime.getSubject()); assertTrue(mime.getContentType().contains(TEXT_HTML)); assertEquals(HTMLMESSAGE, mime.getContent()); }
From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java
@Test public void sendCyrillicEmail() throws Exception { final Map<String, Object> parameters = getBasicSettings(); parameters.put("subject", CYRILLIC_SUBJECT); parameters.put("message", CYRILLIC_MESSAGE); executeConnector(parameters);/*from ww w. j a va2 s . co m*/ final List<WiserMessage> messages = server.getMessages(); assertEquals(1, messages.size()); final WiserMessage message = messages.get(0); assertNotNull(message.getEnvelopeSender()); assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver()); final MimeMessage mime = message.getMimeMessage(); assertEquals(CYRILLIC_SUBJECT, mime.getSubject()); assertTrue(mime.getContentType().contains(TEXT_PLAIN)); assertEquals(CYRILLIC_MESSAGE, mime.getContent()); }
From source file:org.bonitasoft.connectors.email.test.EmailConnectorTest.java
@Test public void sendBadEncodingCyrillicEmail() throws Exception { final Map<String, Object> parameters = getBasicSettings(); parameters.put("charset", "iso-8859-1"); parameters.put("message", CYRILLIC_MESSAGE); executeConnector(parameters);/*from w w w. ja va2 s. com*/ final List<WiserMessage> messages = server.getMessages(); assertEquals(1, messages.size()); final WiserMessage message = messages.get(0); assertNotNull(message.getEnvelopeSender()); assertEquals(ADDRESSJOHN, message.getEnvelopeReceiver()); final MimeMessage mime = message.getMimeMessage(); assertEquals(SUBJECT, mime.getSubject()); assertTrue(mime.getContentType().contains(TEXT_PLAIN)); assertEquals("? ? ?", mime.getContent()); }
From source file:org.broadleafcommerce.common.email.service.LoggingMailSender.java
@Override public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException { for (MimeMessagePreparator preparator : mimeMessagePreparators) { try {//from www. j a v a 2s. co m MimeMessage mimeMessage = createMimeMessage(); preparator.prepare(mimeMessage); LOG.info("\"Sending\" email: "); if (mimeMessage.getContent() instanceof MimeMultipart) { MimeMultipart msg = (MimeMultipart) mimeMessage.getContent(); DataHandler dh = msg.getBodyPart(0).getDataHandler(); ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); dh.writeTo(baos); } catch (Exception e) { // Do nothing } finally { try { baos.close(); } catch (Exception e) { LOG.error("Couldn't close byte array output stream"); } } } else { LOG.info(mimeMessage.getContent()); } } catch (Exception e) { LOG.error("Could not create message", e); } } }
From source file:org.craftercms.commons.mail.impl.EmailFactoryImplTest.java
@Test public void testGetEmailWithBodyParam() throws Exception { EmailImpl email = (EmailImpl) emailFactory.getEmail(FROM, TO, CC, BCC, REPLY_TO, SUBJECT, BODY, false); assertNotNull(email);/*from ww w .j ava 2s . c o m*/ MimeMessage msg = email.message; assertArrayEquals(InternetAddress.parse(FROM), msg.getFrom()); assertArrayEquals(InternetAddress.parse(StringUtils.join(TO)), msg.getRecipients(Message.RecipientType.TO)); assertArrayEquals(InternetAddress.parse(StringUtils.join(CC)), msg.getRecipients(Message.RecipientType.CC)); assertArrayEquals(InternetAddress.parse(StringUtils.join(BCC)), msg.getRecipients(Message.RecipientType.BCC)); assertArrayEquals(InternetAddress.parse(REPLY_TO), msg.getReplyTo()); assertEquals(SUBJECT, msg.getSubject()); assertEquals(BODY, msg.getContent()); }