我的联系我们asp.net页面没有发送电子邮件

时间:2015-03-17 19:49:36

标签: c# asp.net forms sendmail contactus

我正在通过asp.net c#设计一个网站,我是该语言的初学者。 我已经创建了联系我们页面,我按照教程的高手,但它没有工作,任何人都可以帮助我的错误是什么?这是我的contactus.aspx页面: <

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>

这是我在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";
    }
}

我还包括这个: 使用System.Net.Mail; 文:有一个unknwon问题。请稍后再试,继续显示

谢谢

1 个答案:

答案 0 :(得分:0)

如果您打开了2因素身份验证,则需要设备专用密码或关闭2因素身份验证。

Similar question / issue