Solved

Sending MailMessage Using ASP.Net 4.0

Posted on 2012-03-15
5
547 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:craigbtg
  • 2
  • 2
5 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 37725846
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.
0
 

Author Comment

by:craigbtg
ID: 37725958
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?
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 37726023
Honestly, I would guess that it's environmental.  System.Net.Mail hasn't changed any interface details between .NET 2.0 and 4.0  The exception points to a problem connecting to the server.  You might try to telnet into port 25 on the server to test.

XFOR: Telnet to Port 25 to Test SMTP Communication
http://support.microsoft.com/kb/153119
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 37726077
Antivirus software often blocks traffic over port 25 due to Spam. Are you running any AV software on this machine?
0
 

Author Closing Comment

by:craigbtg
ID: 37726261
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!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now