【问题标题】:Mailkit SMTP error - BareLinefeedsAreIllegalMailkit SMTP 错误 - BareLinefeedsAreIllegal
【发布时间】:2017-02-27 17:59:23
【问题描述】:

远程服务器返回 '550 5.6.2 SMTPSEND.BareLinefeedsAreIllegal;消息包含裸换行符,无法通过 DATA' 发送

            var message = new MimeMessage();
            message.From.Add(new MailboxAddress(nameFrom, mailboxFrom));
            message.Subject = Subject;
            message.To.Add(new MailboxAddress(mailboxTo));

            var bodyBuilder = new BodyBuilder();

            var multipart = new Multipart("mixed");

            bodyBuilder.HtmlBody = "Test Body";

            multipart.Add(bodyBuilder.ToMessageBody());


            byte[] bytes = System.IO.File.ReadAllBytes("Test.gif");


            var attachment = new MimePart("binary", "bin")
            {
                ContentObject = new ContentObject(new MemoryStream(bytes), ContentEncoding.Base64),
                ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Binary,
                FileName = "F2.pdf"
            };

            multipart.Add(attachment);

            message.Body = multipart;


            using (var client = new SmtpClient())
            {
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                client.Connect(ssServer, ssPort, MailKit.Security.SecureSocketOptions.Auto);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove("XOAUTH2");

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate(ssMailBox, ssMailBoxPassword);

                client.Send(message);
            }

我已经尝试过对 ContentEncoding.Base64 和 ContentEncoding.Binary 进行编码,结果相同。当我跳过附件部分时,邮件会正确发送。 “Test.gif”只是我正在上传的一个随机 gif。

我已经阅读了有关CHUNKING or the BDAT command 的信息,但不太确定这一点或如何将它与mailkit 一起使用……有什么建议吗?我只是想发送带有附件的普通 SMTP 邮件,这并不难:|

【问题讨论】:

  • 尝试将attachment.ContentTransferEncoding 设置为ContentEncoding.Base64
  • 如果这不起作用,如果您根本不添加附件,它是否可以正确发送?也许裸换行符在标题中?
  • 进一步研究,MailKit 应该自己设置了正确的 ContentTransferEncoding。当我进行本地测试时,确实如此。您的 SMTP 服务器宣传哪些功能?如果收到ProtocolLog,它真的会发送DATA 命令吗?还是发送正确的BDAT?如果您的服务器支持BINARYMIME,那么MailKit 应该自动将编码设置为“二进制”并使用BDAT 命令。

标签: c# email smtp attachment mailkit


【解决方案1】:

正如@Jstedfast 建议的那样,解决方案是改变:

ContentTransferEncoding = ContentEncoding.Base64

我误解并尝试更改 ContentObject 编码。

感谢@Jstedfast

【讨论】:

  • 是的,ContentObject 的 Encoding 值是源编码,而不是传输编码:)
猜你喜欢
  • 1970-01-01
  • 2018-09-28
  • 2018-06-15
  • 2017-06-08
  • 2021-10-03
  • 2015-10-11
  • 2015-06-12
  • 2016-10-26
  • 2019-12-31
相关资源
最近更新 更多