Reputation: 4103
I have a Problem, when i m sending a mail through this code then error is occured that "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. "
and my code is :
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]");
string body = "<table><tr><td>Company Name:</td><td>" + txt_cname.Text + "</td></tr><tr><td>Address With No.:</td><td>" + txt_addwithno.Text + "</td></tr><tr><td>Contact Person:</td><td>" + txt_conperson.Text + "</td></tr><tr><td>Email Id</td><td>" + txt_email.Text + "</td></tr><tr><td>Description</td><td>" + txt_description.Text + "</td></tr></table>";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 25;
smtp.UseDefaultCredentials = true;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "xyz");
smtp.EnableSsl = true;
smtp.Send(mail);
txt_cname.Focus();
txt_cname.Text = "";
txt_addwithno.Text = "";
txt_conperson.Text = "";
txt_email.Text = "";
txt_description.Text = "";
}
Upvotes: 5
Views: 21122
Reputation: 1116
Microsoft States that SmtpClient is not recommended. You should use MailKit it will work for sure.
https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.0
We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.
Upvotes: 0
Reputation: 369
I was trying to set this up through SQL Server 2012 Database Mail, and was getting the same message.
I was never able to get it to work using my primary g-mail account, which uses double authentication, even after using an Application-specific password. I ended up trying my secondary account, which does not have double auth, and it worked immediately. So for anyone who can't get it working using one of the solutions above, try it with a standard auth g-mail account.
Upvotes: 0
Reputation:
The port doesn't really matter as System.Net.Mail does not support implicit SSL, in other words it totally ignores the "Port" parameter.
Upvotes: 0
Reputation: 3021
Google may prevent an application from accessing your account
Here you can enable applications to access google account with your credentials:
https://accounts.google.com/DisplayUnlockCaptcha
Upvotes: 15
Reputation: 515
Try the port 587(it worked for me ) and also set UseDefaultCredentials to false. Set this and try to send the mail
Upvotes: 2
Reputation: 2155
i think you are using the wrong port.
you should use 465
http://support.google.com/mail/bin/answer.py?hl=en&answer=13287
cheers
Upvotes: 1
Reputation: 42910
I think you should set UseDefaultCredentials to false, since you define credentials for the connection.
Set this property to true when this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user.
[...]
If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server
Upvotes: 4