简体   繁体   English

smtp异常“发送邮件失败”

[英]smtp exception “failure sending mail”

I created console application for sending Email 我创建了用于发送电子邮件的控制台

    public static void sendEmail(string email, string body)
    {
        if (String.IsNullOrEmpty(email))
            return;
        try
        {
            MailMessage mail = new MailMessage();
            mail.To.Add(email);
            mail.From = new MailAddress("test@gmail.com");
            mail.Subject = "sub";

            mail.Body = body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "admin@1234"); // ***use valid credentials***
            smtp.Port = 587;

            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }
        catch (Exception ex)
        {

        }
    }

I am using correct credential of my gmail account. 我正在使用我的Gmail帐户的正确凭据。

Is there any settings I need to do for GMail Account ? 我需要为GMail账户做什么设置吗?

It's likely that you're actually getting this error, it's just suppressed in some way by you being in a console app. 你可能实际上遇到了这个错误,它只是在你控制台应用程序中以某种方式被抑制了。

The SMTP server requires a secure connection or the client was not authenticated. SMTP服务器需要安全连接或客户端未经过身份验证。 The server response was: 5.5.1 Authentication Required. 服务器响应为:5.5.1需要身份验证。

Change this piece of code and add one extra line. 更改这段代码并添加一行。 It's important that it goes before the credentials. 它必须在凭证之前进行。

smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
// ** HERE! **
smtp.UseDefaultCredentials = false;
// ***********
smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "admin@1234"); // ***use valid credentials***
smtp.Port = 587;

You'll also need to go here and enable less secure apps. 您还需要访问此处并启用安全性较低的应用。

https://www.google.com/settings/security/lesssecureapps https://www.google.com/settings/security/lesssecureapps

Or if you have two step authentication on your account you'll need to set an app specific password, then use that password in your code instead of your main account password. 或者,如果您的帐户有两步验证,则需要设置应用专用密码,然后在代码中使用该密码而不是主帐户密码。

I've just tested and verified this works on my two step account. 我刚刚在我的两步帐户上测试并验证了这项工作。 Hell, here's my entire method copied right out of LINQPad in case it helps (with removed details of course). 好吧,这是我的整个方法,直接从LINQPad复制,以防它有帮助(当然删除了详细信息)。

var fromAddress = new MailAddress("myaccount@gmail.com", "My Name");
var toAddress = new MailAddress("test.address@email.com", "Mr Test");
const string fromPassword = "tbhagpfpcxwhkczd";
const string subject = "test";
const string body = "HEY, LISTEN!";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    Timeout = 20000
};

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

Edit: 编辑:

Using attachments: 使用附件:

using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    Attachment attachment = new Attachment(filePath);
    message.Attachments.Add(attachment);

    smtp.Send(message);
}

In my experienced using C# SMTP Client, there is a certain steps to follow in the code (see code snippet below) 在我使用C#SMTP客户端的经验中,代码中有一些步骤(请参阅下面的代码片段)

smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("account@gmail.com", "secretPassword");
smtpClient.Send(mail);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM