如何在 .NET 中对 SMTP 进行身份验证?

发布于 2024-08-12 05:31:04 字数 1610 浏览 3 评论 0原文

我正在尝试在 Visual Studio 2005 中构建一个电子邮件应用程序,并使用 .NET 的 System::Net::Mail::Smtp 类来发送电子邮件,但我不知道如何验证 SMTP 与服务器(现在我一直在尝试使用 smtp.gmail.com)。

这是我的代码:

SmtpClient^ client = gcnew SmtpClient("smtp.gmail.com");
NetworkCredential^ basicCredential = gcnew NetworkCredential("[email protected]", "password");
client->UseDefaultCredentials = false;
client->Credentials = basicCredential;

令我烦恼的是它不会抛出任何 SmtpExceptions,因此它可以与服务器通信,只是没有进行身份验证。 我也尝试过设置:

client->EnableSsl = true;

但这也不起作用。

关于如何实现这项工作有什么想法吗?

编辑:我删除了这个问题的答案,因为它是错误的。关闭防火墙没有任何作用,看起来这段代码可以打开和关闭。有谁知道 Gmail 是否有某种垃圾邮件功能,可以转储任何看起来不合法的标题/正文?或者这个代码十次尝试一次有效的任何其他原因?

第二次编辑:这就是我的 MailMessage 的样子:

MailAddress^ from = gcnew MailAddress(fromAddr, fromName, System::Text::Encoding::UTF8);
MailAddress^ to = gcnew MailAddress((String^) toAddr[0]);
MailMessage^ message = gcnew MailMessage(from, to);
MailAddress^ cc = gcnew MailAddress(ccAddr);
message->CC->Add(cc);
MailAddress^ bcc = gcnew MailAddress(bccAddr);
message->Bcc->Add(bcc);
message->Body = body;
message->BodyEncoding = System::Text::Encoding::UTF8;
message->Subject = sub;
message->SubjectEncoding = System::Text::Encoding::UTF8;
message->Attachments->Add(attachment);

现在似乎工作更加一致,但我没有更改代码中的任何内容..它一定与实际的 SMTP 服务器有关?

I'm trying to build an email application in Visual Studio 2005 and I'm using .NET's System::Net::Mail::Smtp class to send emails, but I can't figure out how to authenticate SMTP with a server (right now I've been trying with smtp.gmail.com the most).

Here's my code:

SmtpClient^ client = gcnew SmtpClient("smtp.gmail.com");
NetworkCredential^ basicCredential = gcnew NetworkCredential("[email protected]", "password");
client->UseDefaultCredentials = false;
client->Credentials = basicCredential;

The thing that annoys me is that it doesn't throw any SmtpExceptions so it's communicating with the server alright, it's just not authenticating.
I've also tried setting:

client->EnableSsl = true;

but that also doesn't work.

Any ideas on how to make this work?

Edit: I've removed my answer to this question because it was wrong. Having turned off the Firewall does nothing, it seems like this code works on and off. Does anyone know if Gmail has some sort of spam feature that dumps anything that doesn't have a legitimate looking title/body? Or any other reason why this code works once out of ten tries??

Second edit: This is what my MailMessage looks like:

MailAddress^ from = gcnew MailAddress(fromAddr, fromName, System::Text::Encoding::UTF8);
MailAddress^ to = gcnew MailAddress((String^) toAddr[0]);
MailMessage^ message = gcnew MailMessage(from, to);
MailAddress^ cc = gcnew MailAddress(ccAddr);
message->CC->Add(cc);
MailAddress^ bcc = gcnew MailAddress(bccAddr);
message->Bcc->Add(bcc);
message->Body = body;
message->BodyEncoding = System::Text::Encoding::UTF8;
message->Subject = sub;
message->SubjectEncoding = System::Text::Encoding::UTF8;
message->Attachments->Add(attachment);

This seems to work more consistently now, but I haven't changed anything in my code.. It must be something to do with the actual SMTP server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浪推晚风 2024-08-19 05:31:05

当您尝试启用 ssl 时,是否还将端口更改为 587(GMail SSL SMTP 端口)?

When you tried enabling ssl did you also change the port to 587, the GMail SSL SMTP port?

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