smtpmime4j

Is there an accepted maximum line length for SMTP header fields?


Question

What is a good value to use as "max header line length" if there's no standard?

Is there some kind of informal standard for what the maximum length of an SMTP header line should be?


Context

I use Mime4J for parsing SMTP messages.

Recently, I had a parsing failure related to a very long header line, caused by a header apparently added by Microsoft Forefront. The offending header line looks like:

x-forefront-antispam-report: 
    CIP:94.245.94.31;IPV:NLI;CTRY:IE;EFV:NLI;SFV:NSPM;SFS:(10019020)(4636009)(136003)(376002)(396003)(346002)(39850400004)(2980300002)(62414003)(199004)(189003)(11346002)(2501003)(6916009)(5640700003)(8936002)(126002)(76176011)(9326002)(486006)(2476003)(476003)(33656002)(3480700005)(2906002)(99286004)(568964002)(21480400003)(446003)(606006)(6506007)(15974865002)(14454004)(61614004)(229853002)(33964004)(53546011)(71200400001)(71190400001)(26005)(102836004)(66066001)(186003)(5660300002)(5070765005)(6246003)(246002)(235185007)(55016002)(316002)(966005)(7696005)(356004)(3846002)(8676002)(9686003)(86362001)(54896002)(6306002)(790700001)(236005)(6116002)(106002)(7066003)(7636002)(7736002)(70586007)(25786009)(70206006)(7116003)(52536014)(14444005)(74316002)(7596002)(16586007)(336012)(53386004)(5024004)(45080400002)(478600001)(71440200001);DIR:OUT;SFP:1102;SCL:1;SRVR:DB8PR03MB5548;H:eu22-emailsignatures-cloud.codetwo.com;FPR:;SPF:Pass;LANG:en;PTR:eu22-emailsignatures-cloud.codetwo.com;A:1;MX:1;

This caused the following parsing error from Mime4j:

org.apache.james.mime4j.MimeIOException: org.apache.james.mime4j.io.MaxLineLimitException: Maximum line length limit (1000) exceeded

I was able to workaround this problem by configuring Mime4J to accept longer lines:

    msgFactory.setAttribute(
      "MimeEntityConfig",
      new MimeConfig.Builder().setMaxLineLen(2000).build() );

I doubled it to 2000 as I have plenty of head-room in terms of heap space, so that's not a problem.

But I could've actually fixed this by just bumping the max line length to 1001. It seems more than coincidental that this header line is exactly 999 characters long and the default Mime4J value is 1000.

This whole issue is probably an off-by-one error caused by the fact that the mail that was received was terminated with windows line-endings (i.e. two bytes instead of one).

It seems like MS Forefront and Mime4j agree with each other in that the max length is 1000, just someone got their wires crossed about line-endings.

Googling gave me this SO answer which implies there is no standard max length: https://stackoverflow.com/a/2721849/924597

Is there a "correct" value?


Solution

  • SMTP defines a maximum line length of 1000 octets including the new line sequence.

    Seems like the forefront software is the one with a bug.

    That said, it’s silly for software to not properly deal with arbitrarily long lines. You shouldn’t need to define a max length at all.

    In the .NET space, my MIME parser library has no defined max line length for the parser, it only has max line lengths defined for formatting output in order to comply with the specs.