Solved

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

Posted on 2010-08-31
5
377 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

914 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

16 Experts available now in Live!

Get 1:1 Help Now