Introduction
I was working on MVC (C#) application sending email through Gmail Server, meanwhile popped up me issue the server response was 5.5.1 authentication required, So we required action to set on security issue which is default off in Gmail Email settings. I have published an article on How to send SMS using C#, now a days it has become very necessary to integrate SMS API into our application.
Error Description
An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Gmail Code Snippet
Below is the code snippet using to send Email through Gmail Host:[HttpPost] public JsonResult SendEmail(string _name, string _phone, string _email, string _description) { string senderID = "youtemail@gmail.com"; string senderPassword = "demo#123"; string result = "Email Sent Successfully"; string body = " " + _name + " has sent an email from " + _email; body += "Phone : " + _phone; body += _description; try { MailMessage mail = new MailMessage(); mail.To.Add(senderID); mail.From = new MailAddress(senderID); mail.Subject = "My Test Email!"; mail.Body = body; mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address smtp.Credentials = new System.Net.NetworkCredential(senderID, senderPassword); smtp.Port = 587; smtp.EnableSsl = true; smtp.Send(mail); } catch (Exception ex) { result = "problem occurred"; Response.Write("Exception in sendEmail:" + ex.Message); } return Json(result); }
First login into Gmail Account from which Gmail credentials are you using to send email.
Now go to Gmail Settings Feature, it comes in your Gmail Inbox on right side, as you see in picture below.
Move to Security Feature
Now click on Gmail Setting Feature, move to security feature or alternatively can hit/paste this URL in your browser https://www.google.com/settings/security/lesssecureapps where you can see settings.
Access for less secure apps
- Turn off (default)
- Turn On
Click on Turn On option to authorize emails sending through while programming (MVC, C#, PHP, Java etc.) with required credentials in Gmail account.
Video: Authentication the Server Response was 5.5.1 Authentication required in gmail
Summary
Above will help to resolve issue the server response was 5.5.1 authentication required while sending email through Gmail credentials in programming languages like ASP.Net, MVC, C#, PHP, Java etc.
how to change the settings for other servers? i'm not using GMAIL, i am using internail mail server
ReplyDeleteIf you are not using like Gmail, Yahoo, Hotmail etc then you can ask from your hosting company to forward you SMTP server and Port, here we are using following detail, you need to change only.
Deletesmtp.Host = "smtp.gmail.com";
smtp.Port = 587;
Thank U sir...u just solved my problem with in a min.
Deletethanks man
ReplyDeleteyour tutorial was useful to me
ReplyDeleteiranians bless you
Thanks Mohammad. Sure, If any help more required, let me know.
Deletewhat are the steps for ymail and live mail sir?
ReplyDeleteChange this code smtp.Host = "smtp.gmail.com"; for both (Live/Yahoo) below:
Delete// For Live
smtp.Host = "smtp.live.com";
We can use both port 25 or 587
// For Yahoo
smtp.Host = "smtp.mail.yahoo.com";
We can use both port 25 or 587
Thank you.....
DeleteHello, i've done everything but all seems not working.Error says
ReplyDelete"smtp server requires a secure connection or the client was not authenticated.the server response was : 5.5.1 authentication required"
Here's my code....
Private Function SendErrorThroughEmail(FilePath As String) As String
Dim UserName As String = "myemail@gmail.com"
Dim tmpString As String
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
client.Credentials = New System.Net.NetworkCredential(UserName, "password")
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
Dim mail As MailMessage = New MailMessage
Dim attach As New Attachment(FilePath)
mail.From = New MailAddress(UserName)
mail.To.Add(New MailAddress(UserName))
mail.Subject = "Ok"
mail.Body = "This is a test message from myemail.gmail.com"
If FilePath <> " then" Then
mail.Attachments.Add(attach)
End If
mail.IsBodyHtml = True
Try
client.Send(mail)
tmpString = "OK"
Catch ex As Exception
tmpString = ex.Message
End Try
Return tmpString
End Function
remove these both lines and then try again
Deleteclient.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
hi I am doing the same program for sending the email and getting after removing those two lines I am getting this message
DeleteSystem.Net.Mail.SmtpException: SSL must not be enabled for pickup-directory delivery methods. at System.Net.Mail.SmtpClient.Send(MailMessage message)
try to replace this line
ReplyDeletesmtp.EnableSsl = false;
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
ReplyDeleteplz help my contact ni 9714411582
Did you try all required steps provided above in this article.
Deletehi anjan,...i have cofigued mail server in sql management...i have created a SSIS PKG & TRIED TO SEND MAIL USING SMTP TASK ...BUT GIVEN AN ERROR AS below..
Delete"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
yes that means you did not configured your email SMTP setting as mentioned in this article. try this one and let me know.
DeleteThanks for your quick reply...MY REQUIREMENT IS not as in above article.......can u please tell me how can we send a mail using SMTP task(not using any code like c#/vb.net) from SSIS Package using sql server SSDT(SQL SERVER 2012).from our laptop/machines(local system)...Thanks in advance........
ReplyDeletehere are articles which demonstrate how can we send email through sql server http://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Server, http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/
DeleteThanks anjan for replay...as u mentioned paths above are usefull but they are not provided full information...my requirement is need to send email notifIycation using SMTP task IN SSIS PACKAge(SQL SERVER 2012) not using any code like c#/vb.net....
Deleteyes, here is explained SMTP in C#,MVC,VB etc but not mentioned for SSIS, next time will include it.
DeleteThe SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
ReplyDeleteplz resolve my problem
already provided resolution of this issue, if any more facing issue then let me know.
DeleteHi Sir,
ReplyDeletei am getting this error msg
Mailbox unavailable. The server response was: Access denied - Invalid HELO name (See RFC2821 4.1.1.1)
while sending mail from specified domain
its working perfectly while i m using gmail.
how to fix it when using a specified host??
thanks in advance
It is clearly mean that you have to pass correct your specified domain given below information.
DeleteCross check in your hosting environment of Port No for SMTP, smtp.EnableSsl = true (if SSL have then true otherwise false or you can also remove this line) and SMTP Host and user name & password, moreover you can contact your hosting provider directly to confirm your mentioned info.
sir but when client sending mail he dont know how to change settings. is there any way to bypass this method and send email directly
ReplyDelete
DeleteClient don't need to make any changes, website owner have only permissions to make necessary changes in your domain settings. Alternatively, you can also use this link
Send Mail to send email directly.
you tutorial is very help
ReplyDeleteThe SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
ReplyDeleteplizz help me and my code is
private void button1_Click(object sender, EventArgs e)
{
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(textBox4.Text,textBox5.Text);
MailMessage msg = new MailMessage();
msg.To.Add(textBox1.Text);
msg.From = new MailAddress(textBox4.Text);
msg.Subject = textBox2.Text;
msg.Body = textBox3.Text;
client.Send(msg);
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
Now working fine your code given below, only add your emailid and password:
Deletetry
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("mygamil@gmail.com", "********");
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add("sendingemail@gmail.com");
msg.From = new MailAddress("test@gmail.com");
msg.Subject = "Test";
msg.Body = "testing body";
client.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Thank You so much
ReplyDeleteHello
ReplyDeleteI also meet trouble with this issue. I work on Sitefinity CMS with these configuration in the backend of CMS. Can you suggest me some solution for this :( Thanks in advance!
My configuration is here
Host: smtp.gmail.com
Port: 587
UserName: {myGmail}
Password: {myPassword}
Domain: gmail.com
DeliveryMethod: Network
EnableSSL: true
Timeout: 100000
PickupDirectoryLocation: {N/A}
DefaultSenderEmailAddreess: {the same as Gmail above}
EmailSubjectEncoding: utf-8
EmailBodyEncoding: utf-8
Can you try with this code, once this code send email properly then after add other detail.
Deletetry
{
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("mygamil@gmail.com", "********");
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.To.Add("sendingemail@gmail.com");
msg.From = new MailAddress("test@gmail.com");
msg.Subject = "Test";
msg.Body = "testing body";
client.Send(msg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Hello
ReplyDeleteI also meet trouble with this issue. I work on Sitefinity CMS with these configuration in the backend of CMS. Can you suggest me some solution for this :( Thanks in advance!
My configuration is here
Host: smtp.gmail.com
Port: 587
UserName: {myGmail}
Password: {myPassword}
Domain: gmail.com
DeliveryMethod: Network
EnableSSL: true
Timeout: 100000
PickupDirectoryLocation: {N/A}
DefaultSenderEmailAddreess: {the same as Gmail above}
EmailSubjectEncoding: utf-8
EmailBodyEncoding: utf-8
Did you try same above given example. It is working for so many time and moreover can you specify your issue so that I can look into issue.
DeleteVery helpful tutorial..Thankyou
ReplyDeleteThank you, its works for me.
ReplyDeleteSystem.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated.
ReplyDeleteAbove said error came on web server (Znet). My code is working properly on localhost only web server through this error.
Check with your hosting provider, can you send email through your domain.
DeleteIn localhost it works fine,
Deletein live provide error msg:
Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
help me my code is:
public ActionResult Email(RegInfo registration)
{
try
{
var from = System.Configuration.ConfigurationManager.AppSettings["UserName"];
var smtpHost = System.Configuration.ConfigurationManager.AppSettings["Host"];
var mailPwd = System.Configuration.ConfigurationManager.AppSettings["MailPwd"];
var smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
var callBackUrl = System.Configuration.ConfigurationManager.AppSettings["CallBackUrl"] + registration.ROLEBYID;
var msg = new MailMessage(from, registration.EMAIL) { Subject = "Verify Your Account" };
var Body = string.Format("Hi {0}
Your Account Registerd.
Your Id is :{1}
Please confirm your account by clicking here", registration.NAME, registration.ROLEBYID, callBackUrl);
msg.Body = Body;
msg.IsBodyHtml = true;
var smtp = new SmtpClient()
{
Host = smtpHost,
Port = smtpPort,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(from, mailPwd), EnableSsl = true
};
smtp.Send(msg);
return RedirectToAction("Create");
}
catch (Exception ex)
{
TempData["error"] = ex.Message;
return RedirectToAction("Create");
}
}
you need to a change in your web.config, set , it will resolve your problem.
Deleteset trust level "Full"
Deletehello Sir ,
ReplyDeleteI want to send mail with out password.......only provide gmail id not password.....can it possible
can you help me.....urgently....
Without authentication, you can't send email.
DeleteI am getting this error of "An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code
ReplyDeleteAdditional information: Failure sending mail. "
Also Turned on the option of ( Allow less secure apps: ON ) but still getting same error
here is my 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 = false;
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);
}
}
please help..
update your code line: smtp.EnableSsl = true; it should work for you.
DeleteFirst login into Gmail Account from which Gmail credentials are you using to send email... this is the solution. thank you
ReplyDeleteafter doing the less secure apps is ON,i m also getting the same error like The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
ReplyDeletei am using zoho mail for sending emails and getting following error :
ReplyDelete"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "-
please give a solution