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:
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.
@Harold_Finch You can just use Environment.NewLine property instead of <
br
>. That will insert a newline string defined for the current environment.This worked for my Gmail and Outlook.
Gmail:
Outlook:
[UPDATE]
For your spesific code try this:
You can use
StringBuilder
class.OR
It will work :)