2012-08-28 117 views
1

我有我的表单启动并运行没有错误。然而,当我按发送浏览器说等待本地主机然后停止,并没有电子邮件发送。所有的SMTP设置似乎是正确的。继承人我的代码,但我已经拿出我的电子邮件和密码的细节。ASP电子邮件表格将不会发送

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"  Inherits="System.Web.Mvc.ViewPage<dynamic>" %> 
<%@Import Namespace="System.Web.Mail" %> 
<%@Import Namespace="System.Net" %> 
<script language="c#" runat="server"> 

protected void SendMail() 
    { 
     // Gmail Address from where you send the mail 
     var fromAddress = "[removed]"; 
     // any address where the email will be sending 
     var toAddress = YourEmail.Text.ToString(); 
     //Password of your gmail address 
     const string fromPassword = "[removed]"; 
     // Passing the values and make a email formate to display 
     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"; 
     // smtp settings 

     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; 
     } 
     // Passing values to smtp object 
     smtp.Send(fromAddress, toAddress, subject, body); 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      //here on button click what will done 
      SendMail(); 
      DisplayMessage.Text = "Your Comments after sending the mail"; 
      DisplayMessage.Visible = true; 
      YourSubject.Text = ""; 
      YourEmail.Text = ""; 
      YourName.Text = ""; 
      Comments.Text = ""; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 

     } 
    } 


</script> 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
Tickets 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 



<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: [email protected]" 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="DisplayMessage" runat="server" Visible="false" /> 
</p> 

+0

您是否尝试过在代码中设置断点?你有例外吗? – kprobst

+0

奇怪的是,代码不会从SendMail()运行放置断点,但是当我经历这个过程时它不能达到它。也许按钮不运作......不能看到为什么不是tho ...并没有例外 – thai

回答

0

尝试以下操作:

  1. asp:Button标签
  2. 删除ValidationGroup="save"替换为Console.WriteLine(ex.ToString());

    DisplayMessage.Text = ex.ToString();

    DisplayMessage.Visible = true;

让我知道会发生什么。