gpt4 book ai didi

c# - asp.net中如何使用smtp发送邮件

转载 作者:太空狗 更新时间:2023-10-29 23:25:24 27 4
gpt4 key购买 nike

i need solution for this error 

我在运行时发生了一些错误:发送电子邮件失败。SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.7.0 必须先发出 STARTTLS 命令。 i1sm8651517pbj.70

using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
#region "Send email"
protected void btnSendmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();

try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);

// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";

//Default port will be 25
smtpClient.Port = 587;

//From address will be given as a MailAddress Object
message.From = fromAddress;

// To address collection of MailAddress
message.To.Add("muthu17green@gmail.com");
message.Subject = "Feedback";

// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1@yoursite.com")
message.CC.Add("muthu17green@gmail.com");
message.CC.Add("muthu17green@gmail.com");

// You can specify Address directly as string
message.Bcc.Add(new MailAddress("muthu17green@gmail.com"));
message.Bcc.Add(new MailAddress("muthu17green@gmail.com"));

//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;

// Message body content
message.Body = txtMessage.Text;

// Send SMTP mail
smtpClient.Send(message);

lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
}
}
#endregion

#region "Reset"
protected void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
#endregion
}

最佳答案

您需要设置SmtpClient.Credentials 属性:

smtpClient.Credentials = new NetworkCredentials("yourUserName", "yourPassword");

这是用于验证以发送消息的内容。您可能还需要确保启用 SSL:

smtpClient.EnableSsl = true;

SmtpClient.Credentials Property MSDN Reference

关于c# - asp.net中如何使用smtp发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9658093/

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