[英]I want to send mail through ASP.NET but error occurred
I am using C# for the back end of this contact form and html is the front end我在此联系表单的后端使用 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;
}
}
}
This is the error that was shown这是显示的错误
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
在 System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)在 System.Net.Mail.SmtpTransport.SendMail(MailAddress 发件人,MailAddressCollection 收件人,字符串 deliveryNotify,Boolean allowUnicode,SmtpFailedRecipientException& 异常)
at System.Net.Mail.SmtpClient.Send(MailMessage message) at index.Send_Click(Object sender, EventArgs e) in C:\\Users\\robert.crider\\source\\repos\\WebSite2\\WebSite2\\index.aspx.cs:line 37在 System.Net.Mail.SmtpClient.Send(MailMessage message) at index.Send_Click(Object sender, EventArgs e) in C:\\Users\\robert.crider\\source\\repos\\WebSite2\\WebSite2\\index.aspx.cs:line 37
be sure you use the right port for google smtp and delivery network.确保为 google smtp 和交付网络使用正确的端口。 and You already enable IMAP and/or POP3 access in the Gmail account settings.
并且您已经在 Gmail 帐户设置中启用了IMAP 和/或 POP3 访问。
SmtpClient client= new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(yourgoogleemail, yourgooglepassword),
Timeout = 3000
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.