Email error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bluenose
    New Member
    • Apr 2012
    • 56

    Email error

    Hello

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

    Code:
    <input type="email" name="user_email" class="form-control" placeholder="Email Address" required="required">
    In my aspx.vb file, I have:

    Code:
    Dim Email As String = "useremail"
    
    'Request.Form - extract data from form fields
    
    Dim useremail As String = Request.Form("user_email")
            
    'create the mail message
    
    myMessage.From = New MailAddress("user_email") 'User email completed by user
    When I look at the contact form through a browser, I see this:
    [imgnothumb]https://bytes.com/attachment.php? attachmentid=92 99d1511976180[/imgnothumb]

    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 Files
    Last edited by Frinavale; Nov 29 '17, 06:42 PM.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Try adding an ID to your field so that you can get it properly on the server.
    Code:
    <input id="user_email"  type="email" name="user_email" class="form-control" placeholder="Email Address" required="required">

    Comment

    • Bluenose
      New Member
      • Apr 2012
      • 56

      #3
      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

      Code:
      Line 26: 'create the mail message
      Line 27: 
      Line 28: myMessage.From = New MailAddress("user_email") 'User email completed by user I get the following error:
      Exception Details: System.FormatEx ception: The specified string is not in the form required for an e-mail address.

      However, with Line 28 like this:

      Code:
      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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Hi Bluenose,

        What type of web application did you create?
        MVC or Web Forms?

        Comment

        • Bluenose
          New Member
          • Apr 2012
          • 56

          #5
          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 ).

          Comment

          • Bluenose
            New Member
            • Apr 2012
            • 56

            #6
            Just checking

            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:

            Code:
            <input type="text" name="user_name" runat="server">
             <input id="user_email" type="email" name="user_email">
             <input type="text" name="user_subject">
             <textarea name="message" id="user_message"></textarea>
            that the following in my contact.aspx.vb is also correct:

            Code:
            Dim Name As String = "username" 'where username is the unknown variable of the user's name
            
            Dim Email As String = "useremail" 'where useremail is the unknown variable of the user's email
            
            Dim Subject As String = "usersubject" 'where usersubject is the unknown subject of the user
            
            Dim Message As String = "usermessage" 'where usermessage is the unknown message the user will write
            
            'Request.Form - extract data from form fields
            
            Dim username As String = Request.Form("user_name") 'where "user_name" is the known ID of the name field
            
            Dim useremail As String = Request.Form("user_email") 'where "user_email" is the known ID of the email field
            
            Dim usersubject As String = Request.Form("user_subject") 'where "user_subect" is the known ID of the subject field
                    
            Dim usermessage As String = Request.Form("user_message") 'where "user_message" is the known ID of the message field
            
            'create the mail message
            
            myMessage.From = New MailAddress(useremail) 'User email completed by user
            
            myMessage.To.Add(New MailAddress("info@mysite.net")) 'Address of the Webmaster who will receive the user's email message
            
            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
            
            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.

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              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:
              Code:
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head runat="server">
                  <title></title>
                  <style type="text/css">
                      .prompt
                      {
                          display: block;
                          font-weight: bold;
                      }
                      .messageTextArea, .textInput
                      {
                          width: 20em;
                      }
                      .messageTextArea
                      {
                          height: 20em;
                      }
                  </style>
              </head>
              <body>
                  <form id="form1" runat="server">
                  <div>
                      <div>
                          <span class="prompt">Please provide your email:</span>
                          <asp:TextBox ID="user_email" runat="server" CssClass="textInput" /></div>
                      <div>
                          <span class="prompt">Please provide your subject:</span>
                          <asp:TextBox ID="user_subject" runat="server" CssClass="textInput" />
                      </div>
                      <div>
                          <span class="prompt">Please provide your message:</span>
                          <asp:TextBox ID="user_message" runat="server" Wrap="true" TextMode="MultiLine" CssClass="messageTextArea" />
                      </div>
                      <asp:Button ID="SendEmail" runat="server" Text="Send Email" />
                  </div>
                  </form>
              </body>
              </html>
              When ASP.NET sends this to the browser, it is converted into HTML and it looks like this:
              Code:
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head><title>
              
              </title>
                  <style type="text/css">
                      .prompt
                      {
                          display: block;
                          font-weight: bold;
                      }
                      .messageTextArea, .textInput
                      {
                          width: 20em;
                      }
                      .messageTextArea
                      {
                          height: 20em;
                      }
                  </style>
              </head>
              <body>
                  <form method="post" action="./contact.aspx" id="form1">
              <div class="aspNetHidden">
              <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIODY2MjY4OThkZJqkRtnq0NOON5F088ku42vFoM2qk5VPCGYvsOj1lnC3" />
              </div>
              
              <div class="aspNetHidden">
              
              	<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CD2448B2" />
              	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAWoASbgPc0NVNHyADJ8kYTN8WW+kaztJcywh7J3De2DyObVknTnR8DQ8XKfcR0YLpUfjPWxNAflCdG320Un8qULhm2+bBR3PLoxYiWNvjlmDhwLAUvc6M1f26OT+paV9sDCllv7rOjBDebf7FFjtYC1" />
              </div>
                  <div>
                      <div>
                          <span class="prompt">Please provide your email:</span>
                          <input name="user_email" type="text" id="user_email" class="textInput" /></div>
                      <div>
                          <span class="prompt">Please provide your subject:</span>
                          <input name="user_subject" type="text" id="user_subject" class="textInput" />
                      </div>
                      <div>
                          <span class="prompt">Please provide your message:</span>
                          <textarea name="user_message" rows="2" cols="20" id="user_message" class="messageTextArea">
              </textarea>
                      </div>
                      <input type="submit" name="SendEmail" value="Send Email" id="SendEmail" />
                  </div>
                  </form>
              </body>
              </html>
              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:

              Code:
              Public Class contact
                  Inherits System.Web.UI.Page
              
                  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
              
                  End Sub
              
              
                  Private Sub SendEmail_Click(sender As Object, e As System.EventArgs) Handles SendEmail.Click
                      Dim suppliedEmailText = user_email.Text
                      Dim suppliedSubjectText = user_subject.Text
                      Dim suppliedMessageText = user_message.Text
              
              
                  End Sub
              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!
              Last edited by Frinavale; Dec 1 '17, 05:59 PM.

              Comment

              • Bluenose
                New Member
                • Apr 2012
                • 56

                #8
                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.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  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.

                  Comment

                  • Bluenose
                    New Member
                    • Apr 2012
                    • 56

                    #10
                    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

                    Code:
                    LblDate.Text = ReturnDate()
                    
                            user_name.Focus()
                    which I am trying to sort out.

                    Thanks again, Frinavale.

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      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.

                      Comment

                      Working...