0

I just want to implement one sample mail sending application. I have done lot of research for this. From all the corners I am getting same kind of solution. But that is not working for me.I am unable to find my mistake. I am using below code

protected void SendEmail(object sender, EventArgs e)
{
    string to = txtTo.Text;
    string from = txtEmail.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
    using (MailMessage mm = new MailMessage(txtEmail.Text, txtTo.Text))
    {
        mm.Subject = txtSubject.Text;
        mm.Body = txtBody.Text;
        if (fuAttachment.HasFile)
        {
            string FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
            mm.Attachments.Add(new Attachment(fuAttachment.PostedFile.InputStream, FileName));
        }
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(txtEmail.Text, txtPassword.Text);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
    }
}

And getting the exception like below

enter image description here

please suggest me how can I solve this issue.

Thanks in advance.

Sagar
  • 371
  • 9
  • 26
  • Tried that proposed solution by doing turn off the "less secure apps" option from my gmail account it's giving same exception like I have mentioned above. – Sagar Feb 04 '17 at 11:16

1 Answers1

3

Assuming that you are using a correct Gmail credentials for the account with which you are sending emails. You need to turn on "less secure apps" setting using the below link: https://www.google.com/settings/u/1/security/lesssecureapps

Cyber Progs
  • 3,656
  • 3
  • 30
  • 39
Chintan Udeshi
  • 362
  • 1
  • 8
  • Thanks for your suggestion. Yes it's working fine.But I have one more doubt by turning on less secure apps option won't it cause any security issues. Is is secured now? – Sagar Feb 04 '17 at 10:54
  • It will not raise any security issues. There is another way , you can create App in Gmail and use that App for sending emails. – Chintan Udeshi Feb 04 '17 at 11:03
  • Could you please give your suggestions to do that? – Sagar Feb 04 '17 at 11:06
  • check out this link https://www.emailarchitect.net/easendmail/sdk/html/object_oauth.htm let me know if you have any further query – Chintan Udeshi Feb 04 '17 at 12:47