0

I am having a web application with contact us page in that i am having field like Name: email address: phone number : Attach file : Message: Send button

Now on send button click, i want to send the email to some [email protected] with the above body content.

to do some what code i need to write in .cs file

this is what i have tried

protected void Button_Click(object sender, EventArgs e)
{
    try
    {
        if (Page.IsValid)
        {
            MailMessage mail = new MailMessage();
            SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");

            mail.From = new MailAddress("[email protected]");
            mail.To.Add("[email protected]");

            mail.Body = "<b> Sender Name : </b>" + txtbname.Text + "<br/>"
                + "<b> Sender Email : </b>" + txtbemail.Text + "<br/>"
                + "<b> Sender Contact Number : </b>" + txtphone.Text + "<br/>"
                + "<b> Message : </b>" + txtbmessage.Text;

            System.Net.Mail.Attachment attachment;

            attachment = new System.Net.Mail.Attachment("Attachment" + this.fp);

            mail.Attachments.Add(attachment);

            mail.IsBodyHtml = true;


            smtpServer.Port = 587;
            smtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "password");

            smtpServer.EnableSsl = true;

            smtpServer.Send(mail);

            label1.ForeColor = System.Drawing.Color.Green;
            label1.Text = "SENT";
        }
    }
    catch (Exception ex)
    {
        label1.ForeColor = System.Drawing.Color.Red;
        label1.Text = "Failed";
    }
}
5
  • Does it work without attachment? What version of Net are you using? Commented Jun 18, 2016 at 11:57
  • What is the error you are getting Commented Jun 18, 2016 at 12:21
  • No i wont work either Commented Jun 18, 2016 at 13:32
  • Midhun Mundayadan When i remove try block following error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at Commented Jun 18, 2016 at 13:34
  • Yes this error is because you have to Allow less secure apps from your gmail account Commented Jun 19, 2016 at 5:02

1 Answer 1

2

For sending mail you have to Allow less secure apps from your gmail account

  1. Login with your gmail account and find "Allow less secure apps:" from here.

  2. Google manages security with your gmail account. You need to turn on "Allow less secure apps:" and you will receive mail in your gmail account.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.