I need help with a form to e-mail code. Any help will be very appreciated.

Discussion in 'Email' started by Sonia Carter, May 8, 2018.

  1. I am very new in this matter. I checked the KB but is not helping me at all. I also checked all the posting here at the forum but since I am new at this is not helping either.

    The customer will fill the form and will send us an e-mail.

    The form is in vb:
    FlyerForm.aspx


    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="FlyersForm.aspx.vb" Inherits = System.Web.UI.Page%>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <style type="text/css">
    .auto-style1 {
    font-family: Arial, Helvetica, sans-serif;
    }
    .auto-style2 {
    font-family: Arial, Helvetica, sans-serif;
    color: #FF0000;
    }
    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <span class="auto-style1"><strong>Please fill and submit the form to subscribe to the Flyer mailing list from XXXXXX.</strong></span><strong><br class="auto-style1" />
    </strong><span class="auto-style2"><strong>WHOLESALE AND/OR DISTRIBUTORS only PLEASE.</strong></span><br class="auto-style1" />
    <br class="auto-style1" />
    <span class="auto-style1">Name:
    <asp:TextBox ID="txtName" runat="server" required CssClass="auto-style1" Width="182px"></asp:TextBox>
    <br />
    &nbsp; Last name: </span>
    <asp:TextBox ID="txtLast_Name" runat="server" required CssClass="auto-style1" Width="349px"></asp:TextBox>
    <br class="auto-style1" />
    <br class="auto-style1" />
    <span class="auto-style1">Company: </span>
    <asp:TextBox ID="txtCompany" runat="server" required CssClass="auto-style1" Width="547px"></asp:TextBox>
    <br class="auto-style1" />
    <br class="auto-style1" />
    <span class="auto-style1">Telephone: </span>
    <asp:TextBox ID="txtTelephone" runat="server" required CssClass="auto-style1" Width="395px"></asp:TextBox>
    <br class="auto-style1" />
    <br class="auto-style1" />
    <span class="auto-style1">E-Mail: </span>
    <asp:TextBox ID="txtE_Mail" runat="server" required CssClass="auto-style1" Width="437px"></asp:TextBox>
    <br class="auto-style1" />
    <br class="auto-style1" />
    <span class="auto-style1">Are you a XXXXX Customer?&nbsp; YES </span>or NO&nbsp;
    <asp:TextBox ID="YESorNO" runat="server" required></asp:TextBox>
    <br class="auto-style1" />
    <span class="auto-style1"><em>If you are not an actual customer a sales representative will call you before your e-mail is added to our mailing list.</em></span><br class="auto-style1" />
    <br class="auto-style1" />
    <span class="auto-style1"><strong>By submiting this form you are asking to receive the Newsletter, Specials and or Flyers from XXXXXXX.<br />
    </strong></span>
    <br class="auto-style1" />
    <br class="auto-style1" />
    <asp:Button ID="SUBMIT" runat="server" CssClass="auto-style1" Text="SUBMIT" Width="86px" />
    <br />
    <asp:Label runat="server" ID="lblConfirm" />
    <br />

    </div>
    </form>
    </body>
    </html>

    What is the right code for the FlyerForm.aspx.vb since what I have is not doing anything, not even an error:

    Imports System.Net.Mail

    Public Class FlyersForm
    Inherits System.Web.UI.Page

    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles SUBMIT.Click
    Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

    Dim myMessage As New System.Net.Mail.MailMessage
    Dim Smtpserver As New Net.Mail.SmtpClient
    Dim Name As String = "Name"
    Dim Last_Name As String = "Last_Name"
    Dim Company As String = "Company"
    Dim Telephone As String = "Telephone"
    Dim E_mail As String = "E_Mail"
    Dim YESorNO As String = "YESorNO"


    'Request.Form - extract data from form fields

    Dim Name As String = Request.Form("Name")
    Dim Last_Name As String = Request.Form("Last_Name")
    Dim Company As String = Request.Form("Company")
    Dim Telephone As String = Request.Form("Telephone")
    Dim E_Mail As String = Request.Form("E_Mail")
    Dim YESorNO As String = Request.Form("YESorNO")


    mailMessage.From = New System.Net.Mail.MailAddress(txtE_mail.Text.Trim())
    mailMessage.Name = txtName.Text.Trim()
    mailMessage.Last_Name = txtLast_Name.Text.Trim()
    mailMessage.Company = txtCompany.Text.Trim()
    mailMessage.Telephone = txtTelephone.Text.Trim()
    mailMessage.E_Mail = txtE_mail.Text.Trim()
    mailMessage.YESorNO = txtYESorNOText.Trim()

    Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()

    smtpClient.Send(mailMessage)

    Try
    SendMail("[email protected]", sb.ToString())
    Catch ex As Exception
    lblConfirm.Text = "There was an error"
    End Try
    End Sub

    Protected Sub SendMail(ByVal from As String, ByVal body As String)

    Dim Email As New MailMessage("[email protected]", txtE_mail.Text, "Subject of Email", body)

    Dim smtpClient As SmtpClient = New SmtpClient
    smtpClient.Host = "mail.mydomain.com"

    smtpClient.EnableSsl = False

    Dim credentials As New System.Net.NetworkCredential("[email protected]", "*********")

    smtpClient.Credentials = credentials
    smtpClient.Send(Email)

    lblConfirm.Text = "Success"

    End Class

    I have nothing added in the Web.config file
     
  2. Elshadriel

    Elshadriel Winhost Staff

    Did you replace mydomain.com with your actual domain name and did you point the DNS over to us?
     
  3. ComputerMan

    ComputerMan Winhost Staff

    If you're domain name is still not pointing to our hosting service. You can use the email server's direct URL as the SMTP host instead.
     
    Elshadriel likes this.
  4. I believe is more complicated than that. The code is not right. But don't know exactly where is the problem. Will be working tomorrow and will be back here with a solution or more questions. Thank you for your reply.
     
  5. OK, I am back.
    I was able to make the form email works.
    Now I need help with another button.

    I added a new button at the end of the form to give the customer the choice to go back to the Home page even if he/she did not fill or send the form.
    But I was not able to make that button to do it.



    Here the form: (I changed the emails, password etc for obvious reasons)


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!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">

    .auto-style1 {
    font-family: Arial, Helvetica, sans-serif;
    }
    .auto-style2 {
    font-family: Arial, Helvetica, sans-serif;
    color: #FF0000;
    font-size: medium;
    }
    .newStyle1 {
    font-family: Arial, Helvetica, sans-serif;
    width: 620px;
    }
    .auto-style3 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: medium;
    }
    .auto-style4 {
    width: 137px;
    font-weight: bold;
    }
    .auto-style5 {
    font-weight: bold;
    font-size: medium;
    }
    .auto-style6 {
    font-family: Arial, Helvetica, sans-serif;
    font-weight: normal;
    }
    .auto-style7 {
    width: 137px;
    font-weight: bold;
    font-family: Arial, Helvetica, sans-serif;
    height: 45px;
    font-size: small;
    }
    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <h2>

    <span class="auto-style3"><strong>Please fill and submit the form to subscribe to the Flyer email list from our Company.</strong></span><strong><br class="auto-style3" />
    </strong><span class="auto-style2"><strong>WHOLESALE AND/OR DISTRIBUTORS only PLEASE.</strong></span></h2>
    <br />
    <table class="newStyle1">
    <!-- Name -->
    <tr>
    <td align="center" class="auto-style4">
    Name:</td>
    <td>
    <asp:TextBox ID="txtName"
    runat="server" required
    Columns="50" Width="361px"></asp:TextBox>
    </td>
    </tr>

    <tr>
    <td align="center" class="auto-style4">
    Last_Name :</td>
    <td>
    <asp:TextBox ID="txtLastName"
    runat="server" required
    Columns="50" Width="359px"></asp:TextBox>
    </td>
    </tr>
    <!-- Subject -->
    <tr>
    <td align="center" class="auto-style4">
    Email :</td>
    <td>
    <asp:TextBox ID="txtEmail"
    runat="server" required
    Columns="50" Width="359px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td align="center" class="auto-style4">
    Telephone:</td>
    <td>
    <asp:TextBox ID="txtTelephone"
    runat="server" required
    Columns="50" Width="359px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td align="center" class="auto-style4">
    Comments:
    </td>
    <td>
    <asp:TextBox ID="txtMessage"
    runat="server"
    Columns="40"
    Rows="6"
    TextMode="MultiLine"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td align="center" class="auto-style4">
    Company :</td>
    <td>
    <asp:TextBox ID="txtCompany"
    runat="server" required
    Columns="50" Width="359px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td align="center" class="auto-style7">
    Are you a Phoenix MA Customer?
    <br />
    Yes or No </td>
    <td>
    <asp:TextBox ID="txtCustomer"
    runat="server" required
    Columns="50" Width="140px"></asp:TextBox>
    </td>
    </tr>
    <!-- Message -->
    <tr>
    <td align="center" class="auto-style5" colspan="2">
    <span class="auto-style6"><em>If you are not an actual customer a sales representative will call you before your e-mail is added to our Email list.</em></span><b><br class="auto-style1" />
    <br class="auto-style1" />
    </b>
    <span class="auto-style1"><strong>By submitting this form you are asking to receive the Newsletter, Specials and or Flyers from our Company.</strong></span></td>
    </tr>
    <!-- Submit -->
    <tr align="center">
    <td colspan="2">
    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
    onclick="btnSubmit_Click" CssClass="auto-style5" Width="179px" />
    </td>
    </tr>

    <!-- Results -->
    <tr align="center">
    <td colspan="2">
    <b>
    <asp:Label ID="lblResult" runat="server" style="color: #FF0000"></asp:Label>
    </b>
    </td>
    </tr>
    <tr align="center">
    <td colspan="2">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="font-weight: 700; font-size: medium; color: #FFFFFF; background-color: #0000FF" Text="Back to Website" Width="208px" />
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>



    And here is the Code:

    using System;
    using System.Web.UI.WebControls;
    using System.Net.Mail;
    using System.Net;

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    try
    {
    //Create the msg object to be sent
    MailMessage msg = new MailMessage();
    //Add your email address to the recipients
    msg.To.Add("[email protected]");
    //Configure the address we are sending the mail from
    MailAddress address = new MailAddress("[email protected]");
    msg.From = address;
    //Append their name in the beginning of the subject
    msg.Subject = "Flyer Subscription";
    msg.Body = "Name: " + txtName.Text + Environment.NewLine+
    "Last Name: " + txtLastName.Text + Environment.NewLine+
    "EMail: " + txtEmail.Text + Environment.NewLine+
    "Telephone: " + txtTelephone.Text + Environment.NewLine+
    "Comments: " + txtMessage.Text + Environment.NewLine+
    "Company: " + txtCompany.Text + Environment.NewLine+
    "Customer: " + txtCustomer.Text;




    //Configure an SmtpClient to send the mail.
    SmtpClient client = new SmtpClient("mail.myemail.com");
    client.EnableSsl = false; //only enable this if your provider requires it
    //Setup credentials to login to our sender email address ("UserName", "Password")
    NetworkCredential credentials = new NetworkCredential("[email protected]", "***********");
    client.Credentials = credentials;

    //Send the msg
    client.Send(msg);

    //Display some feedback to the user to let them know it was sent
    lblResult.Text = "Your message was sent!";

    //Clear the form
    txtName.Text = "";
    txtLastName.Text = "";
    txtEmail.Text = "";
    txtTelephone.Text = "";
    txtMessage.Text = "";
    txtCompany.Text = "";
    txtCustomer.Text = "";
    }
    catch
    {
    //If the message failed at some point, let the user know
    lblResult.Text = "Your message failed to send, please try again.";
    }

    //This is the button that the customer will use to go back to the Home page even if did not fill or send the form
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    ???????????????????????????????????????????????????
    }
    }
     
  6. Elshadriel

    Elshadriel Winhost Staff

    You can use:

    Response.Redirect("~\Home.aspx");

    or whatever the name of your homepage is. The value in quotes is just an example.
     
  7. Thank you Elshadriel, it works!

    Last question: If I am going to use this form in a website where I have a Master page like Template6.Master but this Master page is in VB code instead of C#, how can I make the form to be shown within the Master page?
     
  8. The problem was solved. Thank you to all that help.
    Form is up and running and it is with the master page.
     
    Elshadriel and ComputerMan like this.

Share This Page