0

i am designing a website by asp.net c# , and i am a beginner to the language. i have created contact us page and i followed altos of tutorials but it did not work, can anyone help me what's the error? this is my contactus.aspx page : <

table>
    <!-- Name -->
    <tr>
        <td align="center">
            Name:</td>
        <td style="width: 327px">
            <asp:TextBox ID="txtName" 
                            runat="server"
                            Columns="50"></asp:TextBox>
        </td>
        <td style="width: 306px">
            &nbsp;</td>
    </tr>

    <!-- Subject -->
    <tr>
        <td align="center">
            Email:
        </td>
        <td style="width: 327px">
            <asp:TextBox ID="txtEmail" 
                            runat="server"
                            Columns="50" TextMode="Email"></asp:TextBox>
        </td>
        <td style="width: 306px">
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ErrorMessage="please Enter Email address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
        </td>
    </tr>

    <!-- Message -->
    <tr>
        <td align="center">
            Subject</td>
        <td style="width: 327px">
            <asp:TextBox ID="txtsubject" 
                            runat="server"
                            Columns="50"></asp:TextBox>
        </td>
        <td style="width: 306px">
            &nbsp;</td>
    </tr>

    <tr>
        <td align="center">
            Message:
        </td>
        <td style="width: 327px">
            <asp:TextBox ID="txtMessage" 
                            runat="server"
                            Columns="40"
                            Rows="6" 
                            TextMode="MultiLine"></asp:TextBox>
        </td>
        <td style="width: 306px">
            &nbsp;</td>
    </tr>

    <!-- Submit -->
    <tr align="center">
        <td colspan="2">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                onclick="btnSubmit_Click" style="width: 61px" />
        </td>
        <td style="width: 306px">
            &nbsp;</td>
    </tr>

    <!-- Results -->
    <tr align="center">
        <td colspan="2">
            <asp:Label ID="lblResult" runat="server"></asp:Label>
        </td>
        <td style="width: 306px">
            &nbsp;</td>
    </tr>
</table>

and this is my code behind contactus.aspx.cs:

 protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {

            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress("ranna.fal@gmail.com");
            mailMessage.To.Add("ranna.fal@gmail.com");
            mailMessage.Subject = txtsubject.Text;

            mailMessage.Body = "<b>Sender Name : </b>" + txtName.Text + "<br/>"
                + "<b>Sender Email : </b>" + txtEmail.Text + "<br/>"
                + "<b>Comments : </b>" + txtMessage.Text;
            mailMessage.IsBodyHtml = true;


            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new
                System.Net.NetworkCredential("ranna.fal@gmail.com", "my password");
            smtpClient.Send(mailMessage);

            lblResult.ForeColor = System.Drawing.Color.Blue;
            lblResult.Text = "Thank you for contacting us";

            txtName.Enabled = false;
            txtEmail.Enabled = false;
            txtMessage.Enabled = false;
            txtsubject.Enabled = false;
            btnSubmit.Enabled = false;
        }

    catch (Exception ex)
    {
        // Log the exception information to 
        // database table or event viewer
        lblResult.ForeColor = System.Drawing.Color.Red;
        lblResult.Text = "There is an unknown problem. Please try later";
    }
}

also i included this: using System.Net.Mail; the text : There is an unknwon problem. Please try later , is keep showing

thank you

  • 3
    What's the `ex.Message` property set to? You're essentially saying, "hey, I want to catch any exception that happens! But the valuable info about what failed... throw that away please." – Lynn Crumbling Mar 17 '15 at 19:53
  • See if this post helps with your problem: http://stackoverflow.com/questions/9801224/smtpclient-with-gmail – JustAPup Mar 17 '15 at 20:01
  • thank you , i removed the ex, and it's working ! – Rana Fahad Mar 17 '15 at 20:03
  • possible duplicate of [Sending email through Gmail SMTP server with C#](http://stackoverflow.com/questions/704636/sending-email-through-gmail-smtp-server-with-c-sharp) – mason Mar 17 '15 at 20:04

1 Answers1

0

If you have 2 factor authentication turned on on you will need a device specific password or shut off 2 factor authentication.

Similar question / issue

Community
  • 1
  • 1
kmcnamee
  • 5,097
  • 2
  • 25
  • 36