Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Formatting Email's body using MailMessage class

Formatting Email's body using MailMessage class

Scheduled Pinned Locked Moved ASP.NET
comhelpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tina P
    wrote on last edited by
    #1

    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);

    A 1 Reply Last reply
    0
    • T Tina P

      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);

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      Try with this StringBuilder_emilBody= new StringBuilder(); _emailBody.Append("

      This is your information

      "); _emailBody.Append("

      N T 2 Replies Last reply
      0
      • A Abhijit Jana

        Try with this StringBuilder_emilBody= new StringBuilder(); _emailBody.Append("

        This is your information

        "); _emailBody.Append("

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        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

        T A 2 Replies Last reply
        0
        • A Abhijit Jana

          Try with this StringBuilder_emilBody= new StringBuilder(); _emailBody.Append("

          This is your information

          "); _emailBody.Append("

          T Offline
          T Offline
          Tina P
          wrote on last edited by
          #4

          Thanks Abhijeet, this really does make sense. I'm going to try it out shortly, thanks again!

          1 Reply Last reply
          0
          • N N a v a n e e t h

            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

            T Offline
            T Offline
            Tina P
            wrote on last edited by
            #5

            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!

            N A 2 Replies Last reply
            0
            • N N a v a n e e t h

              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

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              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

              T 1 Reply Last reply
              0
              • T Tina P

                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!

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #7

                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. Use String.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

                1 Reply Last reply
                0
                • T Tina P

                  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!

                  A Offline
                  A Offline
                  Abhijit Jana
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • A Abhijit Jana

                    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

                    T Offline
                    T Offline
                    Tina P
                    wrote on last edited by
                    #9

                    Thank you guys, both of ya'll are great...I appreciate the help. However; this thing is pretty small (i.e. the content of the email is probably gonna be a few lines) so we should be ok there. Thanks once again. Tina

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups