Solved

Cannot send email using system.net.mail in vb.net

Posted on 2010-08-31
5
395 Views
Last Modified: 2012-05-10
I am trying to send an email using the net.mail class in vb.net but am getting an error "The specified string is not in the form required for an e-mail address.". I have placed each variable in the watch folder and nothing I can find is out of the ordinary. In fact, I turned all variables off and still get the error. I have attached the code and the stack error information. Can someone help?

--Stack Error Message
System.FormatException was unhandled by user code
  Message="The specified string is not in the form required for an e-mail address."
  Source="System"
  StackTrace:
       at System.Net.Mime.MailBnfHelper.ReadMailAddress(String data, Int32& offset, String& displayName)
       at System.Net.Mail.MailAddress.ParseValue(String address)
       at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
       at System.Net.Mail.MailAddress..ctor(String address)
       at System.Net.Mail.MailMessage..ctor()
       at RemitWeb.RemitWeb.Email_Services.Email.Email_Response_Send(String EmailBC, String EmailBody, String EmailCC, String EmailFrom, String EmailSubject, String EmailTo) in C:\Documents and Settings\rwas\My Documents\Visual Studio 2008\Projects\RemitWebNew\RemitWeb\code\EmailServices.vb:line 170
       at RemitWeb.RemitWeb.Email_Services.Email.Response(String FirstName, String LastName, String EmailBC, String EmailID, String EmailCC, String EmailFrom, String EmailSubject, String EmailTo, String EmailTitle, String Misc1, String Misc2, String Misc3) in C:\Documents and Settings\rwas\My Documents\Visual Studio 2008\Projects\RemitWebNew\RemitWeb\code\EmailServices.vb:line 28
       at RemitWeb.exec_UploadSummaryDaily.send_daily_file_upload_summary() in C:\Documents and Settings\rwas\My Documents\Visual Studio 2008\Projects\RemitWebNew\RemitWeb\utilities\exec_UploadSummaryDaily.aspx.vb:line 18
       at RemitWeb.exec_UploadSummaryDaily.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\rwas\My Documents\Visual Studio 2008\Projects\RemitWebNew\RemitWeb\utilities\exec_UploadSummaryDaily.aspx.vb:line 6
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

--Code to send e-mail
        Private Shared Sub Email_Response_Send(ByVal EmailBC As String, ByVal EmailBody As String, ByVal EmailCC As String, ByVal EmailFrom As String, ByVal EmailSubject As String, ByVal EmailTo As String)

            Dim mMailMessage As New MailMessage()
            Dim objPicture As LinkedResource
            Dim objPicture2 As LinkedResource
            Dim objAVHTML As AlternateView

            'Action to send entire e-mail
            objAVHTML = AlternateView.CreateAlternateViewFromString(EmailBody, Nothing, "Text/html")
            mMailMessage.AlternateViews.Add(objAVHTML)

            'Header Image
            objPicture = New LinkedResource("E:\Web_Resources\images\test_email_header.jpg")
            objPicture.ContentId = "emailheader01"
            objAVHTML.LinkedResources.Add(objPicture)

            'Footer Image
            objPicture2 = New LinkedResource("E:\Web_Resources\images\test_email_logo.jpg")
            objPicture2.ContentId = "emailfooter01"
            objAVHTML.LinkedResources.Add(objPicture2)

            mMailMessage.From = New MailAddress(EmailFrom)
            mMailMessage.To.Add(New MailAddress(EmailTo))

            'Add BCC if populated
            If EmailBC <> "" Then
                mMailMessage.Bcc.Add(New MailAddress(EmailBC))
            End If

            'Add CC if populated
            If EmailCC <> "" Then
                mMailMessage.CC.Add(New MailAddress(EmailCC))
            End If

            'Add Subject line to email
            mMailMessage.Subject = EmailSubject

            'Add Body to email
            mMailMessage.Body = EmailBody

            'Set Format of email to html
            mMailMessage.IsBodyHtml = True

            'Set Priority of email to high
            mMailMessage.Priority = MailPriority.High

            'Initiates a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient("smtp.webmail.test.com", 2525)

            'Send the e-mail
            Try
                mSmtpClient.Send(mMailMessage)
            Catch ex As Exception

            End Try

        End Sub
0
Comment
Question by:OB1Canobie
[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
  • 3
5 Comments
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
ID: 33567689
Apparently it complaints the format of email address, double check all the email addresses in To, CC and BCC. If it has more than one email address in any one of them, they need to be separated by comma ",".
 
0
 

Author Comment

by:OB1Canobie
ID: 33567739
I'm really confused because I cannot find where the e-mail is an issue. Each e-mail is valid. Nothing really out of the ordinary.
0
 

Author Comment

by:OB1Canobie
ID: 33567767
Actually, the error stops on the line where I declare the mMailMessage As New MailMessage.  I didn't even add the e-mail to the variable yet. Does this make any sense?
0
 
LVL 30

Expert Comment

by:MlandaT
ID: 33567925
That does not make sense. Sounds more like you are runing code that hasnt compiled or are debugging with the incorrect version of the file.
0
 

Author Comment

by:OB1Canobie
ID: 33567965
Found the issue. It was in the Web Config which was assigning a default from ..incorrect format. Thanks for the help.
0

Featured Post

Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

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