【发布时间】: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