我用这个发送电子邮件,但是当我提交时,它显示“第41行:smtp.Send(msg);”错误。我不知道这有什么问题。谢谢你的帮助!
public partial class Contact : Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("xxxxx@gmail.com");
msg.To.Add("xxxxx@hotmail.com");
msg.Subject = txtSubject.Text;
msg.Body = txtFirstName.Text + " " + txtLastName.Text + "'s phone number is: " + txtPhone.Text + ". <br />" + "Email Address is: " + txtEmail.Text + "Message: <br />" + txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "xxxxx@gmail.com";
NetworkCred.Password = "xxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(msg);
lblMessage.Text = "Email has been successfully sent!";
}
}
Stack Trace:
[SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at]
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) +1844406
System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) +46
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) +88
System.Net.Mail.SmtpClient.Send(MailMessage message) +1856
XinNing_Web.Contact.btnSubmit_Click(Object sender, EventArgs e) in E:\Projects\ASP.NET\XinNing_Web\XinNing_Web\Contact.aspx.cs:41
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9767618
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1738
答案 0 :(得分:0)
在您尝试发送邮件的Gmail帐户中允许不太安全的应用为true。
转到我的帐户 - &gt;登录&amp;安全 - &gt;允许安全性较低的应用(位于页面底部)。
由于此错误发生了一段时间。