Saturday, March 31, 2012

SMTP "Failure to send mail"

I have done a fair bit of reading and scoured previous posts, but noone seems to have my specific problem.

I need to send an email to one or more users of my web page.

I think I have the syntax correct as it seems rather simple, so my issue may very well be specific to some setting I need to change in my environment.

My Development machine.

Windows XP Pro
I have IIS and SMTP Installed
I am using Visual Studio 2005
My Ethernet connection is FireWalled (Microsoft)
I have checked Web Server (HTTP) and Internet Mail Server (SMTP) as services running on my network that internet users can access. (Though I'm not sure I need to do this)

I have a Linksys Router
I have configured the router to open ports 80 and 25 to my workstation.
IP is internal = 192.168.1.100

Here is the code, It was designed to send messages to several users with differing messages and so on. After much frustration I have boiled it down to the simplest form. Which apparently should work.

try
Dim oMail as NEW System.Net.Mail.SMTPClient("localhost",25)
oMail.Send("from@dotnet.itags.org.someplace.com","to@dotnet.itags.org.somplace.com","test","Message")
catch ex as Exception
lblErr.Text = "Error: " & ex.Message
End Try

The email addresses being used have of course been "modified" to protect the innocent .. or guilty .. or whatever.

No matter how elaborate the method and code, I get the exact same result every time. It is one of those typical Microsoft Error Messages that provides so much insight to your problem.

lblErr.Text displays "Error: Failure sending mail", and "Error: " is my addition, so basically "Falure sending mail" is as useful as "Something went wrong". It tells me nothing.

Is there something about SMTP I am missing here? Is there some permission setting not being set? I have tried to add the users ASPNET and IWAM and IUSR to the security tab for the SMTP Virtual Server.

Does anyone have an idea why this doesn't work?Try this code, I haven't tested it as I don't have SMTP installed but I got this from modifying the help code:

Dim testMail As New System.Net.Mail.SmtpClient
Dim fromAddress As New System.Net.Mail.MailAddress("test@.test.com")
Dim toAddress As New System.Net.Mail.MailAddress("test@.test.com")
Dim message As New System.Net.Mail.MailMessage(fromAddress, toAddress)
testMail.Host = "localhost"
testMail.Port = 25
testMail.Send(message)
This works

Try

' Create an instance of the MailMessage class
Dim objMail As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient
Dim Mailmsg As New System.Net.Mail.MailMessage

' Set the properties - send the email to
Mailmsg.To.Clear()
Mailmsg.To.Add(New System.Net.Mail.MailAddress(strEmailAddress))

' From the user
Mailmsg.From = New System.Net.Mail.MailAddress(strFromUser)

' To CC this email to the user
'Mailmsg.CC.Add(New System.Net.Mail.MailAddress(strCCUser)

' Set the subject
Mailmsg.Subject = " - Subject"

' set the message body
Mailmsg.Body = strMessageText

Try
' Specify to use the default Smtp Server
objMail.Host = strSmtpServer1
' Now, to send the message, use the Send method of the SmtpMail class
objMail.Send(Mailmsg)
Catch ex As Exception
' Specify to use the next Smtp Server
objMail.Host = strSmtpServer2
' Now, to send the message, use the Send method of the SmtpMail class
objMail.Send(Mailmsg)
End Try

Catch ex As Exception
End Try

With

Dim strSmtpServer1 As String = "10.123.456.78"
Dim strSmtpServer2As String = "10.123.456.79"
being the IP addresses

0 comments:

Post a Comment