The SMTP server requires a secure connection or th

2019-01-03 02:48发布

I want to send an email from my application and i have written following code for sending mail

    MailMessage msg = new MailMessage();

    msg.From = new MailAddress("mymailid");
    msg.To.Add("receipientid");
    msg.Subject = "test";
    msg.Body = "Test Content";
    msg.Priority = MailPriority.High;

    SmtpClient client = new SmtpClient();

    client.Credentials = new NetworkCredential("mymailid", "mypassword", "smtp.gmail.com");
    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = true;
    client.UseDefaultCredentials = true;

    client.Send(msg);

I am running it on localhost so what mistake i am doing to send it.

When i send button it gives an error like

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

Code in Web.config file

 <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />   
    <add key="smtpServer" value="smtp.gmail.com" />
    <add key="EnableSsl" value = "true"/>
    <add key="smtpPort" value="587" />
    <add key="smtpUser" value="sender@gmail.com" />
    <add key="smtpPass" value="mypassword" />
    <add key="adminEmail" value="sender@gmail.com" />
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp from="sender@gmail.com">
        <network host="smtp.gmail.com" password="mypassword" port="587" userName="sender@gmail.com"  enableSsl="true"/>
      </smtp>
    </mailSettings>
  </system.net>

what should i do to solve this error and send mail??

标签: c# smtp
15条回答
祖国的老花朵
2楼-- · 2019-01-03 03:26

Below is my code.I also had the same error but the problem was that i gave my password wrong.The below code will work perfectly..try it

            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");              
            mail.From = new MailAddress("fromaddress@gmail.com");
            mail.To.Add("toaddress1@gmail.com");
            mail.To.Add("toaddress2@gmail.com");
            mail.Subject = "Password Recovery ";
            mail.Body += " <html>";
            mail.Body += "<body>";
            mail.Body += "<table>";

            mail.Body += "<tr>";
            mail.Body += "<td>User Name : </td><td> HAi </td>";
            mail.Body += "</tr>";

            mail.Body += "<tr>";
            mail.Body += "<td>Password : </td><td>aaaaaaaaaa</td>";
            mail.Body += "</tr>";

            mail.Body += "</table>";
            mail.Body += "</body>";
            mail.Body += "</html>";

            mail.IsBodyHtml = true;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("sendfrommailaddress.com", "password");
            SmtpServer.EnableSsl = true;

            SmtpServer.Send(mail);

You can reffer it in Sending mail

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-03 03:26

If it's a new google account, you have to send an email (the first one) through the regular user interface. After that you can use your application/robot to send messages.

查看更多
beautiful°
4楼-- · 2019-01-03 03:28

You should consider to specify SMTP configuration data in config file and do not overwrite them in a code - see SMTP configuration data at http://www.systemnetmail.com/faq/4.1.aspx

<system.net>
            <mailSettings>
                <smtp deliveryMethod="Network" from="admin@example.com">
                    <network defaultCredentials="false" host="smtp.example.com" port="25" userName="admin@example.com" password="password"/>
                </smtp>
            </mailSettings>
        </system.net>
查看更多
登录 后发表回答