Oz Cohen
Oz Cohen

Reputation: 963

Sending eMail from my web site

the eror:

System.Net.Mail.SmtpException: 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 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 forgot.Button1_Click(Object sender, EventArgs e) in c:\Users\Champion\Desktop\DinoSite\forgot.aspx.cs:line 24

the c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class forgot : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            using (var client = new SmtpClient())
            {
                MailMessage mail = new MailMessage("[email protected]", (string)Email.Text);
                mail.Subject = "this is a test email.";
                mail.Body = "this is my test email body";
                client.Send(mail);
            }
            lblError.Text = "Message sent!";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.ToString();
        }
    }
}

at the Web config:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.net>
    <mailSettings>
        <smtp deliveryMethod="Network" from="[email protected]">
                <network host="smtp.gmail.com" userName="ozcohen06" password="mypass" port="587" enableSsl="true" />
        </smtp>
    </mailSettings>
</system.net>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
</configuration>

Upvotes: 1

Views: 653

Answers (2)

Hardik V.
Hardik V.

Reputation: 374

→ Use SMTP secured port: 465

→ Force your script to make SMTP authentication:

mail.UseDefaultCredentials = false;
mail.Credentials = basicCredential;

Upvotes: 0

Mitesh Gangaramani
Mitesh Gangaramani

Reputation: 307

Points to taken into consideration :

1) Access for less secure apps is "turned On" https://www.google.com/settings/security/lesssecureapps

2) Enable login from other timezone/IP for your google account https://g.co/allowaccess

Upvotes: 1

Related Questions