views:

4922

answers:

5

Hi,

I'm having trouble with this code sending email using my gmail account. Im pulling my hair out.

The same settings work fine in Thunderbird.

Heres the code. I've 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("[email protected]", "[email protected]", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);

Heres the error

"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 "

Heres the 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()
A: 

In order to prevent spamming, it is necessary that you authenticate when you want to sent mail accross domains.

you can simply do this:

SmptClient client = new SmtpClient();
...
SmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials

and it should work.

Frederik Gheysels
Aparently he did that. ss.Credentials = new NetworkCredential("username", "pass");ss.EnableSsl = true;
Bdiem
Those (CredentialCache.DefaultNetworkCredentials) won't work i assume
Bdiem
How do I specify the logon details If I do that?
Sir Psycho
+1  A: 

Stackoverflow said before

In your case it means u have to send with the email adress you logged into google with.

Stackoverflow also says

So maybe theres a firewall that interferes the connection. Im encountering this problem right now while testing your code. Try the suggested TELNET-Test.

Bdiem
This is not working. I've tried my username and [email protected]. Still same problem.
Sir Psycho
+6  A: 

This is silly, you won't belive what fixed my problem.

This line,

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

must be declared AFTER

ss.UseDefaultCredentials = false;

So the final working code is

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("[email protected]", "[email protected]", "subject here", "my body");
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
ss.Send(mm);

Is this a bug?

Sir Psycho
I would say no, because when I changed to UseDefaultCredentials = true, I would want any existing credentials be be overwritten.
kenny
Yes but we're encountering the other way here. Overwritten Credentials if you set DefaultCredentials to false. That doesn't sound right.
Bdiem
Kenny, it seems a bit counter intuitive that you need to declare properties in the right order.
Sir Psycho
You can still get that error when exceeding sending limits, or if the machine in question has never had a real user logged on before through a browser. It is confusing Google returns that error, but they do.
Jason Short
encountered same bug, UseDefaultCredentials must be set first before using Credentials property. a bit counter-intuitive, properties should not depend on order
Michael Buen
thanks bro just what I needed!!!!
Jean Paul
A: 

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

Johann Blake
A: 

Impossible. I tried everything. It is not possible to use a googlemail account for automaticall sending emails. One day it works, the other day the email is refused. I surrender..

Laf