[英]System.Net.Mail.SmtpException {“Failure Sending Email.”}
Keeps breaking and crashing at client.Send(email);
不断在客户client.Send(email);
崩溃和崩溃。 client.Send(email);
with the error above. 与上面的错误。 Quadruple checked everything. 四重检查了一切。
Here's my code: 这是我的代码:
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);
}
What am I doing wrong, please? 请问我做错了什么?
Gmail SMTP port is 587, not 465. You also need to set the SmtpClient.EnableSsl
property to true
. Gmail SMTP端口是587,而不是465。您还需要将SmtpClient.EnableSsl
属性设置为true
。
client.EnableSsl = true;
It is possible that you might need to set client.UseDefaultCredentials
to false
prior to setting the new network credentials. 这是可能的,你可能需要设置client.UseDefaultCredentials
到false
之前设置新的网络凭据。 This is not always the case though - all I know is that when other options have been exhausted, this tends to work. 不过,情况并非总是如此-我所知道的是,当其他选择都用尽时,这往往会奏效。
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(username, password);
Since you are using gmail, you will need to allow less secure applications in your google account security settings. 由于您使用的是Gmail,因此您需要在Google帐户安全设置中允许使用不太安全的应用程序。 If you are using 2-factor authentication you will need to create an application-specific password as well, and use that in your code. 如果您正在使用2要素身份验证,则还需要创建一个应用程序专用密码,并在代码中使用该密码。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.