422,024 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,024 IT Pros & Developers. It's quick & easy.

Email error

P: 39
Hello

I have the following code in a contact form field in my aspx file:

Expand|Select|Wrap|Line Numbers
  1. <input type="email" name="user_email" class="form-control" placeholder="Email Address" required="required">
In my aspx.vb file, I have:

Expand|Select|Wrap|Line Numbers
  1. Dim Email As String = "useremail"
  2.  
  3. 'Request.Form - extract data from form fields
  4.  
  5. Dim useremail As String = Request.Form("user_email")
  6.  
  7. 'create the mail message
  8.  
  9. myMessage.From = New MailAddress("user_email") 'User email completed by user
When I look at the contact form through a browser, I see this:



My understanding with that error is that it is looking for a valid email address and it is not possible to do that because the Webmaster, who is creating this SMTP form, cannot possibly know the user's email address before he types it in.

user_email is not the variable, it is the name of the email field. Should it be the variable useremail? When I try that with the quotation marks it gives that same 'not in the form required for an email address error', and when I try it without the quotation marks, I get: Value cannot be null. Parameter name: address

Thanks for any advice.
Attached Images
File Type: jpg source_error.jpg (40.1 KB, 208 views)
Nov 29 '17 #1

✓ answered by Frinavale

So, when you are working with a Web Forms application it is probably best to stick with the way that it was intended to be used.

Microsoft has made tools available to you, such as the asp.net TextBox control that makes it easier than you think to interact with data in your server code.

The TextBox control is a .net object that exposes properties like the "Text" property so that you can retrieve this information without having to go through the Request object like you would have to do in classic asp or php type environments.

For example, say you have your ASP.NET web form as such:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title></title>
  5.     <style type="text/css">
  6.         .prompt
  7.         {
  8.             display: block;
  9.             font-weight: bold;
  10.         }
  11.         .messageTextArea, .textInput
  12.         {
  13.             width: 20em;
  14.         }
  15.         .messageTextArea
  16.         {
  17.             height: 20em;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.     <form id="form1" runat="server">
  23.     <div>
  24.         <div>
  25.             <span class="prompt">Please provide your email:</span>
  26.             <asp:TextBox ID="user_email" runat="server" CssClass="textInput" /></div>
  27.         <div>
  28.             <span class="prompt">Please provide your subject:</span>
  29.             <asp:TextBox ID="user_subject" runat="server" CssClass="textInput" />
  30.         </div>
  31.         <div>
  32.             <span class="prompt">Please provide your message:</span>
  33.             <asp:TextBox ID="user_message" runat="server" Wrap="true" TextMode="MultiLine" CssClass="messageTextArea" />
  34.         </div>
  35.         <asp:Button ID="SendEmail" runat="server" Text="Send Email" />
  36.     </div>
  37.     </form>
  38. </body>
  39. </html>
When ASP.NET sends this to the browser, it is converted into HTML and it looks like this:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head><title>
  4.  
  5. </title>
  6.     <style type="text/css">
  7.         .prompt
  8.         {
  9.             display: block;
  10.             font-weight: bold;
  11.         }
  12.         .messageTextArea, .textInput
  13.         {
  14.             width: 20em;
  15.         }
  16.         .messageTextArea
  17.         {
  18.             height: 20em;
  19.         }
  20.     </style>
  21. </head>
  22. <body>
  23.     <form method="post" action="./contact.aspx" id="form1">
  24. <div class="aspNetHidden">
  25. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIODY2MjY4OThkZJqkRtnq0NOON5F088ku42vFoM2qk5VPCGYvsOj1lnC3" />
  26. </div>
  27.  
  28. <div class="aspNetHidden">
  29.  
  30.     <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CD2448B2" />
  31.     <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAWoASbgPc0NVNHyADJ8kYTN8WW+kaztJcywh7J3De2DyObVknTnR8DQ8XKfcR0YLpUfjPWxNAflCdG320Un8qULhm2+bBR3PLoxYiWNvjlmDhwLAUvc6M1f26OT+paV9sDCllv7rOjBDebf7FFjtYC1" />
  32. </div>
  33.     <div>
  34.         <div>
  35.             <span class="prompt">Please provide your email:</span>
  36.             <input name="user_email" type="text" id="user_email" class="textInput" /></div>
  37.         <div>
  38.             <span class="prompt">Please provide your subject:</span>
  39.             <input name="user_subject" type="text" id="user_subject" class="textInput" />
  40.         </div>
  41.         <div>
  42.             <span class="prompt">Please provide your message:</span>
  43.             <textarea name="user_message" rows="2" cols="20" id="user_message" class="messageTextArea">
  44. </textarea>
  45.         </div>
  46.         <input type="submit" name="SendEmail" value="Send Email" id="SendEmail" />
  47.     </div>
  48.     </form>
  49. </body>
  50. </html>
  51.  
ASP.NET automatically converts its objects into HTML for you when it sends the data down to the browser. Likewise, it automatically creates the .NET objects (the button and TextBox objects) and populates them with what the user supplies in the browser when the user submits the form.

So, this would be the VB.NET server code that would retrieve the information from the user based on what the user supplied in the browser:

Expand|Select|Wrap|Line Numbers
  1. Public Class contact
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6.     End Sub
  7.  
  8.  
  9.     Private Sub SendEmail_Click(sender As Object, e As System.EventArgs) Handles SendEmail.Click
  10.         Dim suppliedEmailText = user_email.Text
  11.         Dim suppliedSubjectText = user_subject.Text
  12.         Dim suppliedMessageText = user_message.Text
  13.  
  14.  
  15.     End Sub
  16. End Class
Notice that the SendEmail button object has a Click event? That event is created by ASP.NET when the user submits the form by clicking the button. When the button is created, the TextBoxes are also created. After they are initialized, they are populated with data that the user provided in the form in the browser.

After the event is created and the text boxes are populated, the server-side code for the page that handles the Click event is executed to do stuff...like retrieve the information the user provided (validate the information the user provided) and send the email (or not if the data isn't valid).

You should familiarize yourself with the ASP.NET Page Life Cycle so that you can understand how the process of sending and retrieving data works in Web Forms projects (along with understanding the events that occur in this process too)

For more information on how to send an email using .NET check out this Bytes article: Quick Reference on how to send an email using .NET

Let me know if you have any questions or problems.

ASP.NET is a pretty big topic!

Share this Question
Share on Google+
10 Replies


Frinavale
Expert Mod 5K+
P: 9,712
Try adding an ID to your field so that you can get it properly on the server.
Expand|Select|Wrap|Line Numbers
  1. <input id="user_email"  type="email" name="user_email" class="form-control" placeholder="Email Address" required="required">
Nov 29 '17 #2

P: 39
Hello Frinavale

Many thanks for your reply.

With that line of code you suggest in the aspx file and with this in the aspx.vb file

Expand|Select|Wrap|Line Numbers
  1. Line 26: 'create the mail message
  2. Line 27: 
  3. Line 28: myMessage.From = New MailAddress("user_email") 'User email completed by user I get the following error:
  4.  
Exception Details: System.FormatException: The specified string is not in the form required for an e-mail address.

However, with Line 28 like this:

Expand|Select|Wrap|Line Numbers
  1. Line 28: myMessage.From = New MailAddress(useremail) 'User email completed by user I get the following error:
I get the 'Value cannot be null. Parameter name: address' error.

Thanks again
Nov 29 '17 #3

Frinavale
Expert Mod 5K+
P: 9,712
Hi Bluenose,

What type of web application did you create?
MVC or Web Forms?
Nov 30 '17 #4

P: 39
Hello Frinavale

Thanks again for your reply.

It is a Web Forms site which, so far, only consists of 2-files (contact.aspx and contact.aspx.vb).
Nov 30 '17 #5

P: 39
Hello Frinavale

Am I right in saying that when I have the following (stripped of HTML/CSS) code in my contact.asp code for the form fields:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="user_name" runat="server">
  2.  <input id="user_email" type="email" name="user_email">
  3.  <input type="text" name="user_subject">
  4.  <textarea name="message" id="user_message"></textarea>
that the following in my contact.aspx.vb is also correct:

Expand|Select|Wrap|Line Numbers
  1. Dim Name As String = "username" 'where username is the unknown variable of the user's name
  2.  
  3. Dim Email As String = "useremail" 'where useremail is the unknown variable of the user's email
  4.  
  5. Dim Subject As String = "usersubject" 'where usersubject is the unknown subject of the user
  6.  
  7. Dim Message As String = "usermessage" 'where usermessage is the unknown message the user will write
  8.  
  9. 'Request.Form - extract data from form fields
  10.  
  11. Dim username As String = Request.Form("user_name") 'where "user_name" is the known ID of the name field
  12.  
  13. Dim useremail As String = Request.Form("user_email") 'where "user_email" is the known ID of the email field
  14.  
  15. Dim usersubject As String = Request.Form("user_subject") 'where "user_subect" is the known ID of the subject field
  16.  
  17. Dim usermessage As String = Request.Form("user_message") 'where "user_message" is the known ID of the message field
  18.  
  19. 'create the mail message
  20.  
  21. myMessage.From = New MailAddress(useremail) 'User email completed by user
  22.  
  23. myMessage.To.Add(New MailAddress("info@mysite.net")) 'Address of the Webmaster who will receive the user's email message
  24.  
  25. myMessage.CC.Add(New MailAddress("info2@mysite.com")) 'Address of the Webmaster's assistant who will receive a copy of the user's message to the Webmaster
  26.  
  27. myMessage.Body = usermessage 'user's message to the Webmaster 
I get no green or red underlines in Visual Studio 2017 with the above.

Thank you again for your time.
Nov 30 '17 #6

Frinavale
Expert Mod 5K+
P: 9,712
So, when you are working with a Web Forms application it is probably best to stick with the way that it was intended to be used.

Microsoft has made tools available to you, such as the asp.net TextBox control that makes it easier than you think to interact with data in your server code.

The TextBox control is a .net object that exposes properties like the "Text" property so that you can retrieve this information without having to go through the Request object like you would have to do in classic asp or php type environments.

For example, say you have your ASP.NET web form as such:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title></title>
  5.     <style type="text/css">
  6.         .prompt
  7.         {
  8.             display: block;
  9.             font-weight: bold;
  10.         }
  11.         .messageTextArea, .textInput
  12.         {
  13.             width: 20em;
  14.         }
  15.         .messageTextArea
  16.         {
  17.             height: 20em;
  18.         }
  19.     </style>
  20. </head>
  21. <body>
  22.     <form id="form1" runat="server">
  23.     <div>
  24.         <div>
  25.             <span class="prompt">Please provide your email:</span>
  26.             <asp:TextBox ID="user_email" runat="server" CssClass="textInput" /></div>
  27.         <div>
  28.             <span class="prompt">Please provide your subject:</span>
  29.             <asp:TextBox ID="user_subject" runat="server" CssClass="textInput" />
  30.         </div>
  31.         <div>
  32.             <span class="prompt">Please provide your message:</span>
  33.             <asp:TextBox ID="user_message" runat="server" Wrap="true" TextMode="MultiLine" CssClass="messageTextArea" />
  34.         </div>
  35.         <asp:Button ID="SendEmail" runat="server" Text="Send Email" />
  36.     </div>
  37.     </form>
  38. </body>
  39. </html>
When ASP.NET sends this to the browser, it is converted into HTML and it looks like this:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head><title>
  4.  
  5. </title>
  6.     <style type="text/css">
  7.         .prompt
  8.         {
  9.             display: block;
  10.             font-weight: bold;
  11.         }
  12.         .messageTextArea, .textInput
  13.         {
  14.             width: 20em;
  15.         }
  16.         .messageTextArea
  17.         {
  18.             height: 20em;
  19.         }
  20.     </style>
  21. </head>
  22. <body>
  23.     <form method="post" action="./contact.aspx" id="form1">
  24. <div class="aspNetHidden">
  25. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIODY2MjY4OThkZJqkRtnq0NOON5F088ku42vFoM2qk5VPCGYvsOj1lnC3" />
  26. </div>
  27.  
  28. <div class="aspNetHidden">
  29.  
  30.     <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CD2448B2" />
  31.     <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAWoASbgPc0NVNHyADJ8kYTN8WW+kaztJcywh7J3De2DyObVknTnR8DQ8XKfcR0YLpUfjPWxNAflCdG320Un8qULhm2+bBR3PLoxYiWNvjlmDhwLAUvc6M1f26OT+paV9sDCllv7rOjBDebf7FFjtYC1" />
  32. </div>
  33.     <div>
  34.         <div>
  35.             <span class="prompt">Please provide your email:</span>
  36.             <input name="user_email" type="text" id="user_email" class="textInput" /></div>
  37.         <div>
  38.             <span class="prompt">Please provide your subject:</span>
  39.             <input name="user_subject" type="text" id="user_subject" class="textInput" />
  40.         </div>
  41.         <div>
  42.             <span class="prompt">Please provide your message:</span>
  43.             <textarea name="user_message" rows="2" cols="20" id="user_message" class="messageTextArea">
  44. </textarea>
  45.         </div>
  46.         <input type="submit" name="SendEmail" value="Send Email" id="SendEmail" />
  47.     </div>
  48.     </form>
  49. </body>
  50. </html>
  51.  
ASP.NET automatically converts its objects into HTML for you when it sends the data down to the browser. Likewise, it automatically creates the .NET objects (the button and TextBox objects) and populates them with what the user supplies in the browser when the user submits the form.

So, this would be the VB.NET server code that would retrieve the information from the user based on what the user supplied in the browser:

Expand|Select|Wrap|Line Numbers
  1. Public Class contact
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.  
  6.     End Sub
  7.  
  8.  
  9.     Private Sub SendEmail_Click(sender As Object, e As System.EventArgs) Handles SendEmail.Click
  10.         Dim suppliedEmailText = user_email.Text
  11.         Dim suppliedSubjectText = user_subject.Text
  12.         Dim suppliedMessageText = user_message.Text
  13.  
  14.  
  15.     End Sub
  16. End Class
Notice that the SendEmail button object has a Click event? That event is created by ASP.NET when the user submits the form by clicking the button. When the button is created, the TextBoxes are also created. After they are initialized, they are populated with data that the user provided in the form in the browser.

After the event is created and the text boxes are populated, the server-side code for the page that handles the Click event is executed to do stuff...like retrieve the information the user provided (validate the information the user provided) and send the email (or not if the data isn't valid).

You should familiarize yourself with the ASP.NET Page Life Cycle so that you can understand how the process of sending and retrieving data works in Web Forms projects (along with understanding the events that occur in this process too)

For more information on how to send an email using .NET check out this Bytes article: Quick Reference on how to send an email using .NET

Let me know if you have any questions or problems.

ASP.NET is a pretty big topic!
Dec 1 '17 #7

P: 39
Many thanks for your detailed reply.

I will try to digest your code and go through the link you have kindly sent.

I have to admit, I almost never go to the Toolbox and seldom use anything in VS itself. I have never used 'Design View'. I was never really sure if those controls could be stylised - I know the 'file upload' box cannot be because I tried changing it once over hours and I was unsuccessful.

I think what you are saying is bypass what I have done so far (which is fine), and use the tools in VS and attempt to make them prettier than the default.

I have been doing it the hard way!

Many thanks, again, Frinavale, and I will copy and paste your reply and try to come to terms with it sooner rather than later.
Dec 1 '17 #8

Frinavale
Expert Mod 5K+
P: 9,712
Since the tools provided in .NET are converted to HTML so that they can be displayed in a web browser, you can use CSS to style them just like any HTML element.

There are ways to style a file upload control; however it typically involves hiding the upload control on the page and hooking up the features it offers to other, style-able, controls like buttons and text inputs.
Dec 5 '17 #9

P: 39
Yes, thanks.

I have started to style the <asp:TextBox> in Visual Studio 2017 - I will post a link when I have completed the 'contact' page (the one with the SMTP in it that you offered me advice about).

I have a couple of errors to resolve concerning

Expand|Select|Wrap|Line Numbers
  1. LblDate.Text = ReturnDate()
  2.  
  3.         user_name.Focus()
which I am trying to sort out.

Thanks again, Frinavale.
Dec 5 '17 #10

Frinavale
Expert Mod 5K+
P: 9,712
Please remember that VB.NET code is run on the server which means using VB.NET code like user_name.Focus() won't accomplish anything.

You will have to use client-side technology (JavaScript) to set the focus on the field in the web browser.
Dec 5 '17 #11

Post your reply

Sign in to post your reply or Sign up for a free account.