在asp.net中使用smtp和gmail提交表单后发送电子邮件

时间:2013-11-18 12:15:20

标签: c# asp.net forms email smtp

我的网页上提交表单后,我正在尝试接收电子邮件。目前它提交罚款没有任何错误,但我没有收到电子邮件。有谁知道我必须在代码隐藏页面中添加哪些代码才能使其工作?

这是html;

<h2>Contact Us</h2>
        <br />
        <table>            
            <tr>
                <td  style="align-items:center">
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtName"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td  style="align-items:center">
                    email:</td>
                <td>
                    <asp:TextBox ID="txtEmail"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>



            <!-- Message -->
            <tr>
                <td style="align-items:center">
                    What are you looking for?
                </td>
                <td>
                    <asp:TextBox ID="txtMessage"
                                    runat="server"
                                    Columns="40"
                                    Rows="6"
                                    TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
             <tr>
                <td  style="align-items:center">
                    What would you be willing to pay for this app?</td>
                <td>
                    <asp:TextBox ID="txtPay"
                                    runat="server"
                                    Columns="40"></asp:TextBox>
                </td>
            </tr>

            <!-- Submit -->
            <tr style="align-items:center">
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                        onclick="btnSubmit_Click" /><br />
                </td>
            </tr>

            <!-- Results -->
            <tr style="align-items:center">
                <td colspan="2">
                    <asp:Label ID="lblResult" runat="server"></asp:Label>
                </td>
            </tr>
        </table>

这是背后的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Telluswhatyouwant : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        try
        {
            //Create the msg object to be sent
            MailMessage msg = new MailMessage();
            //Add your email address to the recipients
            msg.To.Add("ronan.byrne@mhlabs.net");

//Send the msg
            client.Send(msg);

3 个答案:

答案 0 :(得分:2)

这是localhost的完美工作代码,其中启用了邮件选项。您可以更改端口号。从哪里,以字符串格式邮寄地址(即:david@yahoo.com)

    string sMailServer = "127.0.0.1";

    MailMessage MyMail = new MailMessage();
    MyMail.From = fromWho;
    MyMail.To = toWho;
    if (toCC != "" || toCC != null)
    {
        MyMail.Cc = toCC;
    }
    if (toBCC != "" || toBCC != null)
    {
        MyMail.Bcc = toBCC;
    }

    MyMail.Subject = Subject;
    MyMail.Body = Body;
    //MyMail.BodyEncoding = Encoding.UTF8;
    MyMail.BodyFormat = MailFormat.Html;

    SmtpMail.SmtpServer = sMailServer;
    try
    {

        SmtpMail.Send(MyMail);


    }
    catch (Exception ex)
    {
        return ex;
    }

答案 1 :(得分:1)

您可以尝试这一点并确保使用有效的登录凭据,并且您具有正确的互联网连接:

 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = 
                  new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
     smtp.Send(mail);                
  }
  catch (Exception ex)
  {   
    // Handle exception here 

  }

答案 2 :(得分:1)

您已配置client对象的位置。提供完整的细节。

然后通过我回答的以下帖子。您应该正确配置SmtpClient

How to send email in ASP.NET C#