Home All Groups Group Topic Archive Search About

Weird system.net.mail behaviour

Author
1 Feb 2006 12:54 AM
Bob
This code snippet works fine (vs 2005)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim message As New MailMessage()
        Try
            message.From = New MailAddress("myn***@mydomain.com")
            message.To.Add(New MailAddress("myn***@mydomain.com"))
            message.Subject = "Test"
            message.Body = "Test body"
            Dim Client As New SmtpClient()
            Client.Host = "mail.mydomain.com"
            Client.Port = 25
            Dim LoginCredentials As New
System.Net.NetworkCredential("myn***@mydomain.com", "mypassword")
            Client.UseDefaultCredentials = False
            Client.Credentials = LoginCredentials
            Client.Send(message)
        Catch ex As SmtpException
            MsgBox(ex.Message)
        End Try
    End Sub

Yet the following which is EXACTLY the same except for the fact that the
values are passed to it from a call to a sub does not work, yet I have
checked consistently that the values when stepping thru in the debugger are
exactly the same in the equivalent line of code. Here's code snippet 2
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        SendMail("mail.mydomain.com", "myn***@mydomain.com", "sgiimsinc",
"myn***@mydomain.com", "myn***@mydomain.com", "subject", "body")
    End Sub

    Sub SendMail(ByVal SMTPServerName As String, ByVal SMTPUsername As
String, ByVal SMTPPassword As String, _
        ByVal ThisSenderEmail As String, _
        ByVal RecipientEmail As String, _
        ByVal Subject As String, ByVal Body As String)

        Dim message As New MailMessage()

        Try
            message.From = New MailAddress(ThisSenderEmail)
            message.To.Add(New MailAddress(RecipientEmail))
            message.Subject = Subject
            message.Body = Body
            Dim Client As New SmtpClient()
            Client.Host = SMTPServerName
            Client.Port = 25
            Dim LoginCredentials As New
System.Net.NetworkCredential(SMTPUsername, SMTPPassword)
            Client.UseDefaultCredentials = False
            Client.Credentials = LoginCredentials
            Client.Send(message)
        Catch ex As SmtpException
            'Log the exception
            MsgBox(ex.Message)
        End Try
    End Sub
There are no exceptions thrown by code snippet two. It just does not send
the e-mail. The code is all on the same test form. its just called either by
button one which executes it directly with the values hardcoded in or by
button 2 which calls the sendmail mail sub and passes it EXACTLY the same
parameters as the hardcoded values in snippet 1. I quadruple checked the
values of the passed parameters single stepping thru the executing code,
they ARE the same.

Does anyone have any idea why snippet 2 does not work and hpow to workaround
this?

Thanks for your help
Bob

Author
1 Feb 2006 1:49 PM
Andrew Morton
Bob wrote:
> There are no exceptions thrown by code snippet two. It just does not
> send the e-mail.

In my (limited) experience, that seems to mean that the .To address was not
valid.

Just try changing each value, one at a time, from hard-coded to being a
variable and then see which one breaks it.

Also, does MailMessage.To (etc.) accept a string if you want to simplify it
by taking out the New MailAddress? Hmmm... does a MailAddress have a .Valid
property or some such in .NET 2.0?

Andrew
Author
1 Feb 2006 3:17 PM
Bob
Thanks andrew I still am stumped but I will try calling the procedure that
has hardcoded addresses in it and pass it each parameter in turn to see
which one breaks it. I'll share the results.

Show quoteHide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:%23$qvVlzJGHA.3932@TK2MSFTNGP15.phx.gbl...
> Bob wrote:
>> There are no exceptions thrown by code snippet two. It just does not
>> send the e-mail.
>
> In my (limited) experience, that seems to mean that the .To address was
> not valid.
>
> Just try changing each value, one at a time, from hard-coded to being a
> variable and then see which one breaks it.
>
> Also, does MailMessage.To (etc.) accept a string if you want to simplify
> it by taking out the New MailAddress? Hmmm... does a MailAddress have a
> .Valid property or some such in .NET 2.0?
>
> Andrew
>
Author
1 Feb 2006 3:46 PM
Bob
It gets weirder.
I found that I could get the sending to work ONLY if my default email client
(outlook 2003) on this computer was closed!
If I do not close my outlook 2003, use my code to send using either the
hardcoded parameters sub or the sub to which I pass parameters, nothing gets
sent to the smtp server. If I close Outlook 2003, send my messages using my
code, it works fine. I can then open Outlook 2003 and retrieve my test
message from the server. It looks like you can only have one active e-mail
client at a time on a given computer.

Does anyone know if this is so?

Bob


Show quoteHide quote
"Andrew Morton" <a**@in-press.co.uk.invalid> wrote in message
news:%23$qvVlzJGHA.3932@TK2MSFTNGP15.phx.gbl...
> Bob wrote:
>> There are no exceptions thrown by code snippet two. It just does not
>> send the e-mail.
>
> In my (limited) experience, that seems to mean that the .To address was
> not valid.
>
> Just try changing each value, one at a time, from hard-coded to being a
> variable and then see which one breaks it.
>
> Also, does MailMessage.To (etc.) accept a string if you want to simplify
> it by taking out the New MailAddress? Hmmm... does a MailAddress have a
> .Valid property or some such in .NET 2.0?
>
> Andrew
>
Author
1 Feb 2006 10:28 PM
Phil Morris
Not immediately related to the problem you're seeing, but in trying to find
out why I was having problems with authetication, I found that if
UseDefaultCredentials was not set to false BEFORE assigning credentials to
the SmtpClient object, the values of credentials in the object were null
immediately before sending. 

Show quoteHide quote
"Bob" wrote:

> This code snippet works fine (vs 2005)
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>         Dim message As New MailMessage()
>         Try
>             message.From = New MailAddress("myn***@mydomain.com")
>             message.To.Add(New MailAddress("myn***@mydomain.com"))
>             message.Subject = "Test"
>             message.Body = "Test body"
>             Dim Client As New SmtpClient()
>             Client.Host = "mail.mydomain.com"
>             Client.Port = 25
>             Dim LoginCredentials As New
> System.Net.NetworkCredential("myn***@mydomain.com", "mypassword")
>             Client.UseDefaultCredentials = False
>             Client.Credentials = LoginCredentials
>             Client.Send(message)
>         Catch ex As SmtpException
>             MsgBox(ex.Message)
>         End Try
>     End Sub
>
> Yet the following which is EXACTLY the same except for the fact that the
> values are passed to it from a call to a sub does not work, yet I have
> checked consistently that the values when stepping thru in the debugger are
> exactly the same in the equivalent line of code. Here's code snippet 2
> Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button2.Click
>         SendMail("mail.mydomain.com", "myn***@mydomain.com", "sgiimsinc",
> "myn***@mydomain.com", "myn***@mydomain.com", "subject", "body")
>     End Sub
>
>     Sub SendMail(ByVal SMTPServerName As String, ByVal SMTPUsername As
> String, ByVal SMTPPassword As String, _
>         ByVal ThisSenderEmail As String, _
>         ByVal RecipientEmail As String, _
>         ByVal Subject As String, ByVal Body As String)
>
>         Dim message As New MailMessage()
>
>         Try
>             message.From = New MailAddress(ThisSenderEmail)
>             message.To.Add(New MailAddress(RecipientEmail))
>             message.Subject = Subject
>             message.Body = Body
>             Dim Client As New SmtpClient()
>             Client.Host = SMTPServerName
>             Client.Port = 25
>             Dim LoginCredentials As New
> System.Net.NetworkCredential(SMTPUsername, SMTPPassword)
>             Client.UseDefaultCredentials = False
>             Client.Credentials = LoginCredentials
>             Client.Send(message)
>         Catch ex As SmtpException
>             'Log the exception
>             MsgBox(ex.Message)
>         End Try
>     End Sub
> There are no exceptions thrown by code snippet two. It just does not send
> the e-mail. The code is all on the same test form. its just called either by
> button one which executes it directly with the values hardcoded in or by
> button 2 which calls the sendmail mail sub and passes it EXACTLY the same
> parameters as the hardcoded values in snippet 1. I quadruple checked the
> values of the passed parameters single stepping thru the executing code,
> they ARE the same.
>
> Does anyone have any idea why snippet 2 does not work and hpow to workaround
> this?
>
> Thanks for your help
> Bob
>
>
>
>