【问题标题】:Sending an email once a form is submitted using smtp and gmail in asp.net在 asp.net 中使用 smtp 和 gmail 提交表单后发送电子邮件
【发布时间】:2013-12-01 14:10:25
【问题描述】:

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

这里是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);

【问题讨论】:

  • 分享您发送邮件的完整代码

标签: c# asp.net forms email smtp


【解决方案1】:

这是启用了邮件选项的 localhost 的完美工作代码。您可以更改端口号。fromWho、toWho 是字符串格式的邮寄地址(即: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;
    }

【讨论】:

    【解决方案2】:

    您可以试试这个并确保您使用的是有效的登录凭据并且您有正确的互联网连接:

     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 
    
      }
    

    【讨论】:

      【解决方案3】:

      您在哪里配置了 client 对象。给出完整的细节。

      然后浏览我回答的以下帖子。你应该正确配置你的SmtpClient

      How to send email in ASP.NET C#

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-24
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        相关资源
        最近更新 更多