【问题标题】:smtpclient not working for exchange server but works for smtp serversmtpclient 不适用于 Exchange 服务器,但适用于 smtp 服务器
【发布时间】:2012-05-10 18:20:51
【问题描述】:

我写了一个非常基础的类来发送电子邮件。我用 smtp 服务器对其进行了测试,它工作正常,但是当我尝试使用我公司的交换服务器时,它给出了这个异常:

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

我的代码如下:

MailMessage mailMessage = new MailMessage("From@company.com", "To@company.com", "Test Subject", "Void body");
SmtpClient smtpClient = new SmtpClient(smtpServerAddress, smtpServerPort);
NetworkCredential credentials = new NetworkCredential(AccountName,Password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; // without this I get: The remote certificate is invalid according to the validation procedure.
smtpClient.Send(mailMessage);

我需要以不同于 smtp 服务器的方式处理交换服务器吗?

请指教 谢谢

【问题讨论】:

  • 最有可能的是,Exchange 服务器需要进行一些配置才能让您使用它。有很多事情可能会导致这样的错误。 (未启用 SMTP 直通,有关允许哪些服务器/用户使用直通的配置规则等)。这可能更适合 ServerFault.com。谷歌搜索错误提供了通过更改 Exchange 服务器的配置来解决此问题的说明。 smtp25.blogspot.com/2009/04/…
  • smtpServerPort 的值是多少?
  • @DavidStratton 但同样的交换服务器也适用于 Outlook。
  • Outlook 默认使用 MAPI,而不是 SMTP。 it.med.miami.edu/x1111.xml 两种不同的协议,例如 http 与 ftp。运行 IIS 的 Web 服务器可以启用 http 并禁用 ftp。同样,Exchange Server 可以启用 MAPI 并禁用 SMTP。此外,SMPT 通常被锁定,因此只有特定的、特别允许的用户和机器才能将其用作直通服务器,以防止恶意程序使用服务器发送垃圾邮件。每个的设置都是不同的。设置使用 MAPI 的权限并不意味着同样的权限适用于 SMTP。

标签: c# .net smtp


【解决方案1】:

您使用的端口号是否正确?

协议:SMTP/SSL

•端口(TCP/UDP):465(TCP)

•描述:基于 SSL 的 SMTP。 TCP 端口 465 由 common 保留 使用 SSL 进行安全 SMTP 通信的行业实践 协议。但是,与 IMAP4、POP3、NNTP 和 HTTP 不同的是,SMTP 在 Exchange 2000 不使用单独的端口进行安全通信 (SSL),而是采用称为“带内安全子系统”的 传输层安全性 (TLS)。启用 TLS 在 Exchange 上工作 2000,您必须在 Exchange 2000 上安装计算机证书 服务器。

从这里撕下来:http://www.petri.co.il/ports_used_by_exchange.htm

【讨论】:

  • 但是我可以通过 Outlook 使用该交换服务器。有什么想法吗?
  • 获取客户端代理(如 Fiddler)或 WireShark 并捕获您的盒子和交换服务器之间的通信。这将告诉您在对话期间哪些端口正在使用。
【解决方案2】:

我刚遇到这个问题。在您的 Exchange 服务器上,进入 Exchange 系统管理器并打开服务器的对象树,然后打开协议,然后右键单击默认 SMTP 虚拟服务器并选择属性。单击访问选项卡,然后单击身份验证按钮。最后单击用户并确保您使用的用户有权访问 SMTP 中继。此外,如果您还没有这样做,请选择 SMTP 下的中继按钮,并确保运行该应用程序的机器没有被列入黑名单,或者已被授予权限。

【讨论】:

    【解决方案3】:

    我发现这篇关于使用 Exchange 发送电子邮件的帖子。看来他正在使用 Microsoft.Exchange.WebServices 命名空间。我没有交换来测试。只是提出想法。

    http://waqarsherbhatti.posterous.com/sending-email-using-exchange-server-2010-ews

    【讨论】:

      【解决方案4】:

      您可能想尝试使用 WebDAV 发送。如果您想尝试一下,这是我的方法..

          public static void ViaWebDav(String sMailbox, String sExchange, String sTo, String sCc, String sSubject, String sBody, params String[] sAttachments)
          {
              HttpWebRequest hwrOut;
              HttpWebResponse hwrIn;
      
              //String strServer = "SXGM-202.xxx.com";
              //string strPassword = "123";
              //string strUserID = "u";
              //string strDomain = "fg";
      
      
              Byte[] b = null;
              Stream s = null;
      
      
      
              String sMailboxUrl = "http://" + sExchange + "/exchange/" + sMailbox;
      
              String sMailboxSend = sMailboxUrl + "/##DavMailSubmissionURI##/";
      
              String sMailboxTemp = sMailboxUrl + "/drafts/" + sSubject + ".eml";
      
              // Construct the RFC 822 formatted body of the PUT request.
              // Note: If the From: header is included here,
              // the MOVE method request will return a
              // 403 (Forbidden) status. The From address will
              // be generated by the Exchange server.
              StringBuilder sb = new StringBuilder();
              sb.AppendLine("To: " + sTo);
              if (!sCc.IsEmpty()) sb.AppendLine("Cc: " + sCc);
              sb.AppendLine("Subject: " + sSubject);
              sb.AppendLine("Date: " + System.DateTime.Now);
              sb.AppendLine("X-Mailer: AML FIU;");
              sb.AppendLine("MIME-Version: 1.0;");
              sb.AppendLine("Content-Type: text/plain;");
              sb.AppendLine("Charset = \"iso-8859-1\"");
              sb.AppendLine("Content-Transfer-Encoding: 7bit;");
              sb.AppendLine();
              sb.AppendLine(sBody);
      
              // Create a new CredentialCache object and fill it with the network
              // credentials required to access the server.
              //MyCredentialCache = new CredentialCache();
              //MyCredentialCache.Add(new System.Uri(strMailboxURI),
              //    "NTLM",
              //    new NetworkCredential(strUserID, strPassword, strDomain)
              //   );
      
              // Create the HttpWebRequest object.
              hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
              hwrOut.Credentials = CredentialCache.DefaultCredentials;
              hwrOut.Method = "PUT";
              hwrOut.ContentType = "message/rfc822";
      
              // Encode the body using UTF-8.
              b = Encoding.UTF8.GetBytes(sb.ToString());
      
              hwrOut.ContentLength = b.Length;
      
      
              s = hwrOut.GetRequestStream();
              s.Write(b, 0, b.Length);
              s.Close();
      
              // PUT the message in the Drafts folder of the mailbox.
              hwrIn = (HttpWebResponse)hwrOut.GetResponse();
      
      
              #region //ATTACHMENTS
              //Do the PROPPATCH
              sb = new StringBuilder();
              sb.Append("<?xml version='1.0'?>");
              sb.Append("<d:propertyupdate xmlns:d='DAV:'>");
              sb.Append("<d:set>");
              sb.Append("<d:prop>");
              sb.Append("<isCollection xmlns='DAV:'>False</isCollection>");
              sb.Append("</d:prop>");
              sb.Append("</d:set>");
              sb.Append("</d:propertyupdate>");
      
              foreach (String sAttach in sAttachments)
              {
                  hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
                  hwrOut.Credentials = CredentialCache.DefaultCredentials;
                  hwrOut.Method = "PROPPATCH";
                  hwrOut.ContentType = "text/xml";
                  hwrOut.Headers.Set("Translate", "f");
      
                  b = Encoding.UTF8.GetBytes(sb.ToString());
      
                  hwrOut.ContentLength = b.Length;
      
                  s = hwrOut.GetRequestStream();
                  s.Write(b, 0, b.Length);
                  s.Close();
      
                  hwrIn = (HttpWebResponse)hwrOut.GetResponse();
      
      
                  hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp + "/" + Path.GetFileName(sAttach));
                  hwrOut.Credentials = CredentialCache.DefaultCredentials;
                  hwrOut.Method = "PUT";
      
                  using (FileStream fs = new FileStream(sAttach, FileMode.Open, FileAccess.Read))
                  {
                      b = new Byte[fs.Length];
      
                      fs.Read(b, 0, (Int32)fs.Length);
                  }
      
                  hwrOut.ContentLength = b.Length;
      
                  s = hwrOut.GetRequestStream();
                  s.Write(b, 0, b.Length);
                  s.Close();
      
                  hwrIn = (HttpWebResponse)hwrOut.GetResponse();
              }
              #endregion
      
      
      
              // Create the HttpWebRequest object.
              hwrOut = (HttpWebRequest)HttpWebRequest.Create(sMailboxTemp);
              hwrOut.Credentials = CredentialCache.DefaultCredentials;
              hwrOut.Method = "MOVE";
              hwrOut.Headers.Add("Destination", sMailboxSend);
      
              hwrIn = (HttpWebResponse)hwrOut.GetResponse();
      
              // Clean up.
              hwrIn.Close();
          }
      

      【讨论】:

        猜你喜欢
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-03
        • 2012-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多