Append Line Breaks to Email

2019-08-25 23:28发布

So I have written a simple C# WinForms Application that has just one button control which I'll click on the 25th day of every month to notify my users to pay their domain subscription. I'll make it a windows service so that it can run automatically at a later stage when I have time.

I got my implementation from @Tartori on this SO post: Sending emails from a windows forms application which works the way I want it.

Everything is fine but now the problem is that when I send a test mail to an email address which I have the same account setup on Outlook which is honkey dory but now when I view the same test mail using Gmail over the web it does not append the line breaks as per the below two screenshots:

enter image description here enter image description here

Please advise how I can overcome this. My code is as below:

        private void btnSendMail_Click(object sender, EventArgs e)
        {
            MailAddress from = new MailAddress("user@domain.co.za", "Name Surname");

            List<MailAddress> cc = new List<MailAddress>();
            cc.Add(new MailAddress("user2@domain.co.za", "Name2 Surname2"));
            string subjectText = "Friendly Domain Payment Reminder: " + DateTime.Now.ToShortDateString();
            SendEmail(subjectText, from, cc);
        }



        protected void SendEmail(string _subject, MailAddress _from, List<MailAddress> _cc, List<MailAddress> _bcc = null)
        {
            string Text = "";
            SmtpClient mailClient = new SmtpClient("mail.backlinkdesigns.co.za");
            MailMessage msgMail;
            Text = "Good day" + "</br>" + "</br>" + "I hope this message finds you well."                 msgMail = new MailMessage();
            msgMail.From = _from;
            MailAddress outlook= new MailAddress("user@domain.co.za", "User Surbname");
            MailAddress gmailAccount = new MailAddress("user@gmail.com", "Name Surname");
            msgMail.To.Add(outlook);
            msgMail.To.Add(gmailAccount );

            foreach (MailAddress addr in _cc)
            {
                msgMail.CC.Add(addr);
            }
            if (_bcc != null)
            {
                foreach (MailAddress addr in _bcc)
                {
                    msgMail.Bcc.Add(addr);
                }
            }
            msgMail.Subject = _subject;
            msgMail.Body = Text;
            msgMail.IsBodyHtml = true;
            mailClient.Send(msgMail);
            msgMail.Dispose();

            List<MailAddress> countVar = new List<MailAddress>();
            countVar.Add(outlook);
            countVar.Add(gmailAccount );
            var temp = countVar.Count;
            MessageBox.Show("Your email has been successfully sent to " + temp + " users show below: " + "\n\n" + outlook+ "\n" + gmailAccount + "\n" + "\n\n" + "From: " + _from, "Message Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

UPDATE: Attached below is the HTML Visualizer when I use </br> to prove that I am getting the line breaks in code. They show fine in Outlook app but over the web it does not. Please may one try copy paste the code sample before posting a solution seeing as Environment.NewLine does not do the trick.

enter image description here

标签: c# smtp
3条回答
甜甜的少女心
2楼-- · 2019-08-25 23:58

@Harold_Finch You can just use Environment.NewLine property instead of <br>. That will insert a newline string defined for the current environment.

查看更多
该账号已被封号
3楼-- · 2019-08-26 00:07

This worked for my Gmail and Outlook.

  public void SendMail()
    {            

        SmtpClient smtpClient = new SmtpClient(<CLIENT>);
        MailMessage message = new MailMessage(<FROM>, <TO>);

        message.Subject = "Test";
        message.IsBodyHtml = true;

        string html = "<p>hey</p><p></p><p>hey 2</p><p>hey 3</p>";
        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));

        message.AlternateViews.Add(htmlView);

        smtpClient.Send(message);
    }

Gmail:

Displayed in Gmail

Outlook:

Displayed in Outlook 2016

[UPDATE]

For your spesific code try this:

protected void SendEmail(string _subject, MailAddress _from, List<MailAddress> _cc, List<MailAddress> _bcc = null)
    {

        SmtpClient mailClient = new SmtpClient("mail.backlinkdesigns.co.za");
        MailMessage msgMail = new MailMessage(); 
        mailClient.UseDefaultCredentials = false;
        mailClient.Credentials = new NetworkCredential("user@domain.co.za", "password");
        var Text = "Good day" + "<br><br/>" + "I hope this message finds you well.";                

        msgMail.From = _from;
        var mailCollection = new MailAddressCollection()
        {
            new MailAddress("user@domain.co.za", "User Surbname"),
            new MailAddress("user@gmail.com", "Name Surname")
        };

        foreach (var mailAddress in mailCollection)
        {
            msgMail.To.Add(mailAddress);
        }

        foreach (MailAddress addr in _cc)
        {
            msgMail.CC.Add(addr);
        }
        if (_bcc != null)
        {
            foreach (MailAddress addr in _bcc)
            {
                msgMail.Bcc.Add(addr);
            }
        }
        msgMail.Subject = _subject;
        msgMail.IsBodyHtml = true;

        AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Text, new ContentType("text/html"));
        msgMail.AlternateViews.Add(htmlView);

        mailClient.Send(msgMail);
        msgMail.Dispose();


        MessageBox.Show("Your email has been successfully sent to " + mailCollection.Count + " users show below: " + "\n\n" + mailCollection[0].User + "\n" + mailCollection[1].User + "\n" + "\n\n" + "From: " + _from, "Message Status", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
    }
查看更多
Explosion°爆炸
4楼-- · 2019-08-26 00:11

You can use StringBuilder class.

StringBuilder sb = new StringBuilder();

sb.AppendLine("Your tex");

OR

sb.AppendFormat("your text \n");

It will work :)

查看更多
登录 后发表回答