0
我用這個發送電子郵件,但是當我提交時,它顯示我「Line 41:smtp.Send(msg);」錯誤。我不知道這有什麼問題。謝謝您的幫助!SmtpClient.Send(msg);錯誤
public partial class Contact : Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("[email protected]");
msg.To.Add("[email protected]");
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 = "[email protected]";
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
請檢查您的雙重身份驗證設置,通常在使用Gmail提供程序從不同IP地址發送機器時發生此問題。您可以嘗試在當前計算機上登錄Gmail並確認您帳戶的身份驗證,請訪問https://support.google.com/accounts/answer/1144110?hl=zh-CN –
非常感謝。我知道了! –