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

The following examples show how to use javax.mail.internet.MimeMessage#removeHeader() . 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: RemoveMimeHeader.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Override
public void service(Mail mail) throws MessagingException {
    MimeMessage message = mail.getMessage();

    boolean hasHeadersToRemove = message.getMatchingHeaderLines(headers.toArray(String[]::new))
        .hasMoreElements();

    for (String header : headers) {
        message.removeHeader(header);
    }

    removeSpecific(mail);

    if (hasHeadersToRemove) {
        message.saveChanges();
    }
}
 
Example 2
Source File: ExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
protected void convertResentHeader(MimeMessage mimeMessage, String headerName) throws MessagingException {
    String[] resentHeader = mimeMessage.getHeader("Resent-" + headerName);
    if (resentHeader != null) {
        mimeMessage.removeHeader("Resent-" + headerName);
        mimeMessage.removeHeader(headerName);
        for (String value : resentHeader) {
            mimeMessage.addHeader(headerName, value);
        }
    }
}
 
Example 3
Source File: ExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Send message in reader to recipients.
 * Detect visible recipients in message body to determine bcc recipients
 *
 * @param rcptToRecipients recipients list
 * @param mimeMessage      mime message
 * @throws IOException        on error
 * @throws MessagingException on error
 */
public void sendMessage(List<String> rcptToRecipients, MimeMessage mimeMessage) throws IOException, MessagingException {
    // detect duplicate send command
    String messageId = mimeMessage.getMessageID();
    if (lastSentMessageId != null && lastSentMessageId.equals(messageId)) {
        LOGGER.debug("Dropping message id " + messageId + ": already sent");
        return;
    }
    lastSentMessageId = messageId;

    convertResentHeader(mimeMessage, "From");
    convertResentHeader(mimeMessage, "To");
    convertResentHeader(mimeMessage, "Cc");
    convertResentHeader(mimeMessage, "Bcc");
    convertResentHeader(mimeMessage, "Message-Id");

    // do not allow send as another user on Exchange 2003
    if ("Exchange2003".equals(serverVersion) || Settings.getBooleanProperty("davmail.smtpStripFrom", false)) {
        mimeMessage.removeHeader("From");
    }

    // remove visible recipients from list
    Set<String> visibleRecipients = new HashSet<>();
    List<InternetAddress> recipients = getAllRecipients(mimeMessage);
    for (InternetAddress address : recipients) {
        visibleRecipients.add((address.getAddress().toLowerCase()));
    }
    for (String recipient : rcptToRecipients) {
        if (!visibleRecipients.contains(recipient.toLowerCase())) {
            mimeMessage.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(recipient));
        }
    }
    sendMessage(mimeMessage);

}
 
Example 4
Source File: BayesianAnalysisFeeder.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void clearAllHeaders(MimeMessage message) throws javax.mail.MessagingException {
    Enumeration<Header> headers = message.getAllHeaders();

    while (headers.hasMoreElements()) {
        Header header = headers.nextElement();
        try {
            message.removeHeader(header.getName());
        } catch (javax.mail.MessagingException me) {
            LOGGER.error("Cannot remove header.", me);
        }
    }
    message.saveChanges();
}
 
Example 5
Source File: MailDispatcher.java    From james-project with Apache License 2.0 5 votes vote down vote up
private void restoreHeaders(MimeMessage mimeMessage, Map<String, List<String>> savedHeaders) throws MessagingException {
    for (Map.Entry<String, List<String>> header: savedHeaders.entrySet()) {
        String name = header.getKey();
        mimeMessage.removeHeader(name);
        for (String value: header.getValue()) {
            mimeMessage.addHeader(name, value);
        }
    }
}
 
Example 6
Source File: TestExtractEmailHeaders.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Test case added for NIFI-4326 for a potential NPE bug
 * if the email message contains no recipient header fields, ie,
 * TO, CC, BCC.
 */
@Test
public void testValidEmailWithNoRecipients() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractEmailHeaders());
    runner.setProperty(ExtractEmailHeaders.CAPTURED_HEADERS, "MIME-Version");

    MimeMessage simpleEmailMimeMessage = attachmentGenerator.SimpleEmailMimeMessage();

    simpleEmailMimeMessage.removeHeader("To");
    simpleEmailMimeMessage.removeHeader("Cc");
    simpleEmailMimeMessage.removeHeader("Bcc");

    ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
    try {
        simpleEmailMimeMessage.writeTo(messageBytes);
    } catch (IOException | MessagingException e) {
        e.printStackTrace();
    }

    runner.enqueue(messageBytes.toByteArray());
    runner.run();

    runner.assertTransferCount(ExtractEmailHeaders.REL_SUCCESS, 1);
    runner.assertTransferCount(ExtractEmailHeaders.REL_FAILURE, 0);

    runner.assertQueueEmpty();
    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(ExtractEmailHeaders.REL_SUCCESS);
    splits.get(0).assertAttributeEquals("email.headers.from.0", from);
    splits.get(0).assertAttributeExists("email.headers.mime-version");
    splits.get(0).assertAttributeNotExists("email.headers.to");
    splits.get(0).assertAttributeNotExists("email.headers.cc");
    splits.get(0).assertAttributeNotExists("email.headers.bcc");
}
 
Example 7
Source File: TextCalendarBodyToAttachment.java    From james-project with Apache License 2.0 4 votes vote down vote up
private void removeAllContentHeaderFromMimeMessage(MimeMessage mimeMessage, List<Header> contentHeaders) throws MessagingException {
    for (Header header : contentHeaders) {
        mimeMessage.removeHeader(header.getName());
    }
}
 
Example 8
Source File: ListDestination.java    From mireka with Apache License 2.0 4 votes vote down vote up
private MimeMessage createOutgoingMimeMessage(ParsedMail mail)
        throws RejectExceptionExt {
    try {
        // Create a copy of this message to send out
        MimeMessage outgoingMessage =
                new MimeMessage(mail.getMimeMessage());
        // We need to remove this header from the copy we're sending around
        outgoingMessage.removeHeader("Return-Path");

        // Check if the X-been-there header is set to the listserv's name
        // (the address). If it has, this means it's a message from this
        // listserv that's getting bounced back, so we need to swallow it
        if (address.toString().equals(
                outgoingMessage.getHeader("X-been-there"))) {
            throw new RejectExceptionExt(new EnhancedStatus(450, "4.4.6",
                    "Mail list loop detected"));
        }

        setSubject(outgoingMessage);

        // If replies should go to this list, we need to set the header
        if (replyToList) {
            outgoingMessage.setHeader("Reply-To", address.toString());
        }
        // We're going to set this special header to avoid bounces
        // getting sent back out to the list
        outgoingMessage.setHeader("X-been-there", address.toString());

        outgoingMessage.setHeader("List-Id", "<" + listId + ">");
        outgoingMessage.setHeader("List-Post", "<" + address + ">");
        outgoingMessage.removeHeader("List-Help");
        outgoingMessage.removeHeader("List-Unsubscribe");
        outgoingMessage.removeHeader("List-Subscribe");
        outgoingMessage.removeHeader("List-Owner");
        outgoingMessage.removeHeader("List-Archive");

        return outgoingMessage;
    } catch (MessagingException e) {
        logger.error("Cannot create a mail list MimeMessage", e);
        throw new RejectExceptionExt(
                EnhancedStatus.TRANSIENT_LOCAL_ERROR_IN_PROCESSING);
    }
}