Java MimeMessage 电子邮件在正文中打印标题信息

发布于 2024-10-20 15:03:52 字数 1406 浏览 12 评论 0原文

我继承的代码库正在打印电子邮件正文中的一些标题信息。这就是打印的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2024-10-27 15:03:52

这些属性通过 java mail api 公开,它们被设置为头属性,例如 MimeMessage

Message msg = new MimeMessage(session);
msg.setHeader("MIME-Version", "1.0" );
msg.setHeader("Content-Type", "text/plain; charset=us-ascii" );

邮件服务器可以根据其本地策略依次更改标头。邮件服务器间通信可以使用例如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.

Message msg = new MimeMessage(session);
msg.setHeader("MIME-Version", "1.0" );
msg.setHeader("Content-Type", "text/plain; charset=us-ascii" );

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, like setHeader("MIME-Version", "1.0");.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文