Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 864
Juan Avatar asked Nov 14 '22 13:11

Juan


1 Answers

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]");
like image 173
Warntjo Avatar answered Dec 06 '22 08:12

Warntjo