سؤال

I am using a standard .NET SMTPClient to send a PLAIN text email - as follows:

// 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);

        }

When sending through a POP3 client the email that is sent has the expected format, however when I send through the Azure SendGrid module, each new line is doubled up so there are two blank lines for every one in the source body string. Does anyone know how to circumvent this issue as I need (and want) to use SendGrid.

I see this very similar question SendGrid newline issue however the fix relates to PHP - I need the equivalent in C#

هل كانت مفيدة؟

المحلول

Ok, after several hours of investigation, within 5 minutes of posting the question I find the answer and its real simple, adding this to the mail client configuration sorts the issue perfectly:

mailMessage.BodyEncoding = Encoding.UTF8;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top