system.net.mail 中的 C# smtp 配置
我有一个发送通知电子邮件的小型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它适用于 Outlook 客户端,因为工作站(可能)已登录到 Active Directory 域,并且 Exchange 服务器因此“信任”该连接。
您需要在邮件服务上添加用户的凭据,并且必须从用户处获取它们:
另外,不要忘记设置 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:
Also, don't forget to set the Port property for SSL. Some providers don't use the standard ones.
如果您的应用程序与 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?
即使您提供给邮件服务器的凭据本身没问题,但“发件人”字段与该特定用户不匹配,您也可能会收到异常消息。 (这是一项功能,是 SMTP 服务器强制执行的一项限制,以防止欺骗发件人。SMTP 协议本身非常自由,理论上您可以在“发件人”字段中写入任何电子邮件地址。防止欺骗非常重要来对抗垃圾邮件。)
一般来说,如果您做错了什么,您将收到 SmtpException,它具有意外的描述性。有时,嵌入的内部异常可以区分更多情况。您只需要调试各种情况,我整理了一些:
另一件需要注意的事情是如何准确地创建 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:
Another thing to pay attention is how exactly you create your NetworkCredential object.
System.Net.Mail is very convenient!