Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sending MailMessage Using ASP.Net 4.0

Posted on 2012-03-15
5
Medium Priority
?
560 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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