Solved

The specified string is not in the form required for an e-mail address system.net.mail

Posted on 2014-04-02
17
3,163 Views
Last Modified: 2014-04-08
I am trying to email the results of a form to myself, for now. I keep getting "The specified string is not in the form required for an e-mail address"

The address is hard coded in and is correct:

     
  Dim myMessage As MailMessage = New MailMessage()
        myMessage.Subject = "New Benefit Time Request"
        myMessage.Body = mailBody

        myMessage.From = New MailAddress("kba_forms@keystoneblind.org")
        myMessage.IsBodyHtml = True
        myMessage.To.Add(New MailAddress("kevin@keystoneblind.org"))

Open in new window


When I view the mailbody it has all of the information that I want in it.

the odd thing is I copied and pasted this code from another project that I have done. It works fine.

Any help will be appreciated
0
Comment
Question by:ICantSee
[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
  • 9
  • 7
17 Comments
 

Author Comment

by:ICantSee
ID: 39973555
error
0
 

Author Comment

by:ICantSee
ID: 39973560
System.FormatException was unhandled by user code
  HResult=-2146233033
  Message=The specified string is not in the form required for an e-mail address.
  Source=System
  StackTrace:
       at System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index)
       at System.Net.Mail.MailAddressParser.ParseAddress(String data)
       at System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding)
       at System.Net.Mail.MailMessage..ctor()
       at BenefitTime_KevinBly_Default.SubmitFormButton_Click(Object sender, EventArgs e) in \\data\kba\UserFiles\kevin\My Documents\Projects\websites\2014\Service Forms\BenefitTime\kevinbly\Default.aspx.vb:line 76
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Open in new window

0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39973623
If you create a mailaddress with just one parameter it should be in the form

"Tom Smith <tsmith@hotmail.com>"

if the format is not recognised then a format exception will be thrown

Exception - Condition

ArgumentNullException    - address is null.
 
ArgumentException   - address is String.Empty ("").
 
FormatException - address is not in a recognized format.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 40
ID: 39973929
The code you show us is not the code that triggers the exception, so we do not see the whole thing.

Your third post shows that the problem in on line 76. A lot can happen between the code in your first post and line 76.

Looking at your second post, there is something about a DateTime (Date) object, which is strange for an error in a mail address parsing routine. Is it possible that your MailAddress is modified somewhere with something that has a date in it?
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
ID: 39974199
@James

The datetime in the troubleshooting tips is an example of what can cause a format exception, not what actually did cause the format exception. The second line in the troubleshooting tips is the key one in this instance. Also the code posted could be line 76 in the actual subroutine. The stack trace is the key in this case and shows the error in the constructor of the mailaddress class.
0
 

Author Comment

by:ICantSee
ID: 39974666
@james. I have no clue what you are trying to tell me. I have sifted through the items you mentioned before I posted this question and have not  been able to resolve my issue. Sorry.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39974749
Did you try changing the format of your email addresses as I posted?
0
 

Author Comment

by:ICantSee
ID: 39974764
my email address is hand coded in. kevin@keystoneblind.org

no misspelling, even retyped it a few times.

this same code was copied and pasted from another app that I have running right now. No issues with it.
0
 

Author Comment

by:ICantSee
ID: 39974772
I' sorry. I didn't realize you were the same person.

I don't understand what line 76 in the actual sub routine means?
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39974783
I'm not, there are two experts commenting on this.

It should be "kevin <kevin@keystoneblind.org>" rather than "kevin@keystoneblind.org"

The mail message doesn't understand the format of the string that you are providing
0
 

Author Comment

by:ICantSee
ID: 39974835
myMessage.To.Add(New MailAddress("kevin <kevin@keystoneblind.org>"))

Open in new window


Throws the same error.

I am using VB and system.net.mail

As I said, the copy and pasted the exact code from another app that I built, and it runs flawlessly. I really don't get it.
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39974920
What version of VB are you using and what version of the framework
0
 
LVL 15

Expert Comment

by:ChloesDad
ID: 39974942
And did you change the "from" address to be in that format as well as the "to"
0
 

Author Comment

by:ICantSee
ID: 39975038
Its the version of VB that comes with Microsoft Visual Studio 2012

.net version is 4.5

Same error when I change both email addresses to the suggested formaterror
0
 
LVL 15

Assisted Solution

by:ChloesDad
ChloesDad earned 500 total points
ID: 39975083
That makes it a lot clearer, the error is in the declaration of the mailmessage, not when adding the mailaddress, hence why changing the mailaddress didn't work.

MailMessage should work without any parameters

try this

Dim myMessage As MailMessage = New MailMessage("kba_forms@keystoneblind.org","kevin@keystoneblind.org")
        myMessage.Subject = "New Benefit Time Request"
        myMessage.Body = mailBody

'        myMessage.From = New MailAddress("kba_forms@keystoneblind.org")
        myMessage.IsBodyHtml = True
'        myMessage.To.Add(New MailAddress("kevin@keystoneblind.org"))
 

Open in new window

0
 

Assisted Solution

by:ICantSee
ICantSee earned 0 total points
ID: 39975224
The answer ended up being that the app was looking in the web.config file instead of vb file for the "From" email address. Once corrected, it functioned as expected.
0
 

Author Closing Comment

by:ICantSee
ID: 39985541
It was the final answer. No one really steered me to the web.config file.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
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.…

695 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