使用 SmtpClient 和 MailMessage 时如何抑制电子邮件验证

发布于 2024-09-04 10:43:06 字数 287 浏览 5 评论 0原文

使用 SmtpClient 和 MailMessage (.net 3.5) 发送电子邮件时,“收件人”电子邮件地址在发送之前会得到验证。我有一大堆电子邮件地址,它们在 at 符号之前有一个点 (.),当您尝试使用 SmtpClient 发送消息时,会导致 FormatException。这实际上是一件好事,因为根据规范,at 符号之前的点是无效的。不幸的是,这些电子邮件存在于现实世界中,如果您使用您喜欢的电子邮件客户端发送它们,它们就会被送达。

我的问题是,可以禁止通过 SmtpClient/MailMessage 进行电子邮件验证吗?

When sending out emails using the SmtpClient and a MailMessage (.net 3.5) the "To" email address(es) get validated prior to sending. I've got a big stack of email addresses which have a dot (.) before the at-sign, causing a FormatException when you attempt to send the message using the SmtpClient. This is actually a good thing, because by specification a dot before the at-sign is invalid. Unfortunately, those emails exist in the real world and they get delivered, if you send them out using your preferred email client.

My question is, can email validation through the SmtpClient/MailMessage be suppressed?

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

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

发布评论

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

评论(2

呆头 2024-09-11 10:43:06

尝试发送电子邮件后可能会出现错误。

您能否提供一些 CallStack,以便我们可以查看抛出异常的确切位置


更新:
根据reflector的反汇编代码,这个问题只能通过升级到VS2010和.NET 4.0来解决。

较低版本的System.Net程序集没有办法解决这个问题

This can be error after tring to send the e-mail.

Can you provide some CallStack so we can view where exactly exception is thrown


Update:
According to the disassemble code from reflector, this problem can be sold only by upgrading to the VS2010 and .NET 4.0

Lower versions of System.Net assembly have no way to solve this problem

谷夏 2024-09-11 10:43:06

编辑:尝试了 VB2005 中的示例代码,我也遇到了异常。下面的代码在 VB2010 Express 中运行良好,但似乎是一个错误,现已修复。 MSDN 引用来自 4.0 文档,该片段不在早期版本的 MSDN 页面中。

我现在无法尝试,但是您的电子邮件地址的格式到底如何? MailAddress 类的文档指出它支持用户名中的连续点和尾随点。例如,user...name..@host.(引用自 此处)。

编辑:添加示例。

Try
    Dim smtpClient As New SmtpClient()
    smtpClient.Host = "mailserver"
    Dim fromAddress As New MailAddress("[email protected]")
    Dim toAddresses As New MailAddress("[email protected]")
    Using message As New MailMessage()
         message.From = fromAddress
         message.To.Add(toAddresses)

         message.IsBodyHtml = False
         message.Subject = "test"
         smtpClient.Send(message)
    End Using
    TextBox1.Text = "OK"
Catch ex As SmtpException
    TextBox1.Text = ex.ToString()
End Try

Edit: Tried the sample code in VB2005 and I also got the exception. The code below works fine in VB2010 Express though so seems to be a bug that's now been fixed. The MSDN quote is from the 4.0 documenation, that snippet is not in the earlier versions of the MSDN page.

I can't try it right now but exactly how are your email addresses formatted? The documentation for the MailAddress class states that it supports Consecutive and trailing dots in user names. For example, user...name..@host. (quote copied from here).

Edit: Added sample.

Try
    Dim smtpClient As New SmtpClient()
    smtpClient.Host = "mailserver"
    Dim fromAddress As New MailAddress("[email protected]")
    Dim toAddresses As New MailAddress("[email protected]")
    Using message As New MailMessage()
         message.From = fromAddress
         message.To.Add(toAddresses)

         message.IsBodyHtml = False
         message.Subject = "test"
         smtpClient.Send(message)
    End Using
    TextBox1.Text = "OK"
Catch ex As SmtpException
    TextBox1.Text = ex.ToString()
End Try
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文