Java MimeMessage 电子邮件在正文中打印标题信息
我继承的代码库正在打印电子邮件正文中的一些标题信息。这就是打印的内容:
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
如果我在创建消息后立即 message.writeTo(System.out);
,上面的文本将打印出来。
是否有属性文件或其他地方指定了这些东西?
看起来当邮件到达时,发送服务器已经为这三个属性写入了正确/不同的标头信息。
有什么想法可以删除它吗?
另外,这是整个函数:
private String sendConfirmationEmail (String to, String from, String subject, String body, boolean CCSender) {
try
{
String smtpHost = Properties.smtp;
String fromAddress = from;
String toAddress = to;
Properties properties = System.getProperties();
properties.put("mail.smtp.host", smtpHost);
Session session = Session.getInstance(properties, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(toAddress));
message.setRecipient(Message.RecipientType.BCC,
new InternetAddress(fromAddress));
if (CCSender) {
message.setRecipient(Message.RecipientType.CC,
new InternetAddress(from));
}
message.setSubject(subject);
message.setText(body);
message.saveChanges();
Transport.send(message);
return "1:success";
}
catch(Exception e)
{
return "0:failure "+e.toString();
}
}
A code base I inherited is printing out some header info in the body of email. This is what is being printed:
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
If I message.writeTo(System.out);
right after creating the message, the above text will print out.
Is there a properties file or something somewhere that specifies this stuff?
It also looks like when the mail arrives the outgoing server has written proper/different header information for these three attributes.
Any ideas for removing it?
Also, here is the whole function:
private String sendConfirmationEmail (String to, String from, String subject, String body, boolean CCSender) {
try
{
String smtpHost = Properties.smtp;
String fromAddress = from;
String toAddress = to;
Properties properties = System.getProperties();
properties.put("mail.smtp.host", smtpHost);
Session session = Session.getInstance(properties, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(toAddress));
message.setRecipient(Message.RecipientType.BCC,
new InternetAddress(fromAddress));
if (CCSender) {
message.setRecipient(Message.RecipientType.CC,
new InternetAddress(from));
}
message.setSubject(subject);
message.setText(body);
message.saveChanges();
Transport.send(message);
return "1:success";
}
catch(Exception e)
{
return "0:failure "+e.toString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些属性通过 java mail api 公开,它们被设置为头属性,例如 MimeMessage。
邮件服务器可以根据其本地策略依次更改标头。邮件服务器间通信可以使用例如
gzip
压缩来很好地执行,其中需要另一组标头。[编辑]如果您观察源在
MimeMessage
的代码中,您会看到一些标头设置为默认值,例如setHeader("MIME-Version", "1.0");
。These properties are exposed through the java mail api, which are set as header attributes in e.g., MimeMessage.
The headers can in turn be changed by mail servers according to their local policy. Inter-mail servern communication could well be performed using e.g.
gzip
compression where another set of headers will be required.[EDIT] If you observe the source code for
MimeMessage
you will see that some headers are set default, likesetHeader("MIME-Version", "1.0");
.