SMTP客户端不适用于Exchange服务器,但适用于SMTP服务器

本文关键字:服务器 SMTP 适用于 客户端 Exchange 不适用 | 更新日期: 2023-09-27 18:33:44

我写了一个非常基本的类来发送电子邮件。我用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 服务器的方式处理交换服务器。

请指教谢谢

SMTP客户端不适用于Exchange服务器,但适用于SMTP服务器

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

协议:SMTP/SSL

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

•说明:基于 SSL 的 SMTP。TCP 端口 465 由公共端口保留 使用 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

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

我发现了这篇关于使用交换发送电子邮件的文章。他似乎正在使用 Microsoft.Exchange.WebServices 命名空间。我没有交换可以测试。只是提出想法。

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

您可能想尝试使用 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();
    }