C # SmtpClient class not able to send email using gmail

I'm having trouble sending emails using my gmail account. I pull my hair out.

The same settings work fine in Thunderbird.

Here is the code. I also tried port 465 with no luck.

SmtpClient ss = new SmtpClient("smtp.gmail.com", 587); ss.Credentials = new NetworkCredential("username", "pass"); ss.EnableSsl = true; ss.Timeout = 10000; ss.DeliveryMethod = SmtpDeliveryMethod.Network; ss.UseDefaultCredentials = false; MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body"); mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; ss.Send(mm); 

Gets an error

"A secure connection is required for the SMTP server or the client failed authentication. Server response: 5.5.1 Authentication is required. Read more in the section

Gets a stack trace

  at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at email_example.Program.Main(String[] args) in C:\Users\Vince\Documents\Visual Studio 2008\Projects\email example\email example\Program.cs:line 23 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() 
+43
c # smtpclient gmail
Aug 21 '09 at 12:51
source share
7 answers

You will not believe that fixed my problem.

Credentials Property

 ss.Credentials = new NetworkCredential("username", "pass"); 

to be announced after

 ss.UseDefaultCredentials = false; 

Thus, the final list of work codes

 SmtpClient ss = new SmtpClient("smtp.gmail.com", 587); ss.EnableSsl = true; ss.Timeout = 10000; ss.DeliveryMethod = SmtpDeliveryMethod.Network; ss.UseDefaultCredentials = false; ss.Credentials = new NetworkCredential("username", "pass"); MailMessage mm = new MailMessage("donotreply@domain.com", "destination@domain.com", "subject here", "my body"); mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; ss.Send(mm); 

This is mistake?

+59
Aug 21 '09 at 13:55
source share

stack overflow

In your case, this means that you must send it with the email address with which you are logged in to Google.

stack overflow

So there may be a firewall that interferes with the connection. I am facing this problem right now while checking your code. Try the suggested TELNET test.

+5
Aug 21 '09 at 12:58
source share

Work with me only with port 25.

+2
Sep 23 '11 at 11:33
source share

It works, but it is not very convenient. Check out the .SendAsync client: http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx

Usage example:

  var message = new MailMessage("from", "to", "subject", "body"); var client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("login", "password"), EnableSsl = true }; client.SendCompleted += (s, e) => { client.Dispose(); message.Dispose(); }; client.SendAsync(message, null); 
+1
Nov 21
source share

This work is excellent. Create a letter template in a separate MailTemplate.html file.

Add genuine NetworkCredentials - username and password

 private void SendMail() { string filename = Server.MapPath("~/MailTemplate.html"); string username = UserName.Text.ToString(); string mailbody = System.IO.File.ReadAllText(filename); mailbody = mailbody.Replace("##NAME##", username); string to = Email.Text; string from = "test@gmail.com"; MailMessage message = new MailMessage(from, to); message.Subject = "Auto Response Email"; message.Body = mailbody; message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; SmtpClient client = new SmtpClient("smtp.gmail.com", 587); System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("test@gmail.com", "test123#"); client.EnableSsl = true; client.UseDefaultCredentials = true; client.Credentials = basicCredential; try { client.Send(message); SuccessMessage.Text = "Email Sending successfully"; } catch (Exception ex) { ErrorMessage.Text = ex.Message.ToString(); } } 

MailTemplate.html

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Title</title> </head> <body> <div style="border: thin solid #0066FF; width: 550px; margin: 25px auto; padding: 15px; font-family: 'Microsoft Himalaya'; font-size: x-large; font-style: normal; color: #0066FF; background-color: #EfEFF2;"> <br /> <p style="vertical-align: middle">Dear ##NAME##,</p> </div> </body> </html> 
+1
Jan 14 '16 at 3:59
source share

I can verify that setting UseDefaultCredentials to false MUST be done before NetworkCredentials are created. I had the same problem.

0
Mar 22
source share

It works fine in my case:

 private void btnTestConnection_Click(object sender, EventArgs e) { btnTestConnection.Enabled = false; SmtpClient ss = new SmtpClient(txtSmtpHostName.Text.Trim(), Convert.ToInt32(numSmtpHostPort.Value)); ss.EnableSsl = chkSmtpSecureType.Checked; ss.Timeout = 10000; ss.DeliveryMethod = SmtpDeliveryMethod.Network; ss.UseDefaultCredentials = false; ss.Credentials = new NetworkCredential(txtSmtpAccount.Text.Trim(), txtSmtpPassword.Text); MailMessage mm = new MailMessage(txtSmtpFromEmail.Text.Trim(), "test@domain.com", "subject", "my body"); mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; ss.SendCompleted += (s, b) => { ss.Dispose(); mm.Dispose(); }; try { ss.Send(mm); ss.Dispose(); mm.Dispose(); MessageBox.Show("Connection successfully"); } catch (Exception ep) { MessageBox.Show("Connection error: " + ep.Message, "Smtp Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } btnTestConnection.Enabled = true; } 
0
Jul 29 '15 at 7:16
source share



All Articles