C# - 通过 Gmail 或其他方式发送电子邮件?

C# - Sending an email, via Gmail or Other?

服务器:VDS

OS: Windows 服务器 2008 R2

申请:None

库(应用程序使用的 DLL):是,C#

我正在尝试使用我阅读的 gmail 服务通过 C# 发送邮件。基本上,只要给自己发一封测试邮件,就可以开始了解它是否有效。如果你不得不问,信息存储在 config.json 文件中而不是直接在代码中,因此 "AccountRecovery.AccountRecoveryConfig".

我似乎无法让它工作!使用某些端口时出现不同的错误!

PORT 465 - 有凭据 错误:

2016-02-05 02:52:33 - Command: ERROR: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

端口 587 - 有凭证 错误:

2016-02-05 02:55:50 - Command: ERROR: 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

我不知道该怎么办。我做错了什么吗?

    public static void SendEmail(string email)
    {
        MailMessage mail = new MailMessage(AccountRecovery.AccountRecoveryConfig.ServerEmailAddress, email);
        SmtpClient client = new SmtpClient();
        client.Timeout = 30000;
        client.Host = AccountRecovery.AccountRecoveryConfig.HostSMTPServer;
        client.Port = AccountRecovery.AccountRecoveryConfig.HostPort;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential(AccountRecovery.AccountRecoveryConfig.ServerEmailAddress, AccountRecovery.AccountRecoveryConfig.ServerEmailPassword);
        client.EnableSsl = true;
        //client.ServicePoint.MaxIdleTime = 1;
        mail.Subject = AccountRecovery.AccountRecoveryConfig.EmailSubjectLine;
        mail.Body = AccountRecovery.AccountRecoveryConfig.EmailBodyLine;
        mail.IsBodyHtml = false;
        client.Send(mail);
    }

google正确的端口是587,这个错误:

2016-02-05 02:55:50 - Command: ERROR: 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

您应该授予对安全性较低的应用程序的访问权限。这是 LINK 您可以为当前登录的 google 帐户执行此操作的地方。