【发布时间】:2016-03-21 05:31:38
【问题描述】:
我正在尝试在我的网站中构建一个联系表单,我正在使用带有 C# 的 asp.net,当我提交消息时它没有到达电子邮件,是因为我使用的是本地服务器吗?或者我的代码中有错误?我在 catch 部分收到此错误消息:“您的消息发送失败,请重试。”
这是页面 C# 后面的代码
try
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("jasmine.afnan@gmail.com");
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("jasmine@gmail.com");
msg.From = address;
//Append their name in the beginning of the subject
msg.Subject = txtName.Text + " : " + ddlSubject.Text;
msg.Body = txtMessage.Text;
//Configure an SmtpClient to send the mail.
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true; //only enable this if your provider requires it
//Setup credentials to login to our sender email address ("UserName", "Password")
NetworkCredential credentials = new NetworkCredential("jasmine.afnan@gmail.com", "*");
client.Credentials = credentials;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
lblResult.Text = "Your message was sent!";
//Clear the form
txtName.Text = "";
txtMessage.Text = "";
}
catch
{
//If the message failed at some point, let the user know
lblResult.Text = "Your message failed to send, please try again.";
}
这是个例外
System.Net.Mail.SmtpException:SMTP 服务器需要安全 连接或客户端未通过身份验证。服务器响应 是:5.5.1 需要身份验证。了解更多信息 System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, 字符串响应)在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] 命令, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(邮件地址发件人, MailAddressCollection 收件人,字符串 deliveryNotify,布尔值 allowUnicode、SmtpFailedRecipientException& 异常)在 System.Net.Mail.SmtpClient.Send(MailMessage 消息)在 Default2.btnSubmit_Click(Object sender, EventArgs e) in c:\Users\looly\Documents\Visual Studio 2015\WebSites\WebSite6\Default2.aspx.cs:46 行
【问题讨论】:
-
我们想知道正确的异常,所以只需删除catch块内的标签。然后写异常..Catch(exception ex){throw ex};
-
乍一看,您的代码似乎没问题。您是否正确配置了 Gmail?它经常阻止从它认为不安全的应用程序发送。查看是否启用了 2 步,您可能需要生成应用专用密码 SEE HERE
-
发布您的确切例外情况。
-
如果异常非常多
The server response was: 5.5.1 Authentication Required.,我会得到 10 美元 -
我更新了帖子,你说得对:D