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

标签 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);

最佳答案

这是启用邮件选项的本地主机的完美工作代码。您可以更改端口号。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;
    }

关于c# - 在 asp.net 中使用 smtp 和 gmail 提交表单后发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20047572/

相关文章:

c# - WaitForExit() 在 UAC 或安全警告时激活

c# - 即使鼠标指针移动到 wpf 中的另一个控件,弹出窗口也不会消失

c# - 使用时间跨度显示总持续时间

javascript - 如何在asp.net用户控件中动态附加脚本标签?

python Mechanize 表单登录给出TypeError

php - 如何操作字符串来删除引号?

c# - 如何使图像在 wpf 中的鼠标悬停事件中增长 x %?

c# - 从服务器端在 asp.net 中将内容添加到 Bootstrap 模式中的 div

javascript - Window.Location.Href = 由选定的单选按钮值设置的变量

c# - 使用 Func 和依赖注入(inject)的 Autofac 动态调用