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

标签 c# email smtp gmail

服务器:VDS

操作系统:Windows Server 2008 R2

应用:无

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

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

我似乎无法让它工作!使用某些端口时,我会遇到不同的错误!

端口 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 帐户执行此操作。

关于C# - 通过 Gmail 或其他方式发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35218989/

相关文章:

用于 64 位操作系统的 C#?

c# - 如何在 Visual Studio 中使用 XUnit 对在远程进程中运行的代码进行单元测试

php - 收到电子邮件时如何运行代码

linux - sendmail 452 4.5.3 收件人太多

python - 无法在python中登录gmail smtp服务器

c# - 我应该如何使用 Entity Framework 实体分部类?

c# - ASP.NET Web 应用程序(.NET 框架)中的 asp.net core SignalR 配置

html - 如何使整个 Mailchimp 按钮可点击?

jQuery : extracting the Personal name and email address from a "full" email address on a form

authentication - ASP : SMTP authentification fails with JMail