简体   繁体   中英

Error sending email via SMTP in C#, Does this still work in 2017?

Having researched the issue extensively especially applying recommendations from similar issues on stackoverflow, the below code still returns error ""System.Net.Mail.SmtpException: Syntax error, command unrecognized. The server response was: connection rejected at....""

        try
        {
            var mail = new MailMessage();
            mail.From = new MailAddress("demo77377@gmail.com");
            mail.To.Add(new MailAddress("xxxxxxx@gmail.com"));
            mail.Subject = "TEST";
            mail.Body = "This is a test mail from C# program";

            using (var smtp = new SmtpClient())
            {
                smtp.Credentials = new System.Net.NetworkCredential("demo77377@gmail.com", "AABBCCDDEE1!","gmail.com");
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.UseDefaultCredentials = false;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Timeout = 10000;                    
                //
                smtp.Send(mail);
                Console.WriteLine("Message sent successfully");
                Console.ReadLine();
            }

        }
        catch (Exception e)

I have done everything possible
On Client> I have alternated smtp properties (permutation) etc
On Server> I have made gmail account less secure, I have disabled captcha etc

I observed that similar issues on stackoverflow were mostly dated over 3years ago and thus, is it possible that gmail no longer supports this SMTP method via C#, likewise has it been deprecated in favor of gmail API

Also, please find provided in code, original password supplied for the gmail account, in order to confirm if this issue is general or isolated to this gmail account

Thanks

This code is verified to work fine:

var fromAddress = new MailAddress("YOUREMAIL@gmail.com", "YOUREMAIL@gmail.com");
var toAddress = new MailAddress("YOUREMAIL@gmail.com", "YOUREMAIL@gmail.com");
const string fromPassword = "YOURPASSWORD";
const string subject = "YOUREMAIL@gmail.com";
const string body = "YOUREMAIL@gmail.com";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

I'm not comfortable including your personal details even though you have so I have removed them. Obviously replace the email and password with the correct details.

从我酒店的无线网络切换到办公网络后,终于可以正常工作了。网络连接的类型在C#中通过SMTP发送电子邮件中起着一定作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM