Example usage for javax.mail.internet MimeMessage setHeader

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

Introduction

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

Prototype

@Override
public void setHeader(String name, String value) throws MessagingException 

Source Link

Document

Set the value for this header_name.

Usage

From source file:org.xwiki.mail.integration.JavaIntegrationTest.java

@Test
public void sendMailWithCustomMessageId() throws Exception {
    Session session = Session.getInstance(this.configuration.getAllProperties());
    MimeMessage message = new MimeMessage(session) {
        @Override//from  www . ja v a  2  s  .  c  o  m
        protected void updateMessageID() throws MessagingException {
            if (getMessageID() == null) {
                super.updateMessageID();
            }
        }
    };
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
    message.setText("Test message Id support");
    message.setHeader("Message-ID", "<custom@domain>");
    message.setSubject("subject");

    MailListener memoryMailListener = this.componentManager.getInstance(MailListener.class, "memory");
    this.sender.sendAsynchronously(Arrays.asList(message), session, memoryMailListener);

    // Verify that the mails have been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 1);
    MimeMessage[] messages = this.mail.getReceivedMessages();

    assertEquals("<custom@domain>", messages[0].getMessageID());
}

From source file:org.xwiki.watchlist.internal.notification.WatchListEventMimeMessageIterator.java

private MimeMessage setConversationHeaders(MimeMessage originalMessage,
        WatchListMessageData watchListMessageData) throws MessagingException {
    // We need to copy the message in order to be able to set the Message-ID header without JavaMail overriding
    // it with a random one by default.
    MimeMessage result = new MimeMessage(originalMessage);

    DocumentReference documentReference = watchListMessageData.getEvents().get(0).getDocumentReference();
    String serializedReference = serializer.serialize(documentReference);

    // Using MD5 instead of the document reference string to avoid escaping issues. Also, we can not use hashcode()
    // on document reference because it is not consistent across JVM restarts. hashcode() over the reference string
    // would also be subject to many collisions, so it's not a good option either.
    String conversationIDPart = DigestUtils.md5Hex(serializedReference);
    String suffix = getConversationSuffix();
    String conversationID = String.format("<%s.XWiki.%s>", conversationIDPart, suffix);

    // Set the headers.
    result.setHeader("References", conversationID);
    result.setHeader("In-Reply-To", conversationID);

    return result;
}