none
trouble : contact us form using asp.net c# RRS feed

  • Question

  • i tried lots of form in aspx form with references and healthy smtp adresses but i can't use the contact form. can somebody help me about this situation ?

    Here is my contactus.aspx form ;

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactControl.ascx.cs"
        Inherits="Controls_ContactControl" %>
    <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSubmit">
        <p>
            Please Fill the Following to Send Mail.</p>
        <p>
            Your name:
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ErrorMessage="*"
                ControlToValidate="YourName" ValidationGroup="save" /><br />
            <asp:TextBox ID="YourName" runat="server" Width="250px" /><br />
            Your email address:
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                ControlToValidate="YourEmail" ValidationGroup="save" /><br />
            <asp:TextBox ID="YourEmail" runat="server" Width="250px" />
            <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator23"
                SetFocusOnError="true" Text="Example: username@gmail.com" ControlToValidate="YourEmail"
                ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic"
                ValidationGroup="save" /><br />
            Subject:
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
                ControlToValidate="YourSubject" ValidationGroup="save" /><br />
            <asp:TextBox ID="YourSubject" runat="server" Width="400px" /><br />
            Your Question:
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"
                ControlToValidate="Comments" ValidationGroup="save" /><br />
            <asp:TextBox ID="Comments" runat="server" TextMode="MultiLine" Rows="10" Width="400px" />
        </p>
        <p>
            <asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="Button1_Click" ValidationGroup="save" />
        </p>
    </asp:Panel>
    <p>
        <asp:Label ID="lblMsgSend" runat="server" Visible="false" />
    </p>

    And this is my smtp settings as contactus.aspx.cs ;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Mail;
    using System.Net;

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

        }

        protected void SendMail()
        {
            var fromAddress = "Gmail@gmail.com";// Gmail Address from where you send the mail
            var toAddress = YourEmail.Text.ToString();
            const string fromPassword = "Password";//Password of your gmail address
            string subject = YourSubject.Text.ToString();
            string body = "From: " + YourName.Text + "\n";
            body += "Email: " + YourEmail.Text + "\n";
            body += "Subject: " + YourSubject.Text + "\n";
            body += "Question: \n" + Comments.Text + "\n";
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            smtp.Send(fromAddress, toAddress, subject, body);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                SendMail();
                lblMsgSend.Text = "Your Comments after sending the mail";
                lblMsgSend.Visible = true;
                YourSubject.Text = "";
                YourEmail.Text = "";
                YourName.Text = "";
                Comments.Text = "";
            }
            catch (Exception) { }
        }
    }


    Where im wrong?


    • Edited by theprovoker Monday, January 15, 2018 1:42 PM
    • Moved by Sabah ShariqMVP Tuesday, January 16, 2018 3:33 PM Moved From Visual C#
    Monday, January 15, 2018 1:41 PM

All replies