-2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.ComponentModel;// for backgroundworker class
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Configuration;
namespace mail_demo_project
{
  public partial class register_mail_demo : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {

      }
      protected void submit_Click(object sender, EventArgs e)
      {
        try
        {
            string From = "rashmi1810214@gmail.com";
            MailAddress mailAddress = new MailAddress(From, "Contact Us");
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("upadhyaya.monika@gmail.com");
            mailMessage.From = mailAddress;
            mailMessage.Subject = subject_textbox.Text;
            string mailBody = "User Email: " + message_box_textbox.Text + "
            <br/> User Name: " + to_username_textbox.Text + "<br/> Message: 
            " + message_box_textbox.Text;
            mailMessage.Body = mailBody;
            mailMessage.Priority = MailPriority.Normal;
            mailMessage.IsBodyHtml = true;
            System.Net.NetworkCredential credential = new 
            System.Net.NetworkCredential("rashmi1810214@gmail.com", 
            "***password***");
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = false;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = credential;
            smtpClient.Send(mailMessage);
            Response.Write("code successfully executed");
        }
        catch (Exception ex)
        {
            Response.Write("Exception caught in CreateTestMessage1(): {0}"+ 
            ex.ToString());
        }
    }
}
}

And error occurred :-

Exception caught in CreateTestMessage1(): {0}System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. d4sm6963507pfb.185 - gsmtp at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at mail_demo_project.register_mail_demo.submit_Click(Object sender, EventArgs e) in e:\dot net\projects\mail demo project\mail demo project\register_mail_demo.aspx.cs:line 76

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • 1
    Possible duplicate of [Sending email in .NET through Gmail](https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – mason Sep 22 '17 at 16:31
  • Did you do any research for others to see how they send email via Gmail in .NET? Did you see how they enable SSL, not disable it? – mason Sep 22 '17 at 16:33

1 Answers1

0

the mail server requires an SSL connection, but your disabled it

try this

smtpClient.EnableSsl = true;
Ray H
  • 491
  • 3
  • 7