system.net.mail 中的 C# smtp 配置

发布于 2024-12-27 22:03:10 字数 441 浏览 0 评论 0原文

我有一个发送通知电子邮件的小型 WinForms 应用程序。它在 Outlook 客户端上运行良好,但如果输入 gmail/yahoo/windowslive 地址,则会发生轰炸。我见过很多使用 system.web.mail 和模式的帖子,但是 Visual Studio 2010 在尝试使用它时给了我一个错误(我假设是因为 .Net 2.0)。是否可以配置我的 smtp 客户端代码以中继到所有这些电子邮件提供商?

我将 system.net.mail 与 MailMessage 一起使用。我的配置代码如下...

SmtpClient mailSender = new SmtpClient("smtp.myclient.com");
            mailSender.EnableSsl = true;
            mailSender.Send(message);

I have a small WinForms application that sends notification emails. It works fine with outlook clients but bombs if a gmail/yahoo/windowslive address is entered. I've seen many posts using system.web.mail and schemas but Visual Studio 2010 gives me an error when trying to use this (I'm assuming because of .Net 2.0). Is it possible to configure my smtp client code to relay to all of these email providers?

Im using system.net.mail with MailMessage. My configuration code is below...

SmtpClient mailSender = new SmtpClient("smtp.myclient.com");
            mailSender.EnableSsl = true;
            mailSender.Send(message);

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

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

发布评论

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

评论(3

银河中√捞星星 2025-01-03 22:03:10

它适用于 Outlook 客户端,因为工作站(可能)已登录到 Active Directory 域,并且 Exchange 服务器因此“信任”该连接。

您需要在邮件服务上添加用户的凭据,并且必须从用户处获取它们:

 mailsender.Credentials = new NetworkCredential(username, password);

另外,不要忘记设置 SSL 的 Port 属性。有些提供商不使用标准的。

It works fine for Outlook clients because the workstation is (likely) logged into the Active Directory Domain and the Exchange server "trusts" the connection because of it.

You need to add the user's credentials on the mail service, and you'll have to get them from the user:

 mailsender.Credentials = new NetworkCredential(username, password);

Also, don't forget to set the Port property for SSL. Some providers don't use the standard ones.

So尛奶瓶 2025-01-03 22:03:10

如果您的应用程序与 Outlook 客户端配合使用,则您的应用程序可以正确发送电子邮件。与网址的唯一区别是电子邮件的显示方式,因此问题可能在于编码。您能详细说明应用程序炸弹意味着什么吗?

If your application works with outlook clients then you application sends email correctly. The only difference with web address is the way the emails are shown so maybe the problem is with the encoding. Can you detail what means that the application bombs?

莫言歌 2025-01-03 22:03:10

即使您提供给邮件服务器的凭据本身没问题,但“发件人”字段与该特定用户不匹配,您也可能会收到异常消息。 (这是一项功能,是 SMTP 服务器强制执行的一项限制,以防止欺骗发件人。SMTP 协议本身非常自由,理论上您可以在“发件人”字段中写入任何电子邮件地址。防止欺骗非常重要来对抗垃圾邮件。)

一般来说,如果您做错了什么,您将收到 SmtpException,它具有意外的描述性。有时,嵌入的内部异常可以区分更多情况。您只需要调试各种情况,我整理了一些:

  1. 如果电子邮件的“发件人”地址与他/她有权获得的经过身份验证的用户的电子邮件地址不匹配,您将得到出现“邮箱不可用”的 SmtpException。和“客户端没有权限作为此发件人发送”。
  2. 如果指定了错误的主机或端口,SmtpException 消息将以“Mailbox unavailable.”开头,它将有一个 WebException 类型的内部异常,表示“无法连接到远程服务器”,该异常本身将有一个内部异常类型 SocketException,表示“无法建立连接,因为目标计算机主动拒绝”。
  3. 如果您打开 SSL(mailSender.EnableSsl = true; // 对于您的代码),但 SMTP 服务器不支持它,或者反之亦然,那么您将收到 SmtpException,显示“服务器不支持安全连接” ”。
  4. 如果凭据(用户名/密码)错误或其他一些 SSL 问题情况,您将收到 SmtpExeption,指出“SMTP 服务器需要安全连接或客户端未经身份验证”。

另一件需要注意的事情是如何准确地创建 NetworkCredential 对象。

System.Net.Mail非常方便!

You can get an exception message even if your credentials supplied to the mail servers would be OK by themselves, but the "From" field doesn't match that particular user. (This is a feature, a restriction enforced by the SMTP servers to prevent spoofing a sender. The SMTP protocol itself is so liberal, that in theory you can write any e-mail address into the "From" field. Preventing spoofing is very important to fight against e-mail spam.)

In general if you do something wrong, you'll receive an SmtpException, which is unexpectedly descriptive. Sometimes the embedded inner exception distinguish more cases. You just have to debug the various cases, I sorted out some:

  1. If the "from" address of the e-mail doesn't match the e-mail address of the authenticated user what he/she is entitled to, you'll get an SmtpException saying "Mailbox unavailable." and "Client does not have permissions to send as this sender".
  2. If you specify a wrong host or port, the SmtpException message will start with "Mailbox unavailable.", it'll have an inner exception of type WebException saying "Unable to connect to the remote server", which will have an inner exception itself of a type SocketException, saying "No connection could be made because the target machine actively refused it".
  3. If you turn on SSL (mailSender.EnableSsl = true; // for your code), but the SMTP server doesn't support it, or the other way around, then you'll get an SmtpException saying "Server does not support secure connections".
  4. If the credentials (username/password) are wrong or some other SSL problem cases you'll get an SmtpExeption saying "The SMTP server requires a secure connection or the client was not authenticated".

Another thing to pay attention is how exactly you create your NetworkCredential object.

System.Net.Mail is very convenient!

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