Mailkit SMTP 错误 - BareLinefeedsAreIllegal

Mailkit SMTP error - BareLinefeedsAreIllegal

远程服务器返回 '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 邮件,这不会那么难:|

按照@Jstedfast 的建议,解决方案是更改:

ContentTransferEncoding = ContentEncoding.Base64

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

感谢@Jstedfast