开发者

Java MimeMessage email printing header info in body

开发者 https://www.devze.com 2023-02-14 17:19 出处:网络
A code base I inherited is printing out some header info in the body of email. This is what is being printed:

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();
  }
}


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");.

0

精彩评论

暂无评论...
验证码 换一张
取 消