locked
WebForm1.aspx cannot send email with gmail SMTP RRS feed

  • Question

  • User1868852945 posted

    Below is c# code for a webform I need to send email. After that I show the error message I get when EnableSSL is true and when it is false.

    I am running this from VS 2013 localhost
    Thanks if you can advise what I am doing wrong.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using Oracle.ManagedDataAccess.Client;
    using SchemaTrack.Models;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Net;

    namespace SchemaTrack
    {
    public partial class WebForm1 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    try {
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

    mail.From = new MailAddress("jay.gunderson@schematrack.com");
    mail.To.Add("gunderson3@yahoo.com");
    mail.Subject = "test this is the subject";
    mail.Body = "this is the body";
    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["jay.gunderson@schematrack.com"],
    ConfigurationManager.AppSettings["myPassword"]);
    SmtpServer.EnableSsl = true;
    }
    catch (Exception ex)
    {
    }
    }
    }
    }

    error when EnableSSL is true...
    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

    error when EnableSSL is false...
    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. m65sm148049909pfg.180 - gsmtp

    Saturday, January 12, 2019 4:26 PM

Answers

  • User753101303 posted

    Hi,

    If I remember when this message includes the "Learn more at", you are blocked by some additional safety feature (for example if sending that mail from some foreign country because your hosting service is there, it is considered as a suspect activity by gmail). And so you should get a mail that gives instructions to bypass this safety check.

    Manually log to this mailbox and check for incoming messages from the gmail service.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2019 5:15 PM

All replies

  • User475983607 posted

    error when EnableSSL is false...
    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. m65sm148049909pfg.180 - gsmtp

    This is a pretty common question on the forum, either gmail blocked the SMTP request or you credentials are incorrect.  Please login to your gmail account and look for any security related errors.   IIRC, you need to allow your SMTP client access through gmail. 

    gundererj, the code snippet shown is incomplete and can't possible produce the error.  That tell me you are not showing us the actual code and IMHO, not cool.

    Saturday, January 12, 2019 4:34 PM
  • User-943250815 posted

    gunderj,
    Did you try User Name and Password as string instead of get values from AppSettings?

    SmtpServer.Credentials = new System.Net.NetworkCredential("jay.gunderson@schematrack.com","myPassword");

    Saturday, January 12, 2019 4:56 PM
  • User753101303 posted

    Hi,

    If I remember when this message includes the "Learn more at", you are blocked by some additional safety feature (for example if sending that mail from some foreign country because your hosting service is there, it is considered as a suspect activity by gmail). And so you should get a mail that gives instructions to bypass this safety check.

    Manually log to this mailbox and check for incoming messages from the gmail service.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 12, 2019 5:15 PM
  • User1868852945 posted

    Yes, it is the "Let less secure apps access you account" setting.

    Thanks.

    Saturday, January 12, 2019 5:48 PM