Retrieve the generated Message-ID before sending email using Spring JavaMail
Solution 1
Try message.getMimeMessage().saveChanges()
before message.getMimeMessage().getMessageID()
.
You also need this when creating the message:
MimeMessage msg = new MimeMessage(session) {
protected void updateMessageID() throws MessagingException {
if (getHeader("Message-ID") == null)
super.updateMessageID();
}
};
That causes the Message-ID header to only be set the first time if it's not already set.
Solution 2
The messageID doesn't get set until the message is sent.
If you want to set a custom message, I believe you need to override MimeMessage like so:
public class MyMimeMessage extends MimeMessage {
private String messageID;
public MyMimeMessage(Session session, String messageID) {
super(session);
this.messageID = messageID;
}
@Override
protected void updateMessageID() throws MessagingException {
setHeader("Message-ID", messageID);
}
}
Also, some email services (e.g. gmail) require the messageID to fit a certain syntax (almost like an email address) which you can look at here
Mahendran
I am a java programmer now interested in ruby, groovy, scala. @mahendranMR
Updated on June 05, 2022Comments
-
Mahendran almost 2 years
I am using
org.springframework.mail.javamail.JavaMailSender
to send email using SMTP and in that I need to retrieve the Message-ID header. The below codemessage.getMimeMessage().getMessageID()
is returning null. How to retrieve the Message-ID?
public void notifyByMail(final NotificationRequest request) { MimeMessagePreparator preparator = new MimeMessagePreparator() { public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper message = null; message = new MimeMessageHelper(mimeMessage, UTF_8); message.setTo(request.getTo()); message.setCc(request.getCc()); message.setFrom(request.getFrom()); message.setReplyTo(request.getReplyTo()); message.setSubject(request.getSubject()); message.setText("some text", true); //Need to retrieve the Message-ID here System.out.println("Message - ID : " + message.getMimeMessage().getMessageID()); } }; getMailSender().send(preparator); }
-
Olaf almost 10 yearsDid anyone verify that this approach works? I am getting one message ID after saveChanges and another message ID when the message is actually sent.
-
Bill Shannon almost 10 yearsPerhaps Spring is modifying the message after you do the above and before the message is actually getting sent? You could just skip Spring and use JavaMail directly.
-
Olaf almost 10 yearsI am actually using straight JavaMail. The generated IDs have format: <some random number>.<sequence number>.<timestamp in millis>.JavaMail.<username>@<hostname>. The sequence number is incremented by 1 between the number I get and the number I see in the email client on the destination. The timestamp in millis either exactly the same or is off by 1.
-
Olaf almost 10 yearsThanks for the update! This indeed prevents JavaMail from reassigning a new Message-ID to the email.
-
Borgy Manotoy almost 5 yearsHi, how do you instantiate this
MyMimeMessage
? I am using something likem_javaMailSender.createMimeMessage()
. I wonder how I could get the session for the custom MimeMessage. Thanks! -
Borgy Manotoy almost 5 yearsAlso, I think there is a specific format to follow for message ids to be accepted