views:

177

answers:

2

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?

A: 

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
ho1
That may be what the documentation states but I have just tested it and it doesn't allow `[email protected]`
Barry
@Barry: I now managed to try it and `SmtpClient` has no problem with an address in that format for me. However, the actual SMTP client refuses the address and so an exception is thrown, but that's nothing to do with the `SmtpClient` it just passing on the error. So are you sure that it's not the same for you?
ho1
Barry
@ho1 and @Barry: Same situation here. I use C# and get a FormatException.
Matthias
+2  A: 

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

VMAtm
I'm sorry. The exception is thrown when trying to construct a MailMessage object with an email address like for example "[email protected]".
Matthias
@Matthias: Could you please post the actual code you're using, as I said in my reply to Barry's comment above, I can construct an email with an address in that format. I'll amend my answer to include the sample code I use.
ho1
@ho1: My code is more or less identical to yours. But I use C#, like Barry and I get the format exception when constructing the MailAddress object using "[email protected]".
Matthias
@ho1: if you write MailAddress to = new MailAddress("[email protected]"); in VS 2005 or 2008 you should get exception.
Pavel Belousov
@Belousov Pavel: Yep, I discovered that too as I wrote in my amended answer.
ho1