我使用标准.NET SMTPClient发送PLAIN文本电子邮件 - 如下所示:
// 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 module
发送时,每个新行都加倍,因此有两个空白行对于源体字符串中的每一个。有没有人知道如何绕过这个问题,因为我需要(并希望)使用SendGrid。
我看到这个非常相似的问题SendGrid newline issue但是修复与PHP有关 - 我需要C#中的等价物
答案 0 :(得分:6)
好的,经过几个小时的调查后,在发布问题的5分钟内我找到答案并且真的很简单,将其添加到邮件客户端配置中就可以完美地排序问题:
mailMessage.BodyEncoding = Encoding.UTF8;