8

This may be very trivial for you but i just couldn't figure out why am i getting this error message when i run my code. I looked some of the relative questions on this same website for eg Sending email through Gmail SMTP server with C# but none of them was helpful. Anyone willing to help please? using different assemblies are also acceptable. so if anyone got a working solution that would be appreciated.

Error Message = 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

here is my code

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();      
message.From = new MailAddress("bob@googlemail.com");
message.To.Add("bob@hotmail.com");
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("MyGoogleMailAccount", 
                                               "mygooglemailpassword");

smtpClient.Send(message.From.ToString(), message.To.ToString(), 
                message.Subject, message.Body);   
Community
  • 1
  • 1
Sike12
  • 1,232
  • 6
  • 28
  • 53
  • Duplicate http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp – Pete Sep 24 '13 at 17:40
  • 1
    Incidentally, gmail will probably complain if you use a hotmail from address. – Pete Sep 24 '13 at 17:42
  • Hi steve I want to use googlemail to send the email but it should act as if it was sent from hotmail. Am i missing something here? Hi pete i have already seen that link it didn't fix my problem – Sike12 Sep 24 '13 at 17:43
  • Thanks for the info Pete. i'll change the address and give it a go from Googlemail – Sike12 Sep 24 '13 at 17:43
  • I still get the same message even if i use the googlemail account. i have updated the post accordingly – Sike12 Sep 24 '13 at 17:45
  • 1
    This is a very very complecated issue as google added some security layers to prevent the hackers. Login into your google account, it is likely that you see an alert! if so click on it and follow the instructions! – S Nash Sep 24 '13 at 18:31
  • 1
    Do you consider another assembly to do the same job? After failing with many solutions of usingSystem.Net.Mail, I used another assembly and that fixed the issue for me. If you want it please Edit your question and specfity other assemblies are acceptable. – S Nash Sep 24 '13 at 18:39
  • Hi S Nash. I logged into my googlemail account and i see no alerts. Yes i would be interested in using other assemblies. i'll modify the question accordingly – Sike12 Sep 24 '13 at 18:51

2 Answers2

10

I don't think there's anything wrong with your code other than the e-mail addresses. I used this code to successfully send an e-mail from gmail to my personal account (ran it in LINQPad, actually). Simply replace the 3 string values with valid values for your accounts and you should be good to go:

MailMessage message = new System.Net.Mail.MailMessage(); 
string fromEmail = "myaddr@gmail.com";
string fromPW = "mypw";
string toEmail = "recipient@receiver.com";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
    smtpClient.EnableSsl = true;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);

    smtpClient.Send(message.From.ToString(), message.To.ToString(), 
                    message.Subject, message.Body);   
}
Pete
  • 6,585
  • 5
  • 43
  • 69
  • Hi Pete. I think it could be some firewall issues or something to do with my googlemail username and password. – Sike12 Sep 24 '13 at 18:53
  • Are you sure you're using the right account & password? I would think you wouldn't get a response back if it were blocked by a firewall. – Pete Sep 24 '13 at 19:01
  • Try using Port 465 instead. – Pete Sep 24 '13 at 19:08
  • And also to verify, did you copy and paste my code and make changes (as opposed to modifying existing code)? It's important that you set `UseCredentials = false` prior to setting the Credentials. Otherwise it'll wipe out your credentials. In fact, you might verify the credentials in the debugger just prior ot the `Send()`. – Pete Sep 24 '13 at 19:11
  • yes Pete. I checked the account username and password by login into googlemail and i can login fine. Changing the port to 465 gives me the timeout message. Yes i am using your code and testing it – Sike12 Sep 24 '13 at 19:11
  • Also see this: http://rochcass.wordpress.com/2013/05/25/error-the-smtp-server-requires-a-secure-connection-or-the-client-was-not-authenticated-the-server-response-was-5-5-1-authentication-required/ – Pete Sep 24 '13 at 19:15
  • The "Operation Timed out" message at least gives us a clue that we are getting nearer to the solution :) – Sike12 Sep 24 '13 at 19:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37970/discussion-between-sike12-and-pete) – Sike12 Sep 24 '13 at 19:21
  • 1
    The `SmtpClient` should be wrapped in a using directive so that it gets disposed in a timely fashion. – Edward Brey Sep 28 '15 at 19:17
0

By this post.

MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
Community
  • 1
  • 1
Johnaudi
  • 257
  • 1
  • 23
  • 4
    This will report an error message saying "{"The remote name could not be resolved: 'smtp.google.com'"} – Sike12 Sep 24 '13 at 17:55
  • if you change it to smtp.gmail.com (with googlemail credentials) or smtp.live.com (with hotmail credentials) then this would work fine :) – Sike12 Sep 25 '13 at 21:26