0

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();
  }
}
4
  • have you tried to create some kind of filter method, or use regex to chunk off the header or something. Maybe provide sample input/output someone could help you.
    – ant
    Mar 4, 2011 at 17:59
  • Why do you want to remove those headers? They're accurate, as long as the body is all-ASCII and has no lines > 998 characters...
    – dkarp
    Mar 4, 2011 at 19:17
  • I want to stop them from being printed out in the email. All recipients see these three lines at the top of the email body.
    – estubey
    Mar 4, 2011 at 19:23
  • There is no inherent reason for the headers to end up in the message body. Quick question: If you change message.setSubject(subject) to message.setSubject(subject.trim()), does everything work?
    – dkarp
    Mar 4, 2011 at 21:59

1 Answer 1

2

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

7
  • Yes, but how do I get them out of the body of my email?
    – estubey
    Mar 4, 2011 at 18:08
  • @estubey, you don't, as headers to the request/response they are not part of the body unless manually copied into it. Mar 4, 2011 at 18:11
  • Not sure where they would being copied from. When I do this in my function:
    – estubey
    Mar 4, 2011 at 19:01
  • MimeMessage message = new MimeMessage(session); message.writeTo(System.out); The content output contains: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit
    – estubey
    Mar 4, 2011 at 19:02
  • The toString() routine of the Message as it also encapsules the headers. Apparently, the default headeres are printed. Mar 4, 2011 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.