我使用标准 .NET SMTPClient 发送纯文本电子邮件 - 如下:
// Configure mail client
using (SmtpClient mailClient = new SmtpClient(AppConfig.SMTPServer))
{
mailClient.Credentials = new System.Net.NetworkCredential(AppConfig.SMTPUsername, AppConfig.SMTPPassword);
// Create the mail message
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(AppConfig.SMTPSenderEmail, AppConfig.SMTPSenderDisplay);
foreach (string recipient in recipients)
{
mailMessage.To.Add(new MailAddress(recipient));
}
mailMessage.Bcc.Add(new MailAddress(AppConfig.SMTPBC));
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = false;
// Attachments
if (attachments != null && attachments.Any())
{
foreach (KeyValuePair<string, Byte[]> attachment in attachments)
{
MemoryStream memStream = new MemoryStream(attachment.Value);
mailMessage.Attachments.Add(new Attachment(memStream, attachment.Key));
}
}
mailClient.Send(mailMessage);
}
当通过 POP3 客户端发送时,发送的电子邮件具有预期的格式,但是当我通过 Azure SendGrid 模块
发送时,每个新行都会加倍,因此源正文字符串中的每一行都有两个空行。有谁知道如何规避这个问题,因为我需要(并且想要)使用 SendGrid。
我看到这个非常相似的问题SendGrid newline issue但是该修复与 PHP 相关 - 我需要 C# 中的等效项
最佳答案
好的,经过几个小时的调查,在发布问题后 5 分钟内,我找到了答案,而且非常简单,将其添加到邮件客户端配置中可以完美解决问题:
mailMessage.BodyEncoding = Encoding.UTF8;
关于c# - 通过 SendGrid 的纯文本电子邮件中的额外新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22564349/