【发布时间】:2019-05-27 17:24:57
【问题描述】:
我在此联系表单的后端使用 C#,而 html 是前端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class index : System.Web.UI.Page
{
protected void Send_Click(object sender, EventArgs e)
{
try
{
MailMessage message = new MailMessage(From.Text, To.Text, Subject.Text, Body.Text);
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
client.EnableSsl = true;
// The credentials when I ran the code were correct.
client.Credentials = new System.Net.NetworkCredential("example@gmail.com","password");
client.Send(message);
status.Text = "Mail was sent successfully";
status.Text = "Send was clicked";
}
// This catch block is so that you can see what error
// occurs if there is an error
catch(Exception ex)
{
status.Text = ex.StackTrace;
}
}
}
这是显示的错误
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
在 System.Net.Mail.SmtpTransport.SendMail(MailAddress 发件人,MailAddressCollection 收件人,字符串 deliveryNotify,布尔 allowUnicode,SmtpFailedRecipientException& 异常)
在 System.Net.Mail.SmtpClient.Send(MailMessage message) 在 index.Send_Click(Object sender, EventArgs e) 在 C:\Users\robert.crider\source\repos\WebSite2\WebSite2\index.aspx.cs:line 37
【问题讨论】:
-
这是 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) 在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] 命令中显示的错误, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at index.Send_Click (Object sender, EventArgs e) 在 C:\Users\robert.crider\source\repos\WebSite2\WebSite2\index.aspx.cs:line 37
-
能否显示 ex.Message 或 ex.InnerMessage 以获得更多错误详情?
-
我没有 ex.message 或 ex.innermessage?
-
你的错误捕获 "catch(Exception ex)" 它应该有 ex.Message 或 ex.InnerException.Message 。就像你的显示堆栈跟踪 ex.StackTrace
-
请使用问题下方的edit 链接添加任何附加细节,例如错误消息,而不是在 cmets 中。
标签: c# asp.net visual-studio smtp