c# - 通过 SendGrid 的纯文本电子邮件中的额外新行

标签 c# email azure sendgrid

我使用标准 .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/

相关文章:

c# - 为什么我不能分配 LINQ Find() 或 FirstOrDefault() 结果的返回值?

c# - Kernel32.dll 中的 CreateFile 返回无效句柄

node.js - 在不超过速率限制的情况下尽可能快地通过 SES 发送电子邮件

javascript - 有没有办法让电子邮件中的按钮远程发送数据?

连接到 Azure SQL 的本地应用程序需要 Azure 混合连接吗?

node.js - 使用 Microsoft Azure 功能通过 HTTP 进行 FTP

c# - 异常(exception) list

c# - 获取列表列表中的最大值列表

javascript - Node - 用于发送电子邮件的选项,sendgrid 和 nodemailer 其中之一或组合

java - 401-从 Spring Boot 应用程序使用 REST API Dynamics CRM 和 Azure AD 进行未经授权的身份验证