By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,664 Members | 1,592 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,664 IT Pros & Developers. It's quick & easy.

Weird system.net.mail behaviour

P: n/a
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("my****@mydomain.com")
message.To.Add(New MailAddress("my****@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("my****@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", "my****@mydomain.com", "sgiimsinc",
"my****@mydomain.com", "my****@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

Feb 1 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Feb 1 '06 #2

P: n/a
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.

"Andrew Morton" <ak*@in-press.co.uk.invalid> wrote in message
news:%2****************@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

Feb 1 '06 #3

P: n/a
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
"Andrew Morton" <ak*@in-press.co.uk.invalid> wrote in message
news:%2****************@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

Feb 1 '06 #4

P: n/a
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.

"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("my****@mydomain.com")
message.To.Add(New MailAddress("my****@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("my****@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", "my****@mydomain.com", "sgiimsinc",
"my****@mydomain.com", "my****@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

Feb 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.