Solved

Sending MailMessage Using ASP.Net 4.0

Posted on 2012-03-15
5
556 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

Independent Software Vendors: 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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