javax.mail.Multipart Java Examples

The following examples show how to use javax.mail.Multipart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MailUtil.java    From document-management-software with GNU Lesser General Public License v3.0 7 votes vote down vote up
private static void addAttachments(BodyPart p, EMail email, boolean extractAttachmentContent)
		throws UnsupportedEncodingException, MessagingException, IOException {
	if (p.isMimeType("multipart/*")) {
		Multipart mp = (Multipart) p.getContent();
		int count = mp.getCount();
		for (int i = 1; i < count; i++) {
			BodyPart bp = mp.getBodyPart(i);
			if (bp.getFileName() != null && extractAttachmentContent) {
				addAttachment(bp, email);
			} else if (bp.isMimeType("multipart/*")) {
				addAttachments(bp, email, extractAttachmentContent);
			}
		}
	} else if (extractAttachmentContent && StringUtils.isNotEmpty(p.getFileName())) {
		addAttachment(p, email);
	}
}
 
Example #2
Source File: MailServiceIntTest.java    From 21-points with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
Example #3
Source File: MailServiceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #4
Source File: MailServiceIntTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
Example #5
Source File: MailServiceIntTest.java    From e-commerce-microservice with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #6
Source File: MailServiceIntTest.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
Example #7
Source File: MailServiceIntTest.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #8
Source File: MailServiceIT.java    From alchemy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
Example #9
Source File: MailServiceIntTest.java    From TeamDojo with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #10
Source File: MailConnection.java    From hop with Apache License 2.0 6 votes vote down vote up
/**
 * Save attached files to a folder.
 *
 * @param foldername the target foldername
 * @param pattern    regular expression to filter on files
 * @throws HopException
 */
public void saveAttachedFiles( String foldername, Pattern pattern ) throws HopException {
  Object content = null;
  try {
    content = getMessage().getContent();
    if ( content instanceof Multipart ) {
      handleMultipart( foldername, (Multipart) content, pattern );
    }
  } catch ( Exception e ) {
    throw new HopException( BaseMessages.getString( PKG, "MailConnection.Error.SavingAttachedFiles", ""
      + this.message.getMessageNumber(), foldername ), e );
  } finally {
    if ( content != null ) {
      content = null;
    }
  }
}
 
Example #11
Source File: multipart_mixed.java    From FairEmail with GNU General Public License v3.0 6 votes vote down vote up
/**
    * Write the object to the output stream, using the specific MIME type.
    */
   @Override
   public void writeTo(Object obj, String mimeType, OutputStream os) 
		throws IOException {
if (!(obj instanceof Multipart))
    throw new IOException("\"" + getDataFlavors()[0].getMimeType() +
	"\" DataContentHandler requires Multipart object, " +
	"was given object of type " + obj.getClass().toString() +
	"; obj.cl " + obj.getClass().getClassLoader() +
	", Multipart.cl " + Multipart.class.getClassLoader());

try {
    ((Multipart)obj).writeTo(os);
} catch (MessagingException e) {
    IOException ioex =
	new IOException("Exception writing Multipart");
    ioex.initCause(e);
    throw ioex;
}
   }
 
Example #12
Source File: TextCalendarBodyToAttachmentTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
public void contentDispositionOfAttachmentShouldBeOverwrittenWhenOriginalMessageHasContentDisposition() throws Exception {
    String messageContent = "Content-type: text/calendar; method=REPLY; charset=UTF-8\n" +
        "Content-Disposition: inline\n" +
        "\n" +
        "BEGIN:VCALENDAR\n" +
        "END:VEVENT\n" +
        "END:VCALENDAR";
    MimeMessage message = MimeMessageUtil.mimeMessageFromString(messageContent);

    Mail mail = FakeMail.builder()
        .name("name")
        .mimeMessage(message)
        .build();

    mailet.service(mail);

    Multipart multipart = (Multipart)mail.getMessage().getContent();

    int firstBodyPartIndex = 0;
    BodyPart firstBodyPart = multipart.getBodyPart(firstBodyPartIndex);
    assertThat(firstBodyPart.getHeader("Content-Disposition")).containsExactly("attachment");
}
 
Example #13
Source File: MailServiceIntTest.java    From cubeai with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #14
Source File: EmailReader.java    From baleen with Apache License 2.0 6 votes vote down vote up
private String getContent(Message msg) throws IOException, MessagingException {
  Object messageContentObject = msg.getContent();
  if (messageContentObject instanceof Multipart) {
    Multipart multipart = (Multipart) msg.getContent();

    // Loop over the parts of the email
    for (int i = 0; i < multipart.getCount(); i++) {
      // Retrieve the next part
      Part part = multipart.getBodyPart(i);

      if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())
          && StringUtils.isBlank(part.getFileName())) {
        return part.getContent().toString();
      }
    }
  } else {
    return msg.getContent().toString().trim();
  }

  return "";
}
 
Example #15
Source File: MailServiceIntTest.java    From Full-Stack-Development-with-JHipster with MIT License 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #16
Source File: ICSSanitizer.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public void service(Mail mail) {
    try {
        MimeMessage mimeMessage = mail.getMessage();

        if (mimeMessage.getContent() instanceof Multipart) {
            Multipart multipart = (Multipart) mimeMessage.getContent();

            if (needsSanitizing(multipart)) {
                mimeMessage.setContent(sanitize(multipart));
                mimeMessage.saveChanges();
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Could not sanitize {}", mail.getName(), e);
    }
}
 
Example #17
Source File: MailServiceIntTest.java    From jhipster-microservices-example with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject","testContent", true, false);
    verify(javaMailSender).send((MimeMessage) messageCaptor.capture());
    MimeMessage message = (MimeMessage) messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart)((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #18
Source File: MailServiceIT.java    From jhipster-online with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("JHipster Online <test@localhost>");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #19
Source File: MailServiceIT.java    From jhipster-online with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("JHipster Online <test@localhost>");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
Example #20
Source File: MailReader.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Get the content of a mail message.
 * 
 * @param message
 *            the mail message
 * @return the content of the mail message
 */
private String getMessageContent(Message message) throws MessagingException {
    try {
        Object content = message.getContent();
        if (content instanceof Multipart) {
            StringBuffer messageContent = new StringBuffer();
            Multipart multipart = (Multipart) content;
            for (int i = 0; i < multipart.getCount(); i++) {
                Part part = multipart.getBodyPart(i);
                if (part.isMimeType("text/plain")) {
                    messageContent.append(part.getContent().toString());
                }
            }
            return messageContent.toString();
        }
        return content.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}
 
Example #21
Source File: EMLTransformer.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Find "text" parts of message recursively and appends it to sb StringBuilder
 * 
 * @param multipart Multipart to process
 * @param sb StringBuilder 
 * @throws MessagingException
 * @throws IOException
 */
private void processMultiPart(Multipart multipart, StringBuilder sb) throws MessagingException, IOException
{
    boolean isAlternativeMultipart = multipart.getContentType().contains(MimetypeMap.MIMETYPE_MULTIPART_ALTERNATIVE);
    if (isAlternativeMultipart)
    {
        processAlternativeMultipart(multipart, sb);
    }
    else
    {
        for (int i = 0, n = multipart.getCount(); i < n; i++)
        {
            Part part = multipart.getBodyPart(i);
            if (part.getContent() instanceof Multipart)
            {
                processMultiPart((Multipart) part.getContent(), sb);
            }
            else
            {
                processPart(part, sb);
            }
        }
    }
}
 
Example #22
Source File: Job51ResumeParser.java    From job with MIT License 6 votes vote down vote up
protected Document parse2HtmlAsMail(File file) throws Exception {
  InputStream in = new FileInputStream(file);

  Session mailSession = Session.getDefaultInstance(System.getProperties(), null);

  MimeMessage msg = new MimeMessage(mailSession, in);
  
  Multipart part = (Multipart) msg.getContent();
  String html = null;
  for(int i = 0; i < part.getCount(); i++) {
    html = parseHtml(part.getBodyPart(i));
    if(html != null) {
      break;
    }
  }
  in.close();
  return html == null ? null : Jsoup.parse(html);
}
 
Example #23
Source File: MailServiceIntTest.java    From ehcache3-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartHtmlEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, true);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/html;charset=UTF-8");
}
 
Example #24
Source File: MailServiceIntTest.java    From ehcache3-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendMultipartEmail() throws Exception {
    mailService.sendEmail("[email protected]", "testSubject", "testContent", true, false);
    verify(javaMailSender).send(messageCaptor.capture());
    MimeMessage message = messageCaptor.getValue();
    MimeMultipart mp = (MimeMultipart) message.getContent();
    MimeBodyPart part = (MimeBodyPart) ((MimeMultipart) mp.getBodyPart(0).getContent()).getBodyPart(0);
    ByteArrayOutputStream aos = new ByteArrayOutputStream();
    part.writeTo(aos);
    assertThat(message.getSubject()).isEqualTo("testSubject");
    assertThat(message.getAllRecipients()[0].toString()).isEqualTo("[email protected]");
    assertThat(message.getFrom()[0].toString()).isEqualTo("test@localhost");
    assertThat(message.getContent()).isInstanceOf(Multipart.class);
    assertThat(aos.toString()).isEqualTo("\r\ntestContent");
    assertThat(part.getDataHandler().getContentType()).isEqualTo("text/plain; charset=UTF-8");
}
 
Example #25
Source File: Mailer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private static Multipart getMessagePart() throws MessagingException, IOException {
    Multipart multipart = new MimeMultipart();
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(getVal("msg.Body"));
    multipart.addBodyPart(messageBodyPart);
    if (getBoolVal("attach.reports")) {
        LOG.info("Attaching Reports as zip");
        multipart.addBodyPart(getReportsBodyPart());
    } else {
        if (getBoolVal("attach.standaloneHtml")) {
            multipart.addBodyPart(getStandaloneHtmlBodyPart());
        }
        if (getBoolVal("attach.console")) {
            multipart.addBodyPart(getConsoleBodyPart());
        }
        if (getBoolVal("attach.screenshots")) {
            multipart.addBodyPart(getScreenShotsBodyPart());
        }
    }
    messageBodyPart.setContent(getVal("msg.Body")
            .concat("\n\n\n")
            .concat(MailComponent.getHTMLBody()), "text/html");
    return multipart;
}
 
Example #26
Source File: MimeMessageWrapper.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
public synchronized void setMessage(MimeMessage message) {
    if (message != null) {
        // serialize the message
        this.message = message;
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            message.writeTo(baos);
            baos.flush();
            serializedBytes = baos.toByteArray();
            this.contentType = message.getContentType();

            // see if this is a multi-part message
            Object content = message.getContent();
            if (content instanceof Multipart) {
                Multipart mp = (Multipart) content;
                this.parts = mp.getCount();
            } else {
                this.parts = 0;
            }
        } catch (IOException | MessagingException e) {
            Debug.logError(e, module);
        }
    }
}
 
Example #27
Source File: Mail.java    From camunda-bpm-mail with Apache License 2.0 6 votes vote down vote up
protected static void processMessageContent(Message message, Mail mail) throws MessagingException, IOException {

    if (isMultipartMessage(message)) {
      Multipart multipart = (Multipart) message.getContent();

      int numberOfParts = multipart.getCount();
      for (int partCount = 0; partCount < numberOfParts; partCount++) {
        BodyPart bodyPart = multipart.getBodyPart(partCount);

        processMessagePartContent(bodyPart, mail);
      }

    } else {
      processMessagePartContent(message, mail);
    }
  }
 
Example #28
Source File: ServiceMcaCondition.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
private List<String> getBodyText(Part part) throws MessagingException, IOException {
    Object c = part.getContent();
    if (c instanceof String) {
        return UtilMisc.toList((String) c);
    } else if (c instanceof Multipart) {
        List<String> textContent = new ArrayList<>(); // SCIPIO: switched to ArrayList
        int count = ((Multipart) c).getCount();
        for (int i = 0; i < count; i++) {
            BodyPart bp = ((Multipart) c).getBodyPart(i);
            textContent.addAll(this.getBodyText(bp));
        }
        return textContent;
    } else {
        return new ArrayList<>(); // SCIPIO: switched to ArrayList
    }
}
 
Example #29
Source File: GMailClient.java    From blynk-server with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void sendHtmlWithAttachment(String to, String subj, String body,
                                   QrHolder[] attachmentData) throws Exception {
    MimeMessage message = new MimeMessage(session);
    message.setFrom(from);
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
    message.setSubject(subj, "UTF-8");

    Multipart multipart = new MimeMultipart();

    MimeBodyPart bodyMessagePart = new MimeBodyPart();
    bodyMessagePart.setContent(body, TEXT_HTML_CHARSET_UTF_8);

    multipart.addBodyPart(bodyMessagePart);

    attachQRs(multipart, attachmentData);
    attachCSV(multipart, attachmentData);

    message.setContent(multipart);

    Transport.send(message);

    log.trace("Mail to {} was sent. Subj : {}, body : {}", to, subj, body);
}
 
Example #30
Source File: ArdulinkMailMessageCountListener.java    From Ardulink-1 with Apache License 2.0 6 votes vote down vote up
private String getContent(Message message) throws IOException, MessagingException {

		String retvalue = "";
		
		Object msgContent = message.getContent();
		if(msgContent instanceof Multipart) {
			Multipart multipart = (Multipart)message.getContent();
			int count = multipart.getCount();
			for(int i = 0; i < count; i++) {
				BodyPart part = multipart.getBodyPart(i);
				if(part.isMimeType("text/plain")) {
					retvalue += "Part" + i + ": " + part.getContent().toString();
				}
			}
		} else {
			retvalue = msgContent.toString();
		}
		
		return retvalue;
	}