gpt4 book ai didi

asp.net - 使用 ASP .Net 和 Gmail SMTP 发送电子邮件

转载 作者:行者123 更新时间:2023-12-05 06:46:31 30 4
gpt4 key购买 nike

我正在开发一个 ASP .Net 网站。
我的一个 ASPX 页面包含一个按钮。
单击此按钮后,我想使用 Gmail SMTP 服务器发送电子邮件。
我想使用我的 Gmail 帐户作为凭据。

这是按钮的点击事件处理程序:

protected void m_myButton_Click(object p_sender, EventArgs p_args)
{
string l_subject = "My subject";

StringBuilder l_builder = new StringBuilder();
// ...
string l_body = l_builder.ToString();

string l_host = ConfigurationManager.AppSettings["SmtpHost"];
int l_port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);

NameValueCollection l_smtpAccount = (NameValueCollection)ConfigurationManager.GetSection("smtpAccount");
string l_address = l_smtpAccount["SmtpAddress"];
string l_password = l_smtpAccount["SmtpPassword"];

MailMessage l_mail = new MailMessage();
l_mail.From = new MailAddress(l_address);
l_mail.To.Add(l_address);
l_mail.Subject = l_subject;
l_mail.Body = l_body;

SmtpClient l_smtp = new SmtpClient();
l_smtp.Host = l_host; // l_host = "smtp.gmail.com"
l_smtp.Port = l_port; // l_port = 587
l_smtp.EnableSsl = true;
l_smtp.UseDefaultCredentials = false;
l_smtp.Credentials = new NetworkCredential(l_address, l_password);

l_smtp.Send(l_mail);
}

命令行 l_smtp.Send(l_mail) 失败。
出现 SMTP 异常并且 InnerException 属性通知我(从法语翻译成英语):没有建立连接,因为目标计算机已拒绝它 173.194.67.108:587

我的代码基于此页面上的代码:http://www.codeproject.com/Questions/254743/sending-email-in-asp-net-using-smtp-gmail-server

任何帮助将不胜感激

最佳答案

试试这个...

 protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("Email ID where email is to be send");
mail.To.Add("Another Email ID where you wanna send same email");
mail.From = new MailAddress("YourGmailID@gmail.com");
mail.Subject = "Email using Gmail";

string Body = "Hi, this mail is to test sending mail"+
"using Gmail in ASP.NET";
mail.Body = Body;

mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Send(mail);
}

关于asp.net - 使用 ASP .Net 和 Gmail SMTP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14954722/

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