Java Code Examples for javax.mail.internet.MimeMessage#getAllHeaderLines()

The following examples show how to use javax.mail.internet.MimeMessage#getAllHeaderLines() . 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: MailHandlerServlet.java    From appengine-tck with Apache License 2.0 6 votes vote down vote up
/**
 * Stores subject and headers into memcache for test to confirm mail delivery.
 */
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Session session = Session.getDefaultInstance(new Properties(), null);
    try {
        MimeMessage message = new MimeMessage(session, req.getInputStream());
        MimeProperties mp = new MimeProperties(message);

        List<String> headers = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        Enumeration e = message.getAllHeaderLines();
        while (e.hasMoreElements()) {
            String headerLine = (String) e.nextElement();
            headers.add(headerLine);
            sb.append("\n").append(headerLine);
        }
        log.info("HEADERS: " + sb.toString());

        mp.headers = headers.toString();
        TestBase.putTempData(mp);
    } catch (MessagingException me) {
        throw new IOException("Error while processing email.", me);
    }
}
 
Example 2
Source File: AbstractSign.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method for obtaining a string representation of the Message's headers
 * @param message The message to extract the headers from.
 * @return The string containing the headers.
 */
protected final String getMessageHeaders(MimeMessage message) throws MessagingException {
    Enumeration<String> heads = message.getAllHeaderLines();
    StringBuilder headBuffer = new StringBuilder(1024);
    while (heads.hasMoreElements()) {
        headBuffer.append(heads.nextElement()).append("\r\n");
    }
    return headBuffer.toString();
}
 
Example 3
Source File: MimeMessageTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaderOrder() throws Exception {
    MimeMessage message = getSimpleMessage();
    message.setHeader(RFC2822Headers.RETURN_PATH, "<[email protected]>");
    Enumeration<String> h = message.getAllHeaderLines();

    assertThat("Return-Path: <[email protected]>").isEqualTo(h.nextElement());
    LifecycleUtil.dispose(message);
}
 
Example 4
Source File: MimeMessageWrapperTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplaceReturnPathOnBadMessage() throws Exception {
    MimeMessage message = getMessageWithBadReturnPath();
    message.setHeader(RFC2822Headers.RETURN_PATH, "<[email protected]>");
    Enumeration<String> e = message.getMatchingHeaderLines(new String[]{"Return-Path"});
    assertThat(e.nextElement()).isEqualTo("Return-Path: <[email protected]>");
    assertThat(e.hasMoreElements()).isFalse();
    Enumeration<String> h = message.getAllHeaderLines();
    assertThat(h.nextElement()).isEqualTo("Return-Path: <[email protected]>");
    assertThat(h.nextElement().startsWith("Return-Path:")).isFalse();
    LifecycleUtil.dispose(message);
}
 
Example 5
Source File: MimeMessageWrapperTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddReturnPathOnBadMessage() throws Exception {
    MimeMessage message = getMessageWithBadReturnPath();
    message.addHeader(RFC2822Headers.RETURN_PATH, "<[email protected]>");
    // test that we have now 2 return-paths
    Enumeration<String> e = message.getMatchingHeaderLines(new String[]{"Return-Path"});
    assertThat(e.nextElement()).isEqualTo("Return-Path: <[email protected]>");
    assertThat(e.nextElement()).isEqualTo("Return-Path: <[email protected]>");
    // test that return-path is the first line
    Enumeration<String> h = message.getAllHeaderLines();
    assertThat(h.nextElement()).isEqualTo("Return-Path: <[email protected]>");
    LifecycleUtil.dispose(message);
}
 
Example 6
Source File: AbstractSign.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * Service does the hard work, and signs
 *
 * @param mail the mail to sign
 * @throws MessagingException if a problem arises signing the mail
 */
@Override
public void service(Mail mail) throws MessagingException {
    
    try {
        if (!isOkToSign(mail)) {
            return;
        }
        
        MimeBodyPart wrapperBodyPart = getWrapperBodyPart(mail);
        
        MimeMessage originalMessage = mail.getMessage();
        
        // do it
        MimeMultipart signedMimeMultipart;
        if (wrapperBodyPart != null) {
            signedMimeMultipart = getKeyHolder().generate(wrapperBodyPart);
        } else {
            signedMimeMultipart = getKeyHolder().generate(originalMessage);
        }
        
        MimeMessage newMessage = new MimeMessage(Session.getDefaultInstance(System.getProperties(),
        null));
        Enumeration<String> headerEnum = originalMessage.getAllHeaderLines();
        while (headerEnum.hasMoreElements()) {
            newMessage.addHeaderLine(headerEnum.nextElement());
        }
        
        newMessage.setSender(new InternetAddress(getKeyHolder().getSignerAddress(), getSignerName()));
  
        if (isRebuildFrom()) {
            // builds a new "mixed" "From:" header
            InternetAddress modifiedFromIA = new InternetAddress(getKeyHolder().getSignerAddress(), mail.getMaybeSender().asString());
            newMessage.setFrom(modifiedFromIA);
            
            // if the original "ReplyTo:" header is missing sets it to the original "From:" header
            newMessage.setReplyTo(originalMessage.getReplyTo());
        }
        
        newMessage.setContent(signedMimeMultipart, signedMimeMultipart.getContentType());
        String messageId = originalMessage.getMessageID();
        newMessage.saveChanges();
        if (messageId != null) {
            newMessage.setHeader(RFC2822Headers.MESSAGE_ID, messageId);
        }
        
        mail.setMessage(newMessage);
        
        // marks this mail as server-signed
        mail.setAttribute(new Attribute(SMIMEAttributeNames.SMIME_SIGNING_MAILET, AttributeValue.of(this.getClass().getName())));
        // it is valid for us by definition (signed here by us)
        mail.setAttribute(new Attribute(SMIMEAttributeNames.SMIME_SIGNATURE_VALIDITY, AttributeValue.of("valid")));
        
        // saves the trusted server signer address
        // warning: should be same as the mail address in the certificate, but it is not guaranteed
        mail.setAttribute(new Attribute(SMIMEAttributeNames.SMIME_SIGNER_ADDRESS, AttributeValue.of(getKeyHolder().getSignerAddress())));
        
        if (isDebug()) {
            LOGGER.debug("Message signed, reverse-path: {}, Id: {}", mail.getMaybeSender().asString(), messageId);
        }
        
    } catch (MessagingException me) {
        LOGGER.error("MessagingException found - could not sign!", me);
        throw me;
    } catch (Exception e) {
        LOGGER.error("Exception found", e);
        throw new MessagingException("Exception thrown - could not sign!", e);
    }
    
}