0

I am trying to send the email on the same thread by passing the threadID in the "In-Reply-To" and "Reference" parameter of setHeader method but the problem is that the mail is sent but not on the same thread. Below is the code snippet that I am using Any help would be highly appreciated.

String Subject = "Re:Test Java Mail API";
String ThreadID = "PSBPR02MB43444E8F5DA233F48E0CCF9ED7309@PSBPR02MB4344.apcprd02.prod.outlook.com"
    

    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(user));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setHeader("In-Reply-To", ThreadID);
    message.setHeader("References", ThreadID);
    message.setHeader("Subject", Subject);
    message.setContent(str, "text/html;charset=UTF-8");
    Transport.send(message);
    
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • You could find the threadID before calling your send mail method and pass it as a parameter. – Sascha Jun 15 '21 at 13:44
  • @Sascha,thanks for the help.I have tried this by getting the messageID and then passing the same messageID but it is still sending the message on different thread. PFB the link for the modified code. https://pastebin.com/TG8Q9hHJ – AYUSHI Agrahari Jun 15 '21 at 14:45
  • I fix the thread by using the built in `MimeMessage replyMessage = (MimeMessage) emailMessage.reply(repyToAll)`. You could do that just to see which headers are being set to fix the thread. – Benny Bottema Jun 15 '21 at 20:18
  • Just to clarify: You want the mail in the client to be displayed in a thread? Then it seems you are not really in control of it and can only hope the mail client does it the way you want it to. [Reference](https://stackoverflow.com/a/36251102/2135838) – Sascha Jun 16 '21 at 07:45
  • Maybe [this info](https://www.jwz.org/doc/threading.html) could help. But really, it seems that some mail servers [also meddle with the message ids](https://stackoverflow.com/q/8297927/2135838). – Sascha Jun 16 '21 at 07:52

0 Answers0