c# - 发送电子邮件时出错 system.web.httpexceptions : the message could not be sent to the smtp server

标签 c# email smtp

我的程序中有一个函数可以从 Google 帐户发送电子邮件。

我不久前编写了这段代码,它曾经工作正常,它按照预期发送电子邮件。然而现在我无法让它发挥作用。 (我现在正在使用 Windows 7 64 位,如果这会有所不同的话)。

我得到的错误(这是代码中的第一条错误消息)是:

system.web.httpexceptions: the message could not be sent to the smtp server. the transport error code was 0x80040217. the server response was not available --> system.reflection.targetinvocationexception: exception has been thrown by the target of an invocation. --> system.runtime.interopservice.comexception (0x80040211): the message could not be sent to the smtp server. the transport error code was 0x80040217. the server response was not available.

这是代码:

void sendEmail(string [] emailList, int emailLength, string fileName)
{
    int i = 0;      //variable to act as temporary index into emailList array
    try
    {
        try
        {
            MailMessage message = new MailMessage();
        //Because using google server, requires SSL
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
            message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7a666b7b7d6b7c606f636b4e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>" );
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","thepassword" );

            message.From = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5a464b5b5d4b5c404f434b6e49434f4742004d4143" rel="noreferrer noopener nofollow">[email protected]</a>";
            message.To = emailList[i];

            //add Bcc
            while (++i < emailLength)
            {
                message.Bcc = emailList[i];
            }//end while

            message.Subject = "Test Subject";
            message.Body = "Test Body";

            MailAttachment fileAttach = new MailAttachment(fileName);
            message.Attachments.Add(fileAttach);

            try
            {       
                SmtpMail.SmtpServer = "smtp.gmail.com";

提前谢谢您!

最佳答案

我遇到了同样的问题,但当我使用 Gmail 地址时没有使用 Gmail 登录的两步验证时,问题得到解决。如果您只使用密码而不进行额外的验证(在我的例子中通过手机代码),它就可以工作

关于c# - 发送电子邮件时出错 system.web.httpexceptions : the message could not be sent to the smtp server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049714/

相关文章:

c - C linux查询MX记录

c# - 如何将字典的内容写入文本文件?

c# - 使用乘法过滤器的ElasticSearch 5.1过滤查询

ios - 将 csv 附加到电子邮件 xcode

ruby-on-rails - 找不到 Rails 5 实体路径助手方法

c# - 当服务器和请求位于本地时,监听 SMTP 有效,但如果从远程服务器发出请求,则会失败

c# - 如何调试 ASP.NET Boilerplate Web API?

C# - StreamReader/StreamWriter - 另一个进程使用的文件

php - 电子邮件管道 - 工作但电子邮件返回错误

c# - 现有连接被 SMTP 客户端中的远程主机强行关闭