尝试在 C# 中发送邮件时出现 SMTP 异常

发布于 2024-12-03 13:51:25 字数 773 浏览 1 评论 0原文

我正在使用以下非常简单的代码,但我不断收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕花眠 2024-12-10 13:51:25

您缺少凭据并以 TLS(安全连接)发送:

Credentials = new NetworkCredential("[email protected]", "mypwd"), 
EnableSsl = true 

更多详细信息请参见:
使用 C# 通过 Gmail SMTP 服务器发送电子邮件

You're missing credentials and sending as TLS (secured connection):

Credentials = new NetworkCredential("[email protected]", "mypwd"), 
EnableSsl = true 

More Details here:
Sending email through Gmail SMTP server with C#

难理解 2024-12-10 13:51:25

我看到的第一件事是给出的代码不正确,没有给出用于 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

暖心男生 2024-12-10 13:51:25

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文