【问题标题】:Sending an Email with Godaddy contact form using gmail使用 gmail 发送带有 Godaddy 联系表的电子邮件
【发布时间】:2013-09-19 10:01:23
【问题描述】:

我正在尝试使用联系表发送电子邮件。我正在使用 godaddy 作为我的托管服务提供商。

会出现以下错误

<configuration>
    <system.web>
    <customErrors mode="Off"/>
</system.web>
</configuration>

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

我正在使用带有 c# 的 asp.net,并且我正在将代码编写在同一文件中,即 contact.aspx.cs 联系表格式:

我的标记:

 <table>
        <tr>
            <td align="center" colspan="3" class="style32">
                <span style="color: #009966; font-family: Verdana; font-size: 16pt;">
                <strong></strong></span>
                <span style="font-family: Verdana; font-size: 16px;" class="requiredText">
                <strong class="style25">Enter Your Details</strong></span><br />

            </td>
        </tr>
         <tr>
            <td align="right" class="style14">
                <span style="font-family: Verdana" class="style23">
                Name</span>
                <span style="color: #ff0033"> *</span>
                </td>

            <td class="style31">:
            </td>

            <td class="style33">
                <asp:TextBox ID="txtName" runat="server" Font-Names="Verdana" Font-Size="9.6pt"
                    Width="232px" BorderColor="GrayText" BorderStyle="Solid"  Height="32px"></asp:TextBox>
                    </td>
        </tr>

         <tr>
            <td align="right" class="style24">
                Mobile No</span>
                <span style="color: #ff0033"> *</span>
               </td>
            <td class="style13">:
            </td>
            <td class="style34">
                <asp:TextBox ID="txtMobileNo" runat="server" Font-Names="Verdana" Font-Size="9.6pt"
                    Width="232px" BorderColor="GrayText" BorderStyle="Solid" Height="32px"></asp:TextBox><span style="color: #ff0033"> </span>
                </td>
        </tr>

        <tr style="font-size: 12pt">
            <td class="style11" align="right">
                <span style="font-family: Verdana"><span class="style23">
                E-mail </span></span>
                <span style="font-family: Verdana"><span style="font-size: 0.8em">
                <span style="color: #ff0033" class="style23">*</span></span></span></td>
            <td class="style13">:
            </td>
            <td class="style34">
                <asp:TextBox ID="txtEmail" runat="server" Font-Names="Verdana" Font-Size="9.6pt"
                    Width="232px" BorderColor="GrayText" BorderStyle="Solid" Height="32px"></asp:TextBox>

            </td>
        </tr>

         <tr style="font-size: 12pt">
             <td class="style11" align="right">
                <span style="font-family: Verdana"><span class="style23">
                Message</span></span>
                <span style="font-family: Verdana"><span style="font-size: 0.8em">
                <span style="color: #ff0033" class="style23">*</span></span></span></td>
            <td class="style13">:
            </td>
            <td>
                <asp:TextBox ID="txtMessage" runat="server" Font-Names="Verdana" Font-Size="9.6pt"
                    TextMode="MultiLine" Width="232px" BorderColor="GrayText" 
                    BorderStyle="Solid" Height="62px"></asp:TextBox>
            </td>
        </tr>

        <tr style="font-size: 12pt">
        <td class="style14"></td>
        <td class="style31"></td>
            <td class="style28">
                <br />
                <asp:Button ID="btnSubmit" runat="server" Font-Names="Verdana" Font-Size="11pt" OnClick="btnSubmit_Click"
                    Text="Submit" ValidationGroup="first" BorderColor="GrayText" 
                    BorderStyle="Solid" BackColor="Red" ForeColor="White" />

            </td>
        </tr>

    <tr>
    <td class="style14">
        &nbsp;</td>
    </tr>
</table>

我的 Contact.aspx.cs:

public partial class Contact : System.Web.UI.Page
{
    //string strCS = ConfigurationManager.ConnectionStrings["CS"].ToString();
    string Adsfile, strBodymessage;
    string strMobileNo;

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //string strLandline = txtLandCountryCode.Text + "-" + 
        txtLandCityCode.Text +          "-" + txtLandlineNo.Text;
        strMobileNo = txtMobileNo.Text;

        Sendmail(txtEmail.Text);
        ClientScript.RegisterStartupScript(this.GetType(), "ele", " 
         <script> alert('example.com<br>
   Information Successfully Send to Your SMS/Email !.');</script>");

        ResetAll();
    }

    public void Sendmail(string ToID)
    {
        MailMessage msg = new MailMessage();
        SmtpClient c = new SmtpClient();
        msg.From = new MailAddress("xxxx@gmail.com", "example.com");
        msg.To.Add(new MailAddress(ToID));

        msg.Subject = "Response to Your Message.<br> www.example.com";

        strBodymessage = "Dear <b>" + txtName.Text + "</b>,<br>";
        strBodymessage += " Mobile no : <b>" + strMobileNo + "</b>,<br>";
        strBodymessage += "<br>   <b> Conformation Mail</b>:<br><br> Message<br>";
        strBodymessage += "<br>  We hope the Above Information is Useful for You<br>  <br>";
        strBodymessage += "<b>    Thankyou for Contacting us!</b><br>
        Help Line No:   xxxxxxx<br>";
        msg.Body = strBodymessage;

        msg.IsBodyHtml = true;
        c.Host = "smtp.gmail.com";
        c.EnableSsl = true;
        c.Port = 587;
        c.UseDefaultCredentials = true;
        c.Credentials = new System.Net.NetworkCredential("xxxx@gmail.com", "pwd");
        c.Send(msg);
    }

    public void ResetAll()
    {
        txtName.Text = "";
        txtMobileNo.Text = "";
        txtEmail.Text = "";
    }
}

名称:文本框,

电话:文本框,

电子邮件:文本框(访客电子邮件 ID)

消息:访客留言

提交按钮

当用户输入详细信息并提交页面时,我应该会收到电子邮件(yyyy@example.com)

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    我建议先关闭CustomErrors,这样你就可以看到它抛出的异常。

    不管怎样,简单的代码就像

    标记:

    <asp:TextBox ID="Email" runat="server" />
    <asp:TextBox ID="Message" runat="server" TextMode="MultiLine"  />
    <asp:Button runat="server" OnClick="SendEmail_Click" />
    

    代码:

    protected void SendEmail_Click(object sender, EventArgs e) {
    
    MailMessage message = new MailMessage();
    message.From = new MailAddress(Email.Text);
    
    message.To.Add(new MailAddress("YourEmailAddress"));
    
    message.Subject = "New message from Web";
    message.Body = Message.Text;
    
    SmtpClient client = new SmtpClient();
    client.Send(message);
    
    }
    

    当然,假设您的 SMTP 在 Web.config 文件中配置,应该可以工作。

    【讨论】:

    • 先生你能给我简单的解释吗,使用我的代码你可以编辑错误的地方给我先生
    • 我看代码时没有发现任何问题,所以我认为没有问题。针对 SmtpClient 可能抛出的异常关闭 customErrors。或者在那里使用try catch。并关闭您的 UseDefaultCredentials,因为您没有使用默认值,这可能是未经授权的问题。
    猜你喜欢
    • 2017-12-04
    • 2018-01-10
    • 2021-03-26
    • 2013-10-10
    • 1970-01-01
    • 2018-04-15
    • 2020-05-20
    相关资源
    最近更新 更多