System.Net.Mail.SmtpException {"Failure Sending Email."}
System.Net.Mail.SmtpException {"Failure Sending Email."}
在 client.Send(email);
处不断崩溃并出现上述错误。四重检查一切。
这是我的代码:
private void submit_Click(object sender, RoutedEventArgs e)
{
string from = "************@gmail.com";
string to = "*******@sru.edu";
string subject = "PSLM Test";
string body = "PSLM Test";
string server = "smtp.gmail.com";
int port = 465;
string username = "************";
string password = "*******";
SmtpClient client = new SmtpClient(server, port);
client.Credentials = new NetworkCredential(username, password);
MailMessage email = new MailMessage(from, to, subject, body);
email.Attachments.Add(new Attachment(GlobalVariables.attachedFilePath));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[0]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[1]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[2]));
client.Send(email);
}
请问我做错了什么?
Gmail SMTP 端口是 587,不是 465。您还需要将 SmtpClient.EnableSsl
属性 设置为 true
。
client.EnableSsl = true;
在设置新的网络凭据之前,可能 您可能需要将 client.UseDefaultCredentials
设置为 false
。但情况并非总是如此——我所知道的是,当其他选择都用尽时,这往往会奏效。
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(username, password);
由于您使用的是 gmail,因此您需要在 google 帐户安全设置中允许安全性较低的应用程序。如果您使用双因素身份验证,您还需要创建一个应用程序专用密码,并在您的代码中使用它。
SmtpClient client= new SmtpClient(server, port);
client.Credentials = new System.Net.NetworkCredential(username, password);
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
MailMessage email = new MailMessage(from, to, subject, body);
email.Attachments.Add(new Attachment(GlobalVariables.attachedFilePath));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[0]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[1]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[2]));
client.Send(email);
在 client.Send(email);
处不断崩溃并出现上述错误。四重检查一切。
这是我的代码:
private void submit_Click(object sender, RoutedEventArgs e)
{
string from = "************@gmail.com";
string to = "*******@sru.edu";
string subject = "PSLM Test";
string body = "PSLM Test";
string server = "smtp.gmail.com";
int port = 465;
string username = "************";
string password = "*******";
SmtpClient client = new SmtpClient(server, port);
client.Credentials = new NetworkCredential(username, password);
MailMessage email = new MailMessage(from, to, subject, body);
email.Attachments.Add(new Attachment(GlobalVariables.attachedFilePath));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[0]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[1]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[2]));
client.Send(email);
}
请问我做错了什么?
Gmail SMTP 端口是 587,不是 465。您还需要将 SmtpClient.EnableSsl
属性 设置为 true
。
client.EnableSsl = true;
在设置新的网络凭据之前,可能 您可能需要将 client.UseDefaultCredentials
设置为 false
。但情况并非总是如此——我所知道的是,当其他选择都用尽时,这往往会奏效。
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(username, password);
由于您使用的是 gmail,因此您需要在 google 帐户安全设置中允许安全性较低的应用程序。如果您使用双因素身份验证,您还需要创建一个应用程序专用密码,并在您的代码中使用它。
SmtpClient client= new SmtpClient(server, port);
client.Credentials = new System.Net.NetworkCredential(username, password);
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
MailMessage email = new MailMessage(from, to, subject, body);
email.Attachments.Add(new Attachment(GlobalVariables.attachedFilePath));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[0]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[1]));
email.Attachments.Add(new Attachment(GlobalVariables.formsAndTemplatesPath[2]));
client.Send(email);