发送电子邮件至asp.net,html

时间:2016-01-04 10:02:00

标签: html asp.net

这是我的index.aspx表单。

<form role="form" method="post" action="SendMail.aspx">
<div class="form-group">
    <input type="text" class="form-control" id="name" name="name" placeholder="Name"
        required>
</div>
<div class="form-group">
    <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number"
        required>
</div>
<div class="form-group">
    <input type="email" class="form-control" id="email" name="email" placeholder="Email"
        required>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">
    Submit Form</button>
</form>

这是我的SendMail.aspx表单。

<%
Response.Write("Need : " & Request.Form("whatneed") & "<br>")
Response.Write("Budget : " & Request.Form("budget") & "<br>")
Response.Write("When : " & Request.Form("whenneed") & "<br>")
Response.Write("Location : " & Request.Form("location") & "<br>")
Response.Write("Name : " & Request.Form("name") & "<br>")
Response.Write("Description : " & Request.Form("address") & "<br>")
Response.Write("Mobile No : " & Request.Form("mobile") & "<br>")
Response.Write("Landline No : " & Request.Form("landline") & "<br>")
Response.Write("Email Id : " & Request.Form("email") & "<br>")

MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("saravana17.ams@gmail.com");
mailMessage.From = new MailAddress("saro17.ams@gmail.com");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient = new SmtpClient("mail.feo.co.in");
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!"); 
%>

我不知道为什么邮件没有发送到这里。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

根据您的JsFiddle,我发现HTML代码中存在很多愚蠢的错误。

以下是您的ASP.NET代码: -

<form id="form1" runat="server">
<div class="form-group">
    <asp:TextBox ID="txtname" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
    <asp:TextBox ID="txtmobileno" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
    <asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
    <asp:TextBox ID="txtSubject" runat="server" CssClass="form-control"></asp:TextBox>
</div>
<asp:Button ID="btnSubmit" runat="server" CssClass="btn btn-primary pull-right" OnClick="btnSubmit_OnClick" Width="100" Text="Submit" />
</form>

cs代码背后的代码

创建SendMail()函数,该函数将在buttonclick

上触发

注意: - 我还没有在控件上添加验证,因此如果您需要,可以根据需要添加。

protected void SendMail()
{
    // Gmail Address from where you send the mail
    var fromAddress = "Gmail@gmail.com";
    // any address where the email will be sending
    var toAddress = txtEmail.Text.ToString();
    //Password of your gmail address
    const string fromPassword = "Your gmail password";
    // Passing the values and make a email formate to display
    string subject = txtSubject.Text.ToString();
    // Passing the values and make a email formate to display
    string body = "From: " + txtname.Text + "\n";
    body += "Email: " + txtEmail.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);
}

现在将在Button click上调用上述函数,以便每次输入详细信息时都可以调用该函数。

protected void btnSubmit_OnClick(object sender, EventArgs e)
{
    try
    { 
        SendMail(); // Send mail function to send your mail
        txtname.Text = "";
        txtEmail.Text = "";
        txtmobileno.Text = "";
        txtSubject.Text = "";
    }
    catch (Exception ex)
    {
        ex.Message.ToString();
    }
}

有关详细说明,请查看以下链接: -

<强> http://www.codeproject.com/Tips/371417/Send-Mail-Contact-Form-using-ASP-NET-and-Csharp