CRLF的缺失导致smtp方式发送邮件失败

CRLF的缺失导致smtp方式发送邮件失败

November 5, 2023
后端开发
smtp, 邮件发送

问题描述 #

最近在工作中遇到一个问题,客户端(c++)使用smtp方式发送邮件失败,报错如下:

The mail client submitted this message with bare line feeds, which cannot be sent via SMTP protocol DATA command and receiving system does not support BDAT

翻译中中文就是:

邮件客户端在提交此邮件时未换行,无法通过 SMTP 协议 DATA 命令发送,且接收系统不支持 BDAT

我们从这个提示中提取出几个关键词:换行、DATA、BDAT。

使用搜索引擎搜索这个错误,看到了一篇微软官方的文章,见参考资料1。 这片资料中给出的解释和解决方案比较明确: smtp-crlf 这里面说,是由于没有将CR-LF追加到消息末尾,并且提示到了RFC2822。

我们下面看一下相关关键词的含义:

  • CR-LF:回车换行,即\r\n
  • DATA:SMTP协议中的一个命令,用于发送邮件内容。
  • ESMTP: SMTP协议的扩展,即Extended SMTP。
  • BDAT: ESMTP协议中的一个命令,用于发送邮件内容,可以指定数据块的大小。

在RFC2822中,我们可以看到如下内容:

Messages are divided into lines of characters.  A line is a series of
   characters that is delimited with the two characters carriage-return
   and line-feed; that is, the carriage return (CR) character (ASCII
   value 13) followed immediately by the line feed (LF) character (ASCII
   value 10).  (The carriage-return/line-feed pair is usually written in
   this document as "CRLF".)

翻译成中文就是:

信息被分成一行一行的字符。 一行是一系列
   回车和换行这两个字符来分隔。
   回车(CR)字符(ASCII
   值 13),紧接着是换行(LF)字符(ASCII
   值 10)。 (回车/换行在本文档中通常写为 "CRLF"。
   本文件中通常写为 "CRLF")。

我们再来看一下rfc3030中的内容:

A new SMTP verb, BDAT, is defined as an alternative to the "DATA"
      command of [RFC821].  The BDAT verb takes two arguments.  The
      first argument indicates the length, in octets, of the binary data
      chunk.  The second optional argument indicates that the data chunk
      is the last.

      bdat-cmd   ::= "BDAT" SP chunk-size [ SP end-marker ] CR LF
      chunk-size ::= 1*DIGIT
      end-marker ::= "LAST"

大意就是:

BDAT,可以作为“DATA"的替代品,BDAT命令有两个参数,第一个参数标识长度,第二次参数标识是否是最后一个数据块。

问题总结 #

问题原因 #

客户端在发送的时候,没有将CR-LF追加到消息末尾,导致发送失败。而接收服务器不支持BDAT命令,所以报错。

按照参考资料,准确的说是每一行都应该添加(CR-LF),而不仅是消息末尾。

解决方案 #

在每一行消息末尾添加(CR-LF)即可。

客户端这样处理后确实正常了。

实际上,很多语言的库都会自动添加(CR-LF),比如go和python。所以大多数时候我们不需要关心这个问题。

参考资料: