Search code examples
asp.netvb.netsmtpclient

Aspx content page, unable to execute send email code


I'm using an asp content page that uses site master file. I'm particularly confused about the runat=server with the labels and getting the vb.net to execute. I tried this but its not working:

<div class="card-body">
    <asp:Label ID="Label1" runat="server" />
    <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" aria-describedby="emailHelp" placeholder="Enter name" required>
    </div>
    <div class="form-group" runat="server">
        <asp:Label for="email" runat="server" Text="Email address"></asp:Label>
        <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" required>
        <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
    </div>
    <div class="form-group">
        <label for="message">Message</label>
        <textarea class="form-control" id="message" rows="6" required></textarea>
    </div>
    <div class="mx-auto">
        <asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit"></asp:Button>
    </div>

I tried putting the html code in a form but errors saying cannot have nested form, so confused as to how to send the html to the vb.net code

VB.NET code:

Protected Sub Btn_SendMessage_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim Email As String = FindControl("email").ToString
    Dim Name As String = FindControl("name").ToString
    Dim Message As String = FindControl("message").ToString
    Dim Mail As New MailMessage
    Dim SMTP As New SmtpClient("smtp.gmail.com")

    Mail.Subject = Name
    Mail.From = New MailAddress(Email)
    SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "xxxxxxxx") '<-- Password Here

    Mail.To.Add("[email protected]") 

    Mail.Body = Message

    SMTP.EnableSsl = True
    SMTP.Port = "587"

    Try
        SMTP.Send(Mail)
        Label1.Text = "Message sent"
    Catch ex As Exception
        Label1.Text = ex.ToString
    End Try
End Sub

The page refreshes and nothing happens, I don't even think the VB.NET executes.


Solution

  • Here's a working sample based on your code:

    ASPX

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="card-body">
                <asp:Label ID="Label1" runat="server" />
                <div class="form-group">
                    <label for="name">Name</label>
                    <asp:Label ID="Label4" runat="server" AssociatedControlID="TextBoxName" Text="Name"></asp:Label>
                    <asp:TextBox ID="TextBoxName" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ErrorMessage="*" ControlToValidate="TextBoxName"></asp:RequiredFieldValidator>
                </div>
                <div class="form-group" runat="server">                
                    <asp:Label ID="Label3" runat="server" AssociatedControlID="TextBoxEmail" Text="Email address"></asp:Label>
                    <asp:TextBox ID="TextBoxEmail" runat="server" CssClass="form-control" placeholder="Enter name"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="*" ControlToValidate="TextBoxEmail"></asp:RequiredFieldValidator>
                    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                </div>
                <div class="form-group">
                    <asp:Label ID="Label2" runat="server" AssociatedControlID="TextBoxMessage" Text="Message"></asp:Label>
                    <asp:TextBox ID="TextBoxMessage" runat="server" TextMode="MultiLine" CssClass="form-control" Rows="6"></asp:TextBox>                
                </div>
                <div class="mx-auto">
                    <asp:Button type="submit" class="btn btn-primary text-right" ID="Btn_SendMessage" runat="server" Text="Submit" OnClick="Btn_SendMessage_Click"></asp:Button>
                </div>
            </div>
        </form>
    </body>
    </html>
    

    Code behind

    Protected Sub Btn_SendMessage_Click(sender As Object, e As EventArgs) Handles Btn_SendMessage.Click
        Dim Mail As New MailMessage
        Dim SMTP As New SmtpClient("smtp.gmail.com")
    
        Mail.Subject = TextBoxName.Text
        Mail.From = New MailAddress(TextBoxEmail.Text)
        SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "xxxxxxxx") '<-- Password Here
    
        Mail.To.Add("[email protected]")
    
        Mail.Body = TextBoxMessage.Text
    
        SMTP.EnableSsl = True
        SMTP.Port = "587"
    
        Try
            SMTP.Send(Mail)
            Label1.Text = "Message sent"
        Catch ex As Exception
            Label1.Text = ex.ToString
        End Try
    End Sub
    

    Tags with the runat="server" attribute are ASP.NET server side controls which render as their HTML counterparts in the resulting web page sent to the browser. During the server side processing of the page you can access them in your code directly using their IDs.

    Please note that I've used server side validation controls for the required fields. You may need to add the following setting in the Web.config file in case you receive a runtime error about Unobtrusive Validation:

    <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
    </appSettings>
    

    Hope it helps. I am using Visual Studio 2017 and the target framework is .NET 4.6.1.