简体   繁体   English

System.Net.Mail.SmtpException {“发送电子邮件失败。”}

[英]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.UseDefaultCredentialsfalse之前设置新的网络凭据。 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.

相关问题 System.Net.Mail.SmtpException:发送邮件失败。 无法从传输连接中读取数据 - System.Net.Mail.SmtpException: Failure sending mail. Unable to read data from the transport connection 将白名单IP gmail smtp-System.Net.Mail.SmtpException:发送邮件失败 - Whitelist IP gmail smtp - System.Net.Mail.SmtpException: Failure sending mail System.Net.Mail.SmtpException:发送邮件失败。 无法建立连接,因为目标计算机主动拒绝它127.0.0.1:25 - System.Net.Mail.SmtpException: Failure sending mail. No connection could be made because the target machine actively refused it 127.0.0.1:25 出现错误:-System.net.mail.SmtpException - Getting error:-System.net.mail.SmtpException System.Net.Mail.SmtpException:邮箱不可用 - System.Net.Mail.SmtpException: Mailbox unavailable 尝试在System.dll中使用smtp'System.Net.Mail.SmtpException'发送电子邮件时出错 - error when trying to send email using smtp 'System.Net.Mail.SmtpException' in System.dll MailMessage System.Net.Mail.SmtpException,尝试通过 C# 表单发送 email 时出错 - MailMessage System.Net.Mail.SmtpException, Error when trying to send email via C# form 如何修复 System.Net.Mail.SmtpException - How Can I Fix System.Net.Mail.SmtpException system.net.mail.smtpException:无法连接到远程服务器? - system.net.mail.smtpException :Unable to connect to the remote server? System.Net.Mail.SmtpException:该地址具有无效的主机名 - System.Net.Mail.SmtpException: The address has an invalid host name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM