Example usage for javax.mail.internet MimeMessage getSubject

List of usage examples for javax.mail.internet MimeMessage getSubject

Introduction

In this page you can find the example usage for javax.mail.internet MimeMessage getSubject.

Prototype

@Override
public String getSubject() throws MessagingException 

Source Link

Document

Returns the value of the "Subject" header field.

Usage

From source file:ca.airspeed.demo.testingemail.EmailServiceTest.java

@Test
public void testWithAttachment() throws Exception {
    // Given:/*  w  ww.j a va 2 s  .  co m*/
    EmailService service = makeALocalMailer();

    InternetAddress expectedTo = new InternetAddress("Indiana.Jones@domain.com", "Indiana Jones");
    String expectedSubject = "This is a Test Email";
    String expectedTextBody = "This is a test with a PDF attachment.";
    List<FileSystemResource> filesToAttach = new ArrayList<FileSystemResource>();
    filesToAttach.add(
            new FileSystemResource(this.getClass().getClassLoader().getResource("HelloWorld.pdf").getFile()));

    // When:
    service.sendWithAttachments(expectedTo, expectedSubject, expectedTextBody, filesToAttach);

    // Then:
    List<WiserMessage> messages = wiser.getMessages();
    assertEquals("Number of messages sent;", 1, messages.size());
    WiserMessage message = messages.get(0);
    MimeMessage mimeMessage = message.getMimeMessage();

    assertEquals("Subject;", expectedSubject, mimeMessage.getSubject());

    MimeMultipart body = ((MimeMultipart) mimeMessage.getContent());
    assertEquals("Number of MIME Parts in the body;", 2, body.getCount());

    MimeBodyPart attachment = ((MimeBodyPart) body.getBodyPart(1));
    assertTrue("Attachment MIME Type should be application/pdf", attachment.isMimeType("application/pdf"));
    assertEquals("Attachment filename;", "HelloWorld.pdf", attachment.getFileName());
    assertTrue("No content found in the attachment.", isNotBlank(attachment.getContent().toString()));
}

From source file:mitm.application.djigzo.james.matchers.SubjectTrigger.java

protected void removePattern(Mail mail, Matcher matcher) throws MessagingException, IOException {
    MimeMessage message = mail.getMessage();

    String subject = message.getSubject();

    /*/*w  w w  .j av a 2 s .c  o  m*/
     * Store the subject in the Mail attributes before we remove the pattern from the subject. 
     * This will allow other mailets/matchers to get the original subject.
     * 
     * TODO: make the attribute under which the subject should be set, a parameter of the
     * matcher.
     */
    DjigzoMailAttributes mailAttributes = new DjigzoMailAttributesImpl(mail);

    /*
     * Only set the original subject if it has not yet been set
     */
    if (mailAttributes.getOriginalSubject() == null) {
        mailAttributes.setOriginalSubject(subject);
    }

    subject = removePatternFromSubject(subject, matcher, 1);

    message.setSubject(subject);
}

From source file:eu.openanalytics.rsb.component.EmailDepositHandlerTestCase.java

@Test
public void handleResult() throws Exception {
    final Map<String, Serializable> meta = new HashMap<String, Serializable>();
    meta.put(EmailDepositHandler.EMAIL_ADDRESSEE_META_NAME, "addressee@test.com");
    meta.put(EmailDepositHandler.EMAIL_REPLY_TO_META_NAME, "replyto@test.com");
    meta.put(EmailDepositHandler.EMAIL_REPLY_CC_META_NAME, new String[] { "replyCC@test.com" });
    meta.put(EmailDepositHandler.EMAIL_SUBJECT_META_NAME, "subject");
    meta.put(EmailDepositHandler.EMAIL_BODY_META_NAME, "your result");

    final MultiFilesResult multiFilesResult = mock(MultiFilesResult.class);
    when(multiFilesResult.isSuccess()).thenReturn(true);
    when(multiFilesResult.getApplicationName()).thenReturn(TEST_APPLICATION_NAME);
    when(multiFilesResult.getTemporaryDirectory()).thenReturn(FileUtils.getTempDirectory());
    when(multiFilesResult.getMeta()).thenReturn(meta);
    final URL jobFakingAResult = Thread.currentThread().getContextClassLoader()
            .getResource("data/r-job-sample.zip");
    when(multiFilesResult.getPayload()).thenReturn(new File[] { new File(jobFakingAResult.toURI()) });

    when(mailSender.createMimeMessage()).thenReturn(new MimeMessage((Session) null));

    emailDepositHandler.handleResult(multiFilesResult);

    @SuppressWarnings("rawtypes")
    final ArgumentCaptor<Message> messageCaptor = ArgumentCaptor.forClass(Message.class);
    verify(outboundEmailChannel).send(messageCaptor.capture());

    @SuppressWarnings("unchecked")
    final Message<MimeMailMessage> message = messageCaptor.getValue();
    final MimeMessage mimeMessage = message.getPayload().getMimeMessage();
    assertThat(mimeMessage.getSubject(), is("RE: subject"));
}

From source file:com.meltmedia.cadmium.email.TestingMessageTransformer.java

public String newSubject(MimeMessage message) throws MessagingException {
    return getSubjectProperty() + message.getSubject();
}

From source file:org.ofbiz.common.CommonServices.java

public static Map<String, Object> mcaTest(DispatchContext dctx, Map<String, ?> context) {
    MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper");
    MimeMessage message = wrapper.getMessage();
    try {//from   w w  w .  j a v  a2 s  .c  o  m
        if (message.getAllRecipients() != null) {
            Debug.log("To: " + UtilMisc.toListArray(message.getAllRecipients()), module);
        }
        if (message.getFrom() != null) {
            Debug.log("From: " + UtilMisc.toListArray(message.getFrom()), module);
        }
        Debug.log("Subject: " + message.getSubject(), module);
        if (message.getSentDate() != null) {
            Debug.log("Sent: " + message.getSentDate().toString(), module);
        }
        if (message.getReceivedDate() != null) {
            Debug.log("Received: " + message.getReceivedDate().toString(), module);
        }
    } catch (Exception e) {
        Debug.logError(e, module);
    }
    return ServiceUtil.returnSuccess();
}

From source file:com.example.securelogin.domain.service.mail.PasswordReissueMailSharedServiceImpl.java

@Override
public List<ReceivedMail> getReceivedMessages() {
    List<ReceivedMail> mails = new ArrayList<>();
    MimeMessage[] messages = greenMailBean.getReceivedMessages();
    for (MimeMessage message : messages) {
        ReceivedMail mail = new ReceivedMail();
        try {//from  ww  w. j a v a2s  .  c  o m
            mail.setTo(message.getFrom()[0].toString());
            mail.setFrom(message.getRecipients(Message.RecipientType.TO)[0].toString());
            mail.setSubject(message.getSubject());
            mail.setText(message.getContent().toString().replace("\r\n", ""));
            mails.add(mail);
        } catch (MessagingException e) {
            throw new SystemException(MessageKeys.E_SL_FW_9001, e);
        } catch (IOException e) {
            throw new SystemException(MessageKeys.E_SL_FW_9001, e);
        }

    }
    return mails;
}

From source file:com.silverpeas.mailinglist.model.TestMailingListComponent.java

protected void checkSimpleEmail(String address, String subject) throws Exception {
    Mailbox inbox = Mailbox.get(address);
    assertNotNull(inbox);//w ww  .j a  v a 2 s .co m
    assertEquals("No message for " + address, 1, inbox.size());
    MimeMessage alert = (MimeMessage) inbox.iterator().next();
    assertNotNull(alert);
    assertEquals(subject, alert.getSubject());
    assertEquals("text/plain; charset=\"UTF-8\"", alert.getContentType());
    assertEquals(textEmailContent, (String) alert.getContent());
}

From source file:com.silverpeas.mailinglist.service.notification.AdvancedNotificationHelperTest.java

protected void checkSimpleEmail(String address, String subject) throws Exception {
    Mailbox inbox = Mailbox.get(address);
    assertThat(inbox, is(notNullValue()));
    assertThat("No message for " + address, inbox.size(), is(1));
    MimeMessage alert = (MimeMessage) inbox.iterator().next();
    assertThat(alert, is(notNullValue()));
    assertThat(alert.getSubject(), is(subject));
    assertThat((String) alert.getContent(), is(textEmailContent));
}

From source file:com.hmsinc.epicenter.surveillance.notification.MailingEventNotifierTest.java

@Test
public void shouldHaveCorrectSubject() throws MessagingException {
    MimeMessage message = (MimeMessage) sentMessages.get(0);
    assertEquals("Respiratory Anomaly in Pennsylvania", message.getSubject());
}

From source file:mitm.common.mail.repository.hibernate.MailRepositoryItemEntity.java

private void initSubject(MimeMessage message) {
    try {/*w ww  . j  a v  a2s. com*/
        try {
            subject = message.getSubject();
        } catch (MessagingException e) {
            /*
             * Fallback to raw headers
             */
            subject = message.getHeader("subject", ", ");
        }

        subject = StringUtils.abbreviate(subject, SUBJECT_MAX_LENGTH);
    } catch (MessagingException e) {
        logger.debug("Error getting subject.", e);
    }
}