Using SMTP to send internal mail notifications

Franco_1

New member
Joined
Feb 18, 2009
Messages
3
Programming Experience
Beginner
Below is a click event in a button. When I run it, soon as it gets to
mClient.Send(msg) line, it pauses for a few seconds and throws
an exception "Failure to send mail".
What is wrong in my code?
All help, suggestions will be appreciated.:confused:

imports system.Net.Mail

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim msg As New MailMessage
Dim mClient As New SmtpClient
'Dim sent As Integer

mClient.Host = "mailservername.com"
mClient.UseDefaultCredentials = True

msg.From = New MailAddress("myemailAddress@company.com")

msg.To.Add(New MailAddress("toCoWorker@company.com"))

msg.Subject = "Testing email program"

msg.Body = "If you receive this, please smile and let me know that it went through."

Try

mClient.Send(msg)

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
 
My first guess would be lack of credentials. Have you tried it using the credentials of a user account?

Bernie
 
Yes I can see you are using the default credentials and in my experience when I have seen the error you are getting it's because of authentication problems with the SMTP server. So I repeat my suggestion that you explicityly use the authentication credentials of a know working account and see if that works.

I will add to this a suggestion that you manually test the credentials using telnet from the system that will be running the program. This is an easy way to verify the SMTP server likes the credetials you are giving. Here is a link in case you don't know how to do this;
How to Use Telnet to Test SMTP Communication

Bernie
 
SMTP Error

Bernie,
Thanks of all the feedback. I tried what you suggested and it also woks.
Found out my problem was not with the code but with access issues.

I was being denied access to the mail server by my local virus program.
So I tested the code by running it from a server (which does not have a virus program) and it run perfectly.

I appreciate all that help.
 
Back
Top