gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:32 25 4
gpt4 key购买 nike

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

这是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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com