Weird system.net.mail behaviour

B

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("(e-mail address removed)")
message.To.Add(New MailAddress("(e-mail address removed)"))
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("(e-mail address removed)", "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", "(e-mail address removed)", "sgiimsinc",
"(e-mail address removed)", "(e-mail address removed)", "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
 
A

Andrew Morton

Bob said:
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
 
B

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.
 
B

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top