简体   繁体   中英

Java MimeMessage email printing header info in body

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM