-1

I've been having issues sending a simple email through my ASP.NET application with my gmail account. I've looked for help as best I could, but for some reason it just keeps giving me an error and not sending any email.

Here is my code that I have set up to trigger during a button press:

protected void SubmitButton_Click(object sender, EventArgs e)
{
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
    mail.To.Add(new System.Net.Mail.MailAddress("an email address"));
    mail.From = new System.Net.Mail.MailAddress("From Email Address");
    mail.Subject = "PDMS New User Request";
    mail.IsBodyHtml = true;
    mail.Body = "A new user has requested access" + "\n\n"
        + "Name: " + inputFName.Text.ToString() + " " + inputLName.Text.ToString()
        + "\n"
        + "Organization: " + inputOrg.Text.ToString()
        + "\n"
        + "Email: " + inputEmail.Text.ToString()
        + "\n\n"
        + "Please contact them with their login information"
        + "-PDMS System Message";
    mail.IsBodyHtml = true;
    System.Net.Mail.SmtpClient systemEmail = new System.Net.Mail.SmtpClient();
    systemEmail.UseDefaultCredentials = false;
    systemEmail.Credentials=new System.Net.NetworkCredential("From email address", "From Email Addresses' password");
    systemEmail.Port = 587 ;
    systemEmail.Host="smtp.gmail.com";
    systemEmail.EnableSsl=true;
    systemEmail.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    try
    {
        systemEmail.Send(mail);
        Label1.Visible = true;
        Label2.Visible = false;
    }
    catch (Exception ex)
    {
        Label2.Text = "Email could not be sent due to errors";
    }

What is it that I am missing? I just can't seem to get this code to actually send anything - is there a setting in Gmail that I need to configure first perhaps?

Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33
chapmanclay
  • 64
  • 1
  • 9

3 Answers3

2

Remove Label in Catch and throw the Exception.Then we can easily identify the exact exception otherwise it display the label default text.

try
{
    systemEmail.Send(mail);
    Label1.Visible = true;
    Label2.Visible = false;
}
catch (Exception ex)
{
    throw ex;//throw the exception
}

Solution :

I think you are facing 5.5.1 Authentication Required

The following steps will resolved your exception.

  • Enter the correct login password.
  • To remove 2-Step Verification.
  • You have to enable login from other timezone / ip for your google account.To do this follow the Click Here and allow access by clicking the continue button.
  • Go to security settings at the following Click here and enable less secure apps . So that you will be able to login from all apps.
Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33
0

I am unable to know what is the error you are getting, but yes there is a setting that needs to be done in your gmail account before you can start sending mails using code. Please open below page from your google account and turn on access to less secure apps.

https://www.google.com/settings/security/lesssecureapps

Please note, if you have turned on 2 step verification for your google account then you will not see a learn more link on the above mentioned page. From there you can create app specific password for your google account and then that app will be able to access and send mails through your google account.

If this does not solves your issue, please edit your post and add the exact error trace that you are receiving so that we can look into it.

Alok Gupta
  • 1,353
  • 1
  • 13
  • 29
0

Try this out ... I used it before so it may work

`
public void SendEmail() {

        MailMessage msg;
        string emailId = string.Empty;
        msg = new MailMessage();
        SmtpClient smtp = new SmtpClient();

        //sender email address
        msg.From = new MailAddress("youremail@gmail.com");

       //Receiver email address
        msg.To.Add("receiver@gmail.com");
        //email message subject
        msg.Subject = "some string";
        //email message body
        msg.Body = "Some string".Trim();
        msg.IsBodyHtml = true;
        smtp.Credentials = new NetworkCredential("yourEmail@gmail.com","yourpassword");
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        smtp.Send(msg);
    }`