Skip to main content
 首页 » 编程设计

c#之从CSharp/.net使用gmail SMTP发送邮件

2024年01月22日33虾米哥

using(SmtpClient client = new SmtpClient("smtp.gmail.com", 587))  
{ 
    // Configure the client 
    client.EnableSsl = true; 
    client.UseDefaultCredentials = false; 
    client.Credentials = new NetworkCredential(textBox1.Text, textBox3.Text); 
 
    client.DeliveryMethod = SmtpDeliveryMethod.Network; 
    MailMessage message = new MailMessage( 
    textBox1.Text, // From field 
    textBox2.Text, // Recipient field 
    textBox4.Text, // Subject of the email message 
    richTextBox1.Text // Email message body 
    ); 
 
    client.Send(message); 
 
    MessageBox.Show("Email has been sent."); 
} 


Error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

我在使用gmail时遇到此错误,但是能够使用其他SMTP服务器发送邮件。凭据正确。

请您参考如下方法:

Gmail帐户似乎存在安全问题。

我也遇到过同样的问题,然后从this post找到了解决方案。

该帖子提到您需要通过启用“访问不太安全的应用程序”来更改“帐户权限”设置。

实际上,登录到Gmail帐户后,您会收到通知。