【问题标题】:Error when sending email system.web.httpexceptions: the message could not be sent to the smtp server发送电子邮件时出错 system.web.httpexceptions:消息无法发送到 smtp 服务器
【发布时间】:2012-04-06 21:59:20
【问题描述】:

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

我不久前编写了这段代码,它过去工作正常,它按预期发送电子邮件。但是,现在我无法让它工作。 (我现在正在使用 Windows 7 64 位,如果这会有所作为的话)。

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

system.web.httpexceptions:消息无法发送到 smtp 服务器。传输错误代码为 0x80040217。服务器响应 不可用 --> system.reflection.targetinvocationexception: 调用的目标已抛出异常。 --> system.runtime.interopservice.comexception (0x80040211):消息 无法发送到 smtp 服务器。传输错误代码是 0x80040217。服务器响应不可用。

这是代码:

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","theusername@gmail.com" );
            message.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","thepassword" );

            message.From = "theusername@gmail.com";
            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";

提前谢谢你!

【问题讨论】:

标签: c# email smtp


【解决方案1】:

我遇到了同样的问题,但是当我使用未使用 Gmail 登录的两步验证的 Gmail 地址时解决了这个问题。如果您只使用密码而不进行额外验证(在我的情况下通过手机代码),它可以工作

【讨论】:

    【解决方案2】:

    也许你被谷歌屏蔽了,因为你是从远程计算机中的程序发送的(这不是你通常使用的那个) 尝试在运行该程序的同一台计算机上打开 gmail。看看您是否有安全问题。

    在 gmail 帐户上转到 https://www.google.com/settings/account 并打开安全性,看到我的 gmail 已阻止访问您的帐户

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2021-06-17
      • 2016-03-30
      • 1970-01-01
      相关资源
      最近更新 更多