Avatar of craigbtg
craigbtg
 asked on

Sending MailMessage Using ASP.Net 4.0

Hello,
I have updated some code that sends an email message from an ASP.Net 2.0 application to an ASP.Net 4.0 application.  An error is now thrown when trying to send that simply says "failure sending mail" and the stack trace points to System.Net.Mail.SMTPClient.Send.  Does anyone know if MailMessage or SMTPClient has changed with 4.0?  What needs to change?

Here is my code:

sToAddress = "user@test.com;user2@test.com"
sCCAddress = "user@abc.com;user2@abc.com"
sBCCAddress = "user@xyz.com;user2@xyz.com"
sFromAddress = "someone@somewhere.com"
sSubject = "this is the subject"
sBody = "This is the Body"

                If sToAddress <> "" Then
                    sToAddress = sToAddress.Replace(";", ",").Trim()
                End If
                If sCCAddress <> "" Then
                    sCCAddress = sCCAddress.Replace(";", ",").Trim()
                End If
                If sBCCAddress <> "" Then
                    sBCCAddress = sBCCAddress.Replace(";", ",").Trim()
                End If

                'Send the email message
                    Dim MailMsg As New MailMessage
                    MailMsg.From = New MailAddress(sFromAddress)
                    If sToAddress.IndexOf(",") > 0 Then
                        arToAddress = sToAddress.Split(",")
                        For i = 0 To UBound(arToAddress)
                            MailMsg.To.Add(arToAddress(i).Trim())
                        Next
                    Else
                        MailMsg.To.Add(sToAddress)
                    End If
                    If sCCAddress <> "" Then
                        If sCCAddress.IndexOf(",") > 0 Then
                            arCCAddress = sCCAddress.Split(",")
                            For i = 0 To UBound(arCCAddress)
                                MailMsg.CC.Add(arCCAddress(i).Trim())
                            Next
                        Else
                            MailMsg.CC.Add(sCCAddress)
                        End If
                    End If
                    If sBCCAddress <> "" Then
                        If sBCCAddress.IndexOf(",") > 0 Then
                            arBCCAddress = sBCCAddress.Split(",")
                            For i = 0 To UBound(arBCCAddress)
                                MailMsg.Bcc.Add(arBCCAddress(i).Trim())
                            Next
                        Else
                            MailMsg.Bcc.Add(sBCCAddress)
                        End If
                    End If

                    MailMsg.Subject = sSubject
                    MailMsg.Body = sBody

                    Dim SmtpMail As New SmtpClient
                    Dim SMTPUserInfo As New System.Net.NetworkCredential("user@emailhost.com", "smptpassword")
                    SmtpMail.Host = "SmtpServer"
                    SmtpMail.Port = "25"
                    SmtpMail.UseDefaultCredentials = False
                    SmtpMail.Credentials = SMTPUserInfo

                    SmtpMail.Send(MailMsg)

Open in new window

ASP.NETVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
craigbtg

8/22/2022 - Mon
PaulHews

Your code probably won't need changing.  When the error occurs, you need to click detail and look at the inner exception.  

The exception should be a SmtpException that you can get a code from, and that should give you the information you need to address the issue.
craigbtg

ASKER
The inner exception detail says "Unable to connect to the remote server".  The smtp server and credentials are the exact same as the 2.0 app which is still working, albeit on a different server.  I guess my testing machine may not be allowed to connect, but I do not believe there are any restrictions like that in place.  Any other code possibilities that may need a change?
ASKER CERTIFIED SOLUTION
PaulHews

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kaufmed

Antivirus software often blocks traffic over port 25 due to Spam. Are you running any AV software on this machine?
Your help has saved me hundreds of hours of internet surfing.
fblack61
craigbtg

ASKER
Thank you!  I was testing on a different network that was blocking port 25.  Once I transferred my "test" code to the server running the live code everything started working.  Thanks for your help!