C# 通过 SMTP 客户端发送电子邮件时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18988348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:48:29  来源:igfitidea点击:

Error in Sending Email via a SMTP Client

c#.netwebsmtpsmtpclient

提问by Sike12

This may be very trivial for you but i just couldn't figure out why am i getting this error message when i run my code. I looked some of the relative questions on this same website for eg Sending email through Gmail SMTP server with C#but none of them was helpful. Anyone willing to help please? using different assemblies are also acceptable. so if anyone got a working solution that would be appreciated.

这对您来说可能很微不足道,但我只是不明白为什么我在运行代码时会收到此错误消息。我在同一个网站上查看了一些相关问题,例如 使用 C# 通过 Gmail SMTP 服务器发送电子邮件,但没有一个有帮助。请问有人愿意帮忙吗?使用不同的组件也是可以接受的。所以如果有人得到一个可行的解决方案,我们将不胜感激。

Error Message = 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

错误消息 = SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证。了解更多

here is my code

这是我的代码

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();      
message.From = new MailAddress("[email protected]");
message.To.Add("[email protected]");
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("MyGoogleMailAccount", 
                                               "mygooglemailpassword");

smtpClient.Send(message.From.ToString(), message.To.ToString(), 
                message.Subject, message.Body);   

采纳答案by Pete

I don't think there's anything wrong with your code other than the e-mail addresses. I used this code to successfully send an e-mail from gmail to my personal account (ran it in LINQPad, actually). Simply replace the 3 string values with valid values for your accounts and you should be good to go:

除了电子邮件地址之外,我认为您的代码没有任何问题。我使用此代码成功地从 gmail 向我的个人帐户发送了一封电子邮件(实际上是在 LINQPad 中运行的)。只需将 3 个字符串值替换为您帐户的有效值,您就可以开始使用了:

MailMessage message = new System.Net.Mail.MailMessage(); 
string fromEmail = "[email protected]";
string fromPW = "mypw";
string toEmail = "[email protected]";
message.From = new MailAddress(fromEmail);
message.To.Add(toEmail);
message.Subject = "Hello";
message.Body = "Hello Bob ";
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

using(SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
    smtpClient.EnableSsl = true;
    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtpClient.UseDefaultCredentials = false;
    smtpClient.Credentials = new NetworkCredential(fromEmail, fromPW);

    smtpClient.Send(message.From.ToString(), message.To.ToString(), 
                    message.Subject, message.Body);   
}

回答by John

By this post.

通过这个帖子。

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);