7

I have the following program to send an email by using "smtp.gmail.com:587"

namespace TestMailServer
{
    class Program
    {
        static void Main(string[] args)
        {
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("myTest@gmail.com");
            mail.To.Add("myTest2@gmail.com");
            mail.Subject = "Test Mail";
            mail.Body = "This is for testing SMTP mail";

            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential("myTest@gmail.com", "myPassword");
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);
            Console.WriteLine("Send out");

        }
    }
}

myTest@gmail.com, myTest2@gmail.com are really existing and myTest@gmail.com's password is myPassword. Why I got the following error:

Unhandled Exception: System.Net.Mail.SmtpException: 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 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 TestMailServer.Program.Main(String[] args) in D:\visual studio 2010\Projects\TestMailServer\TestMailServer\Program.cs:line 26 Press any key to continue . . .

sajadre
  • 1,141
  • 2
  • 15
  • 30
spspli
  • 3,128
  • 11
  • 48
  • 75
  • I'd recommend taking a look at [this question](http://stackoverflow.com/questions/5023156/what-could-cause-a-message-sent-from-gmail-smtp-using-c-not-to-arrive-no-excep) – Brian Driscoll Apr 28 '11 at 13:07

4 Answers4

5

I'm not sure what is causing your problem. Here is some code I have been using to successfully send email through a gmail account:

const string from = "...";
var fromAddr = new MailAddress(from, "Bug Tracker");
var toAddr = new MailAddress("...@...", "...");
var client = new SmtpClient {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Timeout = 30 * 1000,
    Credentials = new NetworkCredential(fromAddr.Address, "...")
};
using (var msg = new MailMessage(fromAddr, toAddr)) {
    msg.Subject = "...";
    msg.Body = string.Format("username: {0}\nversion: {1}\n\n{2}", Environment.UserName, Assembly.GetExecutingAssembly().GetName().Version.ToString(3), cbtext);
    client.Send(msg);
}
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • Is this still working for you? This is the exact code that I had used in the past, but with Google's recent conversion to their "shared" setup for google apps mail, this appears to have stopped working, getting the error that the OP is getting. – Charles Boyung May 01 '11 at 21:02
  • @Charles Boyung: It worked as of April 19, 2011. The system sends me an email with a stack trace whenever there's an unhandled exception. So it's a rare event. I will test it again. – Ferruccio May 02 '11 at 11:13
  • I just tested it. It's still working. Perhaps the problem is in the gmail settings. – Ferruccio May 02 '11 at 11:27
  • Has your google apps domain been converted yet? Wondering if the conversion changes something. – Charles Boyung May 03 '11 at 15:51
  • I'm not sure what you mean by "google apps domain". I'm not using google apps, just a gmail account. – Ferruccio May 03 '11 at 15:58
  • Jesus.. the Timeout resolved my problem... holy crap, why? Suddenly it stopped and now it works cause of the timeout. – MiKE Mar 02 '15 at 12:07
2

I had the code that Ferruccio posted and this recently stopped working. I moved my settings into the .config file for my site and it started to work again:

<system.net>
    <mailSettings>
        <smtp from="fromEmail" deliveryMethod="Network">
            <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587"
                 userName="fromEmail" password="password"/>
        </smtp>
    </mailSettings>
</system.net>
Charles Boyung
  • 2,464
  • 1
  • 21
  • 31
1

As far as i remember by default UseDefaultCredentialsProperty is set to true. So that could cause the authentification error you've got. try to add these lines from the previous answer to your code

SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.UseDefaultCredentials = false;

I've experienced couple of weird exceptions if not set DeliveryMethod property explicitly.

danyloid
  • 1,677
  • 3
  • 21
  • 47
0

Been struggling with the same error. With smtp.gmail.com, google blocks any attempts to login in to the sender email unless it be authenticated. Login to your email account and act on the sent verification mail. This will grant access to your C# Application