Solved

Sending MailMessage Using ASP.Net 4.0

Posted on 2012-03-15
5
558 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 75

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

626 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