5

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

<1907960987.0.1322086080735.JavaMail.root@smtp.live.com>

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

Message-ID: <BLU0-SMTP33091BE2B32A7F46E370665C2C90@phx.gbl> 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

<1907960987.0.1322086080735.JavaMail.root@smtp.live.com>

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 myaccount@hotmail.com -P mypass -o myaccount@hotmail.com -A anotheraccount@gmail.com

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

<60eea6ae-2657-41bd-b475-3a57eff885ac@mydomain.com>

But then, when i went to check this message on anotheraccount@gmail.com, the Message-Id was different

<BLU0-SMTP109215E6BB99B93FC106B1E88B00@phx.gbl>

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

Juan
  • 467
  • 1
  • 5
  • 19
  • I'm seeing the same thing with outlook/hotmail now using C# and OpenPop. The Message-ID is being overwritten with an ID at least partially based on the SMTP server that sent it. eg... `Message-ID: Received: from myBox ([111.111.111.111]) by BLU0-SMTP238.phx.gbl over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675);` Both are `BLU0-SMTP238`. /sigh I ended up adding another header to the email with my known unique string in it for hotmail/outlook. – ruffin Jan 15 '14 at 03:04
  • I'm having the same issue when sending from microsoft email accounts. Did you ever resolve your problem? – Farzher Mar 17 '14 at 21:27

3 Answers3

2

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", "<your-message-id@domain.nl>");
    }
}

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, "201610131428_newsletter1@domain.nl");
Warntjo
  • 29
  • 5
  • Nope, this is how you normally set the MessageID on the client side. But there are cases like OP's where the SMTP SERVER changes it after sending. You cannot override this behavior from the client side. You can only catch the 205 OK with the ID being returned. – Benny Bottema Dec 26 '21 at 09:58
0

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.

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
-1

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

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I dont think its a server issue cause i have tested it with different accounts, (gmail and hotmail accounts), could it be the format javaMail is generating this Message-Id so when it arrives to the smtp server it detect this format as invalid and then regenerates it? thanks – Juan Nov 28 '11 at 20:20
  • If you're seeing the same behavior using different mail servers, something else is going wrong. I can confirm that when I use JavaMail to send mail through Gmail, the Message-ID header is preserved. Perhaps you have some firewall or anti-virus software that's modifying the message while it's being sent? Tell us more about exactly what you've tested and exactly how you're connecting to the mail servers. Can you reproduce the problem using the smtpsend.java demo program included with JavaMail? – Bill Shannon Nov 29 '11 at 19:17
  • thanks for replying, look at the edit i made to my original post – Juan Nov 30 '11 at 20:31
  • It should not, but some servers specifically allow configuration otherwise (like MS Exhange servers). See https://social.technet.microsoft.com/Forums/en-US/132df54c-871b-4e9d-98c9-5b5caa993322/custom-message-id-replaced-by-outlook-smtp-server?forum=exchangesvrclients – Benny Bottema Dec 26 '21 at 10:00