使用gmail通过Godaddy联系表发送电子邮件

时间:2013-09-19 10:01:23

标签: c# asp.net

我正在尝试使用联系表单发送电子邮件。我使用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>

My 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)

1 个答案:

答案 0 :(得分:0)

我建议先关闭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文件中配置的。