Click here to Skip to main content
14,691,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm trying to send data through email like name,contact,email,city,message these data comes from html. how can i send these data into a email body.

What I have tried:

public void contactus()
        string name = Request.Form.Get("name");
        string contact = Request.Form.Get("contact No.");
        string email = Request.Form.Get("email");
        string city = Request.Form.Get("city");
        string message = Request.Form.Get("message");
        SmtpClient client = new SmtpClient("", 587);
        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("", "1857");
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("");
        msg.Subject = "new user data"; 

// i am using body to send data but it send only last body data
        msg.Body = name;
        msg.Body = contact;
        msg.Body = email;
        msg.Body = city;
        msg.Body = message;
        Message.Show("Data Submitted");
Updated 6-May-20 3:29am

Only a few things stood out to me about the message. The first of which is how you are populating msg.Body. As Richard pointed out, you are actually overwriting when you add onto it instead of appending to it.

The preferred way to build up a string is to use the appropriately named StringBuilder class. Combining this with the string.Format method can give us a a nice multi-lined message with the keys and values
StringBuilder sb = new StringBuilder();
  sb.AppendLine(string.Format("Name: {0}", name));
  sb.AppendLine(string.Format("Contact No: {0}", contact));
  sb.AppendLine(string.Format("Email: {0}", email));
  sb.AppendLine(string.Format("City: {0}", city));
  sb.AppendLine(string.Format("Message: {0}", message));

msg.Body = sb.ToString();
The other item is that you are using GMail as your email server. Google uses extra security measures to protect your account and it adds additional challenges to programmatic access- namely that their accounts cannot be accessed by most POP & SMTP (email) clients; which they refer to as Less Secure Apps. You have a few options on what you can do, outlined in the referenced article.

Microsoft Docs > StringBuilder Class[^]
Microsoft Docs > String.Format Method[^]
Google Accounts => Less secure apps & your Google Account[^]
You are replacing msg.body with each field in turn, so it will only contain message. You need to add everything after the first field using the += operator thus:
msg.Body = name;
msg.Body += contact;
msg.Body += email;
msg.Body += city;
msg.Body += message;

However, the content will end up looking something like
"John Doe1-800-123-4567johndoe:gmail.comSeattlePlease reply to this message"

You need to add more spaces and newline characters so the message is readable.

In practice it would probably be better to use a StringBuilder to create the message body before adding it to the actual message.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900