Formating email using C#

S

Scott Natwick

I'm trying to send an email from a Form. Here's what I have so far:

MailMessage mail = new MailMessage();
mail.To = "(e-mail address removed)";
mail.From = "(e-mail address removed)";
mail.Subject = "this is a test email.";
mail.Body = "Line 1";
mail.Body += "Line 2";
mail.Body += "Line 3";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate","1"); //basic authentication mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here SmtpMail.SmtpServer = "mail.mycompany.com"; //your real server goes here SmtpMail.Send( mail );The example is from here:http://systemwebmail.com/faq/3.8.aspxThe email sends and arrives successfully, however, I need to format themessage. I would like Line 1, Line 2, and Line 3 to be separate paragraphs.Any ideas how to do this?I have searched and found this:Carriage return character (U+000D)Line feed character (U+000A)Carriage return character (U+000D) followed by line feed character (U+000A)Line separator character (U+2028) Paragraph separator character (U+2029)But I don't know if this is what I'm looking for or how to use it.Thanks in advance,Scott Natwick
 
I

i. Wiin

Set MailFormat to a "1", which is HTML format. Then you can insert regular
HTML for formatting in your Body.

Scott Natwick said:
I'm trying to send an email from a Form. Here's what I have so far:

MailMessage mail = new MailMessage();
mail.To = "(e-mail address removed)";
mail.From = "(e-mail address removed)";
mail.Subject = "this is a test email.";
mail.Body = "Line 1";
mail.Body += "Line 2";
mail.Body += "Line 3";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenti
cate","1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername
", "my_username_here"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword
", "super_secret"); //set your password here SmtpMail.SmtpServer =
"mail.mycompany.com"; //your real server goes here SmtpMail.Send(
mail );The example is from here:http://systemwebmail.com/faq/3.8.aspxThe
email sends and arrives successfully, however, I need to format themessage.
I would like Line 1, Line 2, and Line 3 to be separate paragraphs.Any ideas
how to do this?I have searched and found this:Carriage return character
(U+000D)Line feed character (U+000A)Carriage return character (U+000D)
followed by line feed character (U+000A)Line separator character (U+2028)
Paragraph separator character (U+2029)But I don't know if this is what I'm
looking for or how to use it.Thanks in advance,Scott Natwick
 
S

Scott Natwick

I like your suggestion.

I added this code:
mail.BodyFormat = MailFormat.Html;
mail.Body = "<html><body>Testing 123...</body></html>";

And it works -- thanks!

However, now I'm thinking I would give users a choice between HTML and text.
Any ideas about formating a text version?

Regards,
Scott Natwick
 
M

Mark Rae

Any ideas about formating a text version?

"Formatting" and "text version" are pretty much exclusive i.e. you can't
have formatting in a text version because, by definition, it's text only.
You can have carriage returns, though, by using "\r\n"
 
K

Kevin Spencer

Use Environment.NewLine.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

Scott Natwick said:
I'm trying to send an email from a Form. Here's what I have so far:

MailMessage mail = new MailMessage();
mail.To = "(e-mail address removed)";
mail.From = "(e-mail address removed)";
mail.Subject = "this is a test email.";
mail.Body = "Line 1";
mail.Body += "Line 2";
mail.Body += "Line 3";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenti
cate","1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername
", "my_username_here"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword
", "super_secret"); //set your password here SmtpMail.SmtpServer =
"mail.mycompany.com"; //your real server goes here SmtpMail.Send(
mail );The example is from here:http://systemwebmail.com/faq/3.8.aspxThe
email sends and arrives successfully, however, I need to format themessage.
I would like Line 1, Line 2, and Line 3 to be separate paragraphs.Any ideas
how to do this?I have searched and found this:Carriage return character
(U+000D)Line feed character (U+000A)Carriage return character (U+000D)
followed by line feed character (U+000A)Line separator character (U+2028)
Paragraph separator character (U+2029)But I don't know if this is what I'm
looking for or how to use it.Thanks in advance,Scott Natwick
 
S

Scott Natwick

Thanks, Mark

I was trying to use "\n" and it was blowing up. Once I switched to "\r\n",
like you suggested, it worked.

Scott
 
S

Scott Natwick

Thanks Kevin,

I tested the Environment.NewLine like you suggeted, it works! Just what I
needed to know.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top