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

0

本文介绍了从 CSharp/.net 使用 gmail SMTP 发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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.");
}

错误:SMTP 服务器需要安全连接或客户端未通过身份验证.服务器响应为:5.5.1 Authentication Required.

我在使用 gmail 时遇到了这个错误,但我可以使用其他 SMTP 服务器来发送邮件.凭据正确.

I have been getting this error with gmail but am able to use other SMTP Servers to send mails. The credentials are correct.

推荐答案

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

Looks like there is a security issue with Gmail account.

我也遇到了同样的问题,然后从 这篇文章.

I have also faced that same issue and then found solution from this post.

帖子提到您需要在启用访问不太安全的应用程序"的情况下更改帐户权限设置.

The post mentioned that you need to change the Account Permission setting with "Access of Less secure App" Enabled.

事实上,当您登录到您的 gmail 帐户时,您会收到通知.

In fact you will get notification when you logged in to your gmail account.

这篇关于从 CSharp/.net 使用 gmail SMTP 发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14