Formatting Email's body using MailMessage class
-
Hello folks, I'm going to be grabbing pieces of information from a page (webform, various text boxes such as Name, Number, Address etc) and need to put all that stuff together in the form of an email and fire it off to the administrator at admin@email.com. The problem is how do i set the Body property, coz it only takes string input...since I'll be gathering info from various text boxes, I can't just set the Text property of the textbox to the Body property of teh MailMessage object. I have a feeling I have to do string manipulation here. How do I grab the values from these various text boxes and put 'em all together (perhaps one in each line) for instance like this: Name: Dillon Edward Num: 343 Address: 123 Main st. Here's what I have so far: MailMessage msg = new MailMessage("abc@yahoo.com", "admin@email.com"); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; msg.Subject = "Attention - There has been a new user that needs to be looked at"; msg.Body =
Name
I guess here I would have to do textBoxName.Text BUT HOW ; msg.IsBodyHtml = true; //Instentiating SMTP client SmtpClient smtp = new SmtpClient(); smtp.Send(msg);
-
Hello folks, I'm going to be grabbing pieces of information from a page (webform, various text boxes such as Name, Number, Address etc) and need to put all that stuff together in the form of an email and fire it off to the administrator at admin@email.com. The problem is how do i set the Body property, coz it only takes string input...since I'll be gathering info from various text boxes, I can't just set the Text property of the textbox to the Body property of teh MailMessage object. I have a feeling I have to do string manipulation here. How do I grab the values from these various text boxes and put 'em all together (perhaps one in each line) for instance like this: Name: Dillon Edward Num: 343 Address: 123 Main st. Here's what I have so far: MailMessage msg = new MailMessage("abc@yahoo.com", "admin@email.com"); msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; msg.Subject = "Attention - There has been a new user that needs to be looked at"; msg.Body =
Name
I guess here I would have to do textBoxName.Text BUT HOW ; msg.IsBodyHtml = true; //Instentiating SMTP client SmtpClient smtp = new SmtpClient(); smtp.Send(msg);
Try with this StringBuilder_emilBody= new StringBuilder(); _emailBody.Append("
This is your information
"); _emailBody.Append("
-
Try with this StringBuilder_emilBody= new StringBuilder(); _emailBody.Append("
This is your information
"); _emailBody.Append("
Appending in a string builder is tedious. I'd create a HTML page with required design and put placeholders where value gets replaced. Now just read this file and replace the placeholders with real values and assign to mail body. This helps to change the mail design quickly. :)
Navaneeth How to use google | Ask smart questions
-
Try with this StringBuilder_emilBody= new StringBuilder(); _emailBody.Append("
This is your information
"); _emailBody.Append("
-
Appending in a string builder is tedious. I'd create a HTML page with required design and put placeholders where value gets replaced. Now just read this file and replace the placeholders with real values and assign to mail body. This helps to change the mail design quickly. :)
Navaneeth How to use google | Ask smart questions
Naveneeth, I'm not sure If I fully understand what you are suggesting...but the string builder approach that Abhijeet mentioined does make sense, are you aware of any potentiall problems with that...since I'm reading data not from just one location, but from different text boxes, I think putting them all together would be possible only through string builder!
-
Appending in a string builder is tedious. I'd create a HTML page with required design and put placeholders where value gets replaced. Now just read this file and replace the placeholders with real values and assign to mail body. This helps to change the mail design quickly. :)
Navaneeth How to use google | Ask smart questions
N a v a n e e t h wrote:
Appending in a string builder is tedious. I'd create a HTML page with required design and put placeholders where value gets replaced. Now just read this file and replace the placeholders with real values and assign to mail body.
Exactly Navaneeth and I have done it in many cases where mail content is to large and lots of formatting are there. :)
cheers, Abhijit CodeProject MVP
-
Naveneeth, I'm not sure If I fully understand what you are suggesting...but the string builder approach that Abhijeet mentioined does make sense, are you aware of any potentiall problems with that...since I'm reading data not from just one location, but from different text boxes, I think putting them all together would be possible only through string builder!
Tina P wrote:
but the string builder approach that Abhijeet mentioined does make sense, are you aware of any potentiall problems with that
Maintenance will be tough. Think about a scenario where you need to change the mail design. You have to rework the string builder appending.
Tina P wrote:
I'm not sure If I fully understand what you are suggesting
It is very simple. You create a HTML page with the required design. Put place holders, something like {name} and replace this with the real value.
File.ReadAllText
will give you the HTML file's content. UseString.Replace
to replace the placeholder with a real value. So all the places where {name} is given will get replaced with real value. :)Navaneeth How to use google | Ask smart questions
-
Naveneeth, I'm not sure If I fully understand what you are suggesting...but the string builder approach that Abhijeet mentioined does make sense, are you aware of any potentiall problems with that...since I'm reading data not from just one location, but from different text boxes, I think putting them all together would be possible only through string builder!
Tina P wrote:
are you aware of any potentiall problems with that
No, He is not talking about any potential problem.
Tina P wrote:
I'm not sure If I fully understand what you are suggesting
Let me explain, If you are sending a mail to any one, you should look for proper formatting of the mail. Now for that you are using some HTML tag like BR, Table, Font etc. But, did you ever realized how boring is it to formatting text every time using String Builder. The suggestion I have provided to you, is good for small mailing application. But for a large content of mail and formatting you can understand what you have to do. Here, Navaneeth suggested, You can create a HTML file with proper formatting in any HTM L editor like Front page, Dream weaver etc. Put a placeholder on that file. You just read that file and replace the placeholder with value ( your case text box value). This will makes the things very easier.
cheers, Abhijit CodeProject MVP
-
N a v a n e e t h wrote:
Appending in a string builder is tedious. I'd create a HTML page with required design and put placeholders where value gets replaced. Now just read this file and replace the placeholders with real values and assign to mail body.
Exactly Navaneeth and I have done it in many cases where mail content is to large and lots of formatting are there. :)
cheers, Abhijit CodeProject MVP