如何使用javax.mail设置或更改SMTP消息ID? [英] How do I set or change the SMTP Message-ID with javax.mail?
问题描述
如何在使用 javax.mail
发送邮件时设置SMTP邮件ID。我的邮件服务器报告的内容如下:
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
我想在发送之前设置 id = 2043289758.9.1322829290422.JavaMail.thor@developer.local
。这可能吗?它创建的电子邮件如下:
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);
推荐答案
我相信 JavaMail FAQ 回答你的问题:
问:我为新邮件的Message-ID标头设置了一个特定值,但是当我发送此邮件时,标头会被重写。
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:调用saveChanges方法时设置Message-ID字段的新值(通常在消息为
发送时隐式) ,覆盖你自己设定的任何价值。如果您需要设置
自己的Message-ID并保留它,则必须创建自己的
MimeMessage子类,覆盖updateMessageID方法并使用此子类的
实例。 / p>
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");
}
...
}
这篇关于如何使用javax.mail设置或更改SMTP消息ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!