尝试在 C# 中发送邮件时出现 SMTP 异常
我正在使用以下非常简单的代码,但我不断收到 System.Net.Mail.SmtpException
发送邮件失败的消息。这是我的代码:
static void main(string[] args)
{
MailAddress from = new MailAddress("[email protected]", "Mr. Test");
MailAddress to = new MailAddress("[email protected]", "mr. man");
MailMessage msg = new MailMessage(from, to);
msg.Subject = "email";
msg.Body = "This is email.";
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Send(msg);
}
我以前从未尝试过以编程方式发送电子邮件,所以我感谢您的帮助。
I am using the following very simple code and I keep getting System.Net.Mail.SmtpException
failure sending mail. This is my code:
static void main(string[] args)
{
MailAddress from = new MailAddress("[email protected]", "Mr. Test");
MailAddress to = new MailAddress("[email protected]", "mr. man");
MailMessage msg = new MailMessage(from, to);
msg.Subject = "email";
msg.Body = "This is email.";
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Send(msg);
}
I have never tried programmatically sending email before so I appreciate the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您缺少凭据并以 TLS(安全连接)发送:
更多详细信息请参见:
使用 C# 通过 Gmail SMTP 服务器发送电子邮件
You're missing credentials and sending as TLS (secured connection):
More Details here:
Sending email through Gmail SMTP server with C#
我看到的第一件事是给出的代码不正确,没有给出用于 smtp 服务器验证的用户名/密码。
另外,为了更好地了解导致 SmtpException 的确切原因,请在调试器中捕获异常并查看异常的详细信息。通过这样做,我已经得到了关于导致 SMTP 错误的原因的很好的解释。
您可以尝试按照以下说明通过 gmail 使用 SMTP 发送邮件。 http://blogs.msdn.com/b/mariya /archive/2006/06/15/633007.aspx
First thing I see incorrect with the code given is there is no username/password given for validation with the smtp server.
Also, to get a better idea of what exactly is causing the SmtpException, catch the exception in your debugger and look at the details of the exception. I've gotten good explanations of what is causing SMTP errors by doing this.
You can try following these directions to send mail using SMTP via gmail. http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx
Google 不希望您使用端口 25,他们希望您使用 587 (ssl) 或 467。他们还要求您在发送邮件时进行身份验证。
Google does not want you to use port 25, they want you to use 587 (ssl) or 467. They also require that you authenticate when sending mail.