Click here to Skip to main content
15,883,811 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hii all

i am getting an error in sending mail module in my asp .net web application.

my mail module is working properly in local host.. when i publish my application

my btnsnd_click code is
C#
string usertype = string.Empty;
DataTable dt = new DataTable();
try
{
    for (int i = 0; i < grEmp.Rows.Count;i++ )
    {
        CheckBox cb = (CheckBox)grEmp.Rows[i].FindControl("chkSelect");


        if (cb.Checked == true)
        {
            if (cb != null && cb.Checked)
            {
               
                string emailid = grEmp.Rows[i].Cells[1].Text;
           
                SendEmailUsingGmail(emailid);
            
                dt.Clear();
                dt.Dispose();
            }
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Emails sent successfully');", true);
        }
    }
    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Please check CheckBox');", true);
}
catch (Exception ex)
{
    Response.Write("Error occured: " + ex.Message.ToString());
}
finally
{
    usertype = string.Empty;
 }
}

private void SendEmailUsingGmail(string toEmailAddress)
{
    try
    {
        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new NetworkCredential("anilkumar.hrs@gmail.com", "anil");
        smtp.Port = 587;
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        MailMessage message = new MailMessage();
        message.From = new MailAddress("anilkumar.hrs@gmail.com");
        message.To.Add(toEmailAddress);
        message.Subject = TxtSubject.Text;
        message.Body = TxtMessage.Text;
        smtp.Send(message);
    }
    catch (Exception ex)
    {
        Response.Write("Error occured: " + ex.Message.ToString());
    }
}

the error is
C#
Error occured: 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
Posted
v4
Comments
[no name] 29-Mar-14 15:14pm    
You did not format your code so that it is readable. And you did not tell us what the error is. We are not mind readers.

Refer - Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required[^].
Quote:

This generally happens when you try login from different time zone/IP/Computer. You production server and the mail id you have used both are in different time zone. Choose any of the one solution:


1) Log in to production server via remote access, and sing in to gmail once with your credentials. They will ask for the confirmation, confirm it and log out.


Or 2) log in gmail to your local computer, Follow this Link and choose review this activity and take proper actions.

 
Share this answer
 
v2
To Fix THis Problem Go tO GMAIL

CHANGE SECURITY SETTING ... For Access Less Secure App Turn it OFF and Send mail with above code

any problem contact : chandramani.sri @ gmail.com
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900