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

x
?
Solved

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

Posted on 2010-08-31
5
Medium Priority
?
398 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 2000 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 31

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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