【问题标题】:Use Exchange Web Service Managed API to retrieve max send mail size limit使用 Exchange Web 服务托管 API 检索最大发送邮件大小限制
【发布时间】:2021-08-06 08:15:12
【问题描述】:

我一直在寻找一些方法来使用 EWS 托管 API 来检索最大邮件大小限制以进行发送,但没有找到任何可行的解决方案...

到目前为止,我发现了这个链接:https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-get-service-configuration-information-by-using-ews-in-exchange#code-example-get-service-configuration-information-for-mail-tips-by-using-ews

这基本上检索邮件提示中的最大邮件大小。但问题是,当我尝试代码时,我收到了 500 内部服务器错误。而且似乎没有办法直接使用托管 API 本身来执行此操作。

非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: exchange-server exchangewebservices


    【解决方案1】:

    Microsoft 示例中存在一个错误,他们已将 SOAP 命名空间从 http 修改为 https。可能有些编辑认为他们在不了解 XML 模式如何工作的情况下做正确的事情。但是一个工作样本是

            private static void GetServiceConfiguration()
        {
            // XML for the GetServiceConfiguration request SOAP envelope for mail tips configuration information.
            const string getServiceConfigurationRequest =
              "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
              "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
              "               xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\"\n" +
              "               xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" \n" +
              "               xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
              "               xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
              "  <soap:Header>\n" +
              "    <t:RequestServerVersion Version=\"Exchange2013\" />\n" +
              "  </soap:Header>\n" +
              "  <soap:Body>\n" +
              "    <m:GetServiceConfiguration>\n" +
              "      <m:ActingAs>\n" +
              "        <t:EmailAddress>user@domain.com</t:EmailAddress>\n" +
              "        <t:RoutingType>SMTP</t:RoutingType>\n" +
              "      </m:ActingAs>\n" +
              "      <m:RequestedConfiguration>\n" +
              "        <m:ConfigurationName>MailTips</m:ConfigurationName>\n" +
              "      </m:RequestedConfiguration>\n" +
              "    </m:GetServiceConfiguration>\n" +
              "  </soap:Body>\n" +
              "</soap:Envelope>";
            // Encoded GetServiceConfiguration operation request.
            byte[] payload = System.Text.Encoding.UTF8.GetBytes(getServiceConfigurationRequest);
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://outlook.office365.com/ews/exchange.asmx");
                request.AllowAutoRedirect = false;
                request.Credentials = new NetworkCredential("user@domain.com", "blah");
                request.Method = "POST";
                request.ContentType = "text/xml";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
                Stream requestStream = request.GetRequestStream();
                requestStream.Write(payload, 0, payload.Length);
                requestStream.Close();
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream);
                    string responseFromServer = reader.ReadToEnd();
                    Console.WriteLine("You will need to parse this response to get the configuration information:\n\n" + responseFromServer);
                    reader.Close();
                    responseStream.Close();
                }
                else
                    throw new WebException(response.StatusDescription);
    
            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message);
            }
    

    我还添加了一个用户代理,因为如果设置了用户代理限制,这有时也会导致代码失败。但是,如果您对 Office365 使用此功能,您也应该切换到使用 OAuth。

    【讨论】:

    • 嗨,格伦,我尝试了代码,但仍然出现 500 内部服务器错误。我看不到错误的确切细节,因为显然它被某些安全策略阻止了。但是,实际的托管 EWS API 没有问题。所以我认为这可能与权限或身份验证有关……有什么想法可以解决吗?谢谢。
    • 你得到的结果是 XML 还是一些 http 错误?
    • 这是一个 http 错误。我怀疑它在 HttpRequest 中缺少一些额外的标头。我现在正在查看托管 EWS API,以了解 Http 请求的生成方式。
    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 2011-02-03
    • 2015-06-15
    • 2015-03-18
    • 2015-01-15
    • 2011-06-12
    相关资源
    最近更新 更多