Solved

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

Posted on 2010-08-31
5
374 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
  • 3
5 Comments
 
LVL 17

Accepted Solution

by:
Zhaolai earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Found the issue. It was in the Web Config which was assigning a default from ..incorrect format. Thanks for the help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 109
asp.net bundle 8 34
C# code editing and collaboration 3 39
Make a border less form movable 2 11
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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now