Feb 20

When using Javamail to send mail the Message-ID gets set when the message is actually sent. If you want to provide your own Message-ID then you need to subclass MimeMessage. You can then use that subclass to create your message.

public class CustomMimeMessage extends MimeMessage {

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

	@Override
	protected void updateMessageID() throws MessagingException {
		setHeader("Message-ID", "objects-message-id");
	}	
}

written by objects \\ tags: , ,


2 Responses to “How to specify a custom Message-ID with Javamail”

  1. David Zalkin Says:

    I have done this, however the message-ID I supply is not recognized when sending mail using Gmail – rather, Gmail apparently generates its own message-ID (when I use the default JavaMail message-ID, Gmail does recognize it). Any ideas why this is happening or how to solve it?
    Thanx

  2. JimmyL Says:

    Hi David,

    It’s been a long time since you posted your problem, but do you remember if the Message-ID you set – and that was not recognize by GMail – was well-formed?

    I mean that the Message-ID must respect a certain pattern (similar to an e-mail address, cf. http://tools.ietf.org/html/rfc5322#section-3.6.4). Maybe it wasn’t the case with your custom ID and that was why GMail reject it?

Leave a Reply

You must be logged in to post a comment.