Abraham Olatubosun

Abraham Olatubosun

  • NA
  • 471
  • 113.7k

Error When Sending Mail With Asp.net C#

Jan 24 2017 10:08 AM
Hello all,
I want to send a mail after my user fill a form but i keep getting the following error "The smtp server requires a secure connection or the client was not authenticated 5.5.1. Authentication required". bellow is my code :
 
  1. int cnt;  
  2.        try  
  3.        {  
  4.            mEmail = Session["mEmail"].ToString();  
  5.           // string password = "abraham@2013";  
  6.            MailMessage Msg = new MailMessage();  
  7.            Msg.From = new MailAddress(mEmail.Trim());  
  8.            Msg.To.Add("[email protected]"); // create a frame for HR department  
  9.            Msg.Bcc.Add("[email protected]");  //blind copy me  
  10.            StreamReader reader = new StreamReader(Server.MapPath("~/SendMail.html"));  
  11.            string readFile = reader.ReadToEnd();  
  12.            string StrContent = "";  
  13.            StrContent = readFile;  
  14.            //Here replace the name with [MyName]  
  15.            StrContent = StrContent.Replace("[MyName]", lblname.Text);  
  16.            string mSubject = "APPLICATION FOR ANNUAL LEAVE";  
  17.   
  18.            StrContent = StrContent.Replace("[subject]", mSubject);  
  19.            StrContent = StrContent.Replace("[date1]", txtFromDate.Text.Trim());  
  20.            StrContent = StrContent.Replace("[date2]", txtToDate.Text.Trim());  
  21.            StrContent = StrContent.Replace("[ddays]",txtNODays.Text.Trim())+" days";  
  22.            Msg.Subject = mSubject + " FROM " + lblname.Text;  
  23.            Msg.Body = StrContent.ToString();  
  24.            Msg.IsBodyHtml = true;  
  25.            SmtpClient smtp = new SmtpClient();  
  26.            smtp.Host = "smtp.gmail.com"//"mail.dnextageweb.com";    //  
  27.            smtp.Port = 587;  
  28.            smtp.EnableSsl = true;  
  29.            smtp.Timeout = 40000;  
  30.            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;  
  31.            smtp.UseDefaultCredentials = false;  
  32.            smtp.Credentials = new NetworkCredential("[email protected]""Delladv!973@");  
  33.              
  34.            smtp.Send(Msg);  
  35.               
  36.            webMessage.Show("Mail sent successful.....");  
  37.            return cnt = 1;  
  38.        }  
  39.        catch (Exception ex)  
  40.        {  
  41.            webMessage.Show("Error : " + ex.Message.ToString());  
  42.            return cnt = -1;  
  43.        }  

 
 please i have tried all i know, i need your help, how to get smtp.gmail.com to work.
 
Thank you all 

Answers (4)