5
Answers

Send Mail is not working asp.net web forms

Hi,
 
I got an error like this.
 
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
 
Source Error:
tsmtpClient.Send(tmailmessage);
 
private static void SenEmail(string from, string to, string cc, string bcc, string subject, string body, bool IsHTML)
{
//create an instant in mail message
MailMessage tmailmessage = new MailMessage();
//assign sender mail address
tmailmessage.From = new MailAddress(from);
//asssign recipiant email address
tmailmessage.To.Add(new MailAddress(to));
//check if cc/bcc is not null
if (cc != null && cc != "")
tmailmessage.CC.Add(new MailAddress(cc));
if (bcc != null && bcc != "")
tmailmessage.Bcc.Add(new MailAddress(bcc));
//assign the subject
tmailmessage.Subject = subject;
//assign the mail body
tmailmessage.Body = body;
//assign the format into mail box
tmailmessage.IsBodyHtml = IsHTML;
//assign the prority of the mail into normal
tmailmessage.Priority = MailPriority.Normal;
//subject encoding by UTF-8
tmailmessage.SubjectEncoding = System.Text.Encoding.UTF8;
//body encoding by UTF-8
tmailmessage.BodyEncoding = System.Text.Encoding.UTF8;
//Create a new instance of SMTP client and pass name port number from SMTP gmail server
SmtpClient tsmtpClient = new SmtpClient("smtp.gmail.com", 587);
//enable SSL of theamtp client
tsmtpClient.EnableSsl = true;
//use delivery method as network
tsmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
//use the Credentials set to false
tsmtpClient.UseDefaultCredentials = false;
//pass account information to the sender
tsmtpClient.Credentials = new System.Net.NetworkCredential("Email", "Password");
tsmtpClient.Send(tmailmessage);
}
Answers (5)
1
Aman

Aman

NA 4.8k 927.5k 7y
Since for the security purpose Gmail will by default restrict few apps to access from outside and because of that you are getting this error . So just visit this link https://www.google.com/settings/security/lesssecureapps and "Turn On" the "Access for less secure apps" option. It should help .
 
If not then sometimes google may block access from the apps if it is getting accessed from some other region (because may be from hosted server). In that case just open the server in that region and try login your gmail from the browser from the same region where your app is accessing your account.
 
I hope first option will work for you but if not try the second one after both of this it will definitely work.
 
Let me know if any help needed for the same
0
Suraj Kumar

Suraj Kumar

NA 1.3k 14.3k 7y
When you enable less secure access in your account by login to gmail in that case gmail automatically send a mail to your about intimation to less secure app access. You have to accept that mail as highlighted in the screen shot.
 
Believe I have already checked and run your code and I was getting the same error but after enabling the less secure app and running your code it is sending mail successfully and the code which I have provided is also sending mail.
 
Your username should be fully qualified for example, --- Nimanaqipour@gmail.com 
 
0
Amit Kumar

Amit Kumar

NA 3.5k 203.6k 7y
Hi Dear , Now a days It is very common issue For solve it you do change the some setting on ur gmail account go to the below link ................https://myaccount.google.com/intro/security .......... .. Once you visit the link you need to modify the Less Secure Apps setting and Turn On access to Less Secure Apps as shown below. The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
0
Kasuni Abegunawardana

Kasuni Abegunawardana

NA 137 7.5k 7y
I turned on "Access for less secure apps" but still i got an error like previous.
 
In web config i have added follow lines and i gave my real user name and password too.
 
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.nimanaqipour.com" userName="Nimanaqipour" password="mypass" port="587"/>
</smtp>
</mailSettings>
</system.net>
0
Suraj Kumar

Suraj Kumar

NA 1.3k 14.3k 7y
Your code is also sending but you have to First enable less secure app access by the following link
 
https://myaccount.google.com/lesssecureapps
 
and then write the actual email and actual password in second last line of your code. Replace Email with actual email and Password with actual password
 
tsmtpClient.Credentials = new System.Net.NetworkCredential("Email", "Password");
 
alternative you can follow the below code also to send the mail.
 
private void SendMail()
{
try
{
using (MailMessage mm = new MailMessage("testmail@gmail.com", "testmail@rediffmail.com"))
{
mm.Subject = "Sending Mail";
mm.Body = "Hi Suraj, How are you?";
mm.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("testmail@gmail.com", "testPassword");//Provide here your email id and password from which you want to send the mail.
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}