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