Juan
Juan

Reputation: 467

Message-Id is being replaced when sending mail via JavaMail

I have tried to find a solution to this with any luck, so i decided to post it here.

The problem is, when i send a message with javaMail, it automatically generates a Message-Id (The one i store into my database to then identify replies to this message) but it is being changed for some reason by the smpt server when the message is sent so i wont be able to trace any related to this message.

For example

I first send a message via gmail to one of the accounts sycronized with my mail client, then i check the message with my message client and everything is ok the Message-Id is

<CAPDSfCN1qPAhBCRmFK-zwP=MM=KjgpYuvhVRFAPwz1PjOqtnFA@mail.gmail.com>

Then i send a reply for this message via my message client, the id generated by javaMail is

<[email protected]>

Finally, when i go to check the reply in my email account it has the following values in its headers

Message-ID: <[email protected]> FAIL

In-Reply-To: <CAPDSfCN1qPAhBCRmFK-zwP=MM=KjgpYuvhVRFAPwz1PjOqtnFA@mail.gmail.com> OK

As you see, the Message-Id is changed, i was expecting it to be

<[email protected]>

Why is this happening?

I appreciate any help

Thank you all

--Edit

According to sugestions i made a test using smtpsend demo from javaMail (I implemented a subclass of MimeMessage to generate my own Message-Id).

java -jar -Dmail.smtp.starttls.enable=true -Dmail.smtp.port=587 SMTPSend.jar -d -M smtp.live.com -U [email protected] -P mypass -o [email protected] -A [email protected]

Between smtpsend output when message was sent, there was the Message-Id generated

<[email protected]>

But then, when i went to check this message on [email protected], the Message-Id was different

<[email protected]>

Why is it changing my Message-Id on the fly... i dont get it

--Edit 2

I noticed that the problem is now happening just when i send mails from a hotmail account message-id is not changing anymore when i send mails from a gmail account (i think that implementing my own Message-Id generation method helped to solve that)

Thanks for replying

Upvotes: 5

Views: 7423

Answers (3)

Benny Bottema
Benny Bottema

Reputation: 11493

We've encountered the same issue with Simple Java Mail and as far as I know the only way to catch the actual ID assigned by the SMTP server is to parse the 250 OK response which includes it (in case of MS Exchange).

As Bill (the prime author of the original JavaMail) mentions below, the SMTP server should actually never change the Message ID, but some servers like MS Exchange explicitly allow configuration otherwise. For example see: https://social.technet.microsoft.com/Forums/en-US/132df54c-871b-4e9d-98c9-5b5caa993322/custom-message-id-replaced-by-outlook-smtp-server?forum=exchangesvrclients.

If you are seeing this behavior with all servers you connect to, chances are you are not setting MessageID the right way (Java/Jakarta Mail is a little obtuse about this). In that case refer to the other answer.

Upvotes: 0

Warntjo
Warntjo

Reputation: 29

I know this is an old thread, but this answer could still maybe help people.

You need to overrule updateMessageID() in MimeMessage as it gets called everytime before sending an e-mail.

class MyMessage extends MimeMessage {

    public MyMessage(Session session) {
        super(session);
    }

    protected void updateMessageID() throws MessagingException {
        setHeader("Message-ID", "<[email protected]>");
    }
}

And if you would like to pass the unique id for each MyMessage...

class MyMessage extends MimeMessage {
        String uniqueMessageId;     

        public MyMessage(Session session, String uniqueMessageId) {
            super(session);
            this.uniqueMessageId = uniqueMessageId;

        }

        protected void updateMessageID() throws MessagingException {
            setHeader("Message-ID", "<" + uniqueMessageId + ">");
        }
    }

And then call it, eg:

MyMessage message = new MyMessage(session, "[email protected]");

Upvotes: 2

Bill Shannon
Bill Shannon

Reputation: 29961

Your mail server is broken. It should not be changing the Message-ID header. Report the problem to the owner of your mail server.

Upvotes: -1

Related Questions