Reputation: 722
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("[email protected]", "Name Surname");
List<MailAddress> cc = new List<MailAddress>();
cc.Add(new MailAddress("[email protected]", "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("[email protected]", "User Surbname");
MailAddress gmailAccount = new MailAddress("[email protected]", "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.
Upvotes: 0
Views: 2499
Reputation: 1
You can use StringBuilder
class.
StringBuilder sb = new StringBuilder();
sb.AppendLine("Your tex");
OR
sb.AppendFormat("your text \n");
It will work :)
Upvotes: 0
Reputation: 364
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:
Outlook:
[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("[email protected]", "password");
var Text = "Good day" + "<br><br/>" + "I hope this message finds you well.";
msgMail.From = _from;
var mailCollection = new MailAddressCollection()
{
new MailAddress("[email protected]", "User Surbname"),
new MailAddress("[email protected]", "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);
}
Upvotes: 1
Reputation: 690
@Harold_Finch You can just use Environment.NewLine property instead of <br
>. That will insert a newline string defined for the current environment.
Upvotes: 0