How do I set or change the SMTP Message-ID with ja

2019-02-03 05:47发布

问题:

How can I set the SMTP message-id while sending mails with javax.mail. My mail server is reporting something like this:

1 <= me@domain.com H=mail (host) [192.168.1.4] P=esmtp S=142014
     id=2043289758.9.1322829290422.JavaMail.thor@developer.local
2 => sombodey@else R=dnslookup T=remote_smtp H=mx00.t-online.de [194.25.134.8]
3 Completed

I want to set the id=2043289758.9.1322829290422.JavaMail.thor@developer.local before sending it. Is this possible? The email it created like this:

Properties props = System.getProperties();
props.put("mail.smtp.host", "192.168.1.4");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Message msg = createMsg();
Transport.send(msg);

回答1:

I believe this section of the JavaMail FAQ answers your question:

Q: I set a particular value for the Message-ID header of my new message, but when I send this message that header is rewritten.

A: A new value for the Message-ID field is set when the saveChanges method is called (usually implicitly when a message is sent), overwriting any value you set yourself. If you need to set your own Message-ID and have it retained, you will have to create your own MimeMessage subclass, override the updateMessageID method and use an instance of this subclass.

class MyMessage extends MimeMessage {
    ...

    protected void updateMessageID() throws MessagingException {
  setHeader("Message-ID", "my-message-id");
    }
    ...
}


回答2:

According to Oracle's FAQ, when you send the message or call saveChanges, the Message-Id header set by the updateMessageID() method, which overrides any value you might have already set.

To set your own value, you will need to create a sub-class of MimeMessage and implement your own updateMessageID() function:

class MyMimeMessage extends MimeMessage {
    ...
    protected void updateMessageID() throws MessagingException {
        setHeader("Message-ID", "my-message-id");
    }
    ...
}

I was able to use this technique to suppress or eliminate the Message-Id header like this:

@Override
protected void updateMessageID() throws MessagingException {
    removeHeader("Message-Id");
}

This was useful for Mailgun, which sets the Message-Id themselves if you don't set one.



回答3:

You can also save message changes and set a custom Message-ID before sending. Steps:

  1. Create MimeMessage message and Set its data.
  2. message.saveChanges();
  3. message.setHeader("Message-ID", yourCustomMessageId);
  4. Send the message.

This is useful with Spring's JavaMailSender or in other situations where it is difficult to override MimeMessage.



回答4:

MimeMessage msg = new MimeMessage(s) {
    @Override
    protected void updateMessageID() { } // Prevent MimeMessage from overwriting our Message-ID
};
msg.setHeader("Message-ID", "MyOwnMessageID");


回答5:

this is the complete code for implementing the custom id in the message

public class SendMessage{

     private String idMessage;

     private void sendEmail() {
         //...
         // Create de session
         //...

         Message message = new MyMessage(session);

         //...
         // Prepare the body of the message, attachments, etc.
         //...

         // assign custom message id
         this.idMessage = “My own ID”;

         //send
         Transport.send(message);
    }

    class MyMessage extends MimeMessage {

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

        protected void updateMessageID() throws MessagingException {
            setHeader("Message-ID", this.idMessage);
        }
    }
}