smtp异常“发送邮件失败” [英] smtp exception “failure sending mail”

查看:238
本文介绍了smtp异常“发送邮件失败”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建控制台应用程序发送电子邮件

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)
        {

        }
    }

我正在使用我的Gmail帐户的正确凭据。

I am using correct credential of my 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.


SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1需要验证。

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

更改这段代码并添加一条额外的行。重要的是它在凭证之前。

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

或者如果您的帐户有两步验证,则需要设置应用专用密码,然后在代码中使用该密码,而不是您的密码主要帐户密码。

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.

我刚刚测试并验证了这个工作对我的两个帐户。 Hell,这是我的整个方法复制到LINQPad,以防万一有帮助(当然删除了细节)。

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).

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);
}

编辑:

使用附件:

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

    smtp.Send(message);
}

这篇关于smtp异常“发送邮件失败”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆