Exchange WebDAV 保存到草稿不可编辑

发布于 2024-09-16 14:20:16 字数 5280 浏览 9 评论 0原文

我有以下代码将消息保存到 Exchange WEBDAV 草稿文件夹。 IT 人员会保存该邮件,但如果在 Outlook 中打开该邮件,“发送”按钮将被禁用,且该邮件为只读。请帮我找到这段代码中缺少的内容...

谢谢 布万

 strBody = "To: " + strTo + "\n" +
                "Subject: " + (string.IsNullOrEmpty(message.Subject) ? "" : message.Subject) + "\n" +
            "Date: " + System.DateTime.Now + "\n" +
            "X-Mailer: test mailer" + "\n" +
            "MIME-Version: 1.0" + "\n" +
            "Content-Type: text/html;" + "\n" +
            "Charset = \"iso-8859-1\"" + "\n" +
            "Content-Transfer-Encoding: 8bit" + "\n" +
            "\n" + (string.IsNullOrEmpty(message.HtmlBody) ? "" : message.HtmlBody.Replace("<head />","").Replace("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",""));

            // Create the HttpWebRequest object.
            PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);

            // Add the network credentials to the request.
            // Use default credentials of the service to access the server.
            PUTRequest.Credentials = cred;


            // Specify the PUT method.
            PUTRequest.Method = "PUT"; //PROPPATCH

            // Encode the body using UTF-8.
            byte[] bytes = Encoding.UTF8.GetBytes((string)strBody);

            // Set the content header length.  This must be
            // done before writing data to the request stream.
            PUTRequest.ContentLength = bytes.Length;

            // Get a reference to the request stream.
            PUTRequestStream = PUTRequest.GetRequestStream();

            // Write the message body to the request stream.
            PUTRequestStream.Write(bytes, 0, bytes.Length);

            // Close the Stream object to release the connection
            // for further use.
            PUTRequestStream.Close();

            // Set the Content-Type header to the RFC 822 message format.
            PUTRequest.ContentType = "message/rfc822";

            // PUT the message in the Drafts folder of the
            // sender's mailbox.
            PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();

            if (message.Attachments != null)
            {
                //Do the PROPPATCH
                System.Net.HttpWebRequest PROPPATCHRequest;
                System.Net.WebResponse PROPPATCHResponse;
                System.IO.Stream PROPPATCHRequestStream = null;

                string strxml = "<?xml version='1.0'?>" +
                "<d:propertyupdate xmlns:d='DAV:'>" +
                "<d:set>" +
                "<d:prop>" +
                "<isCollection xmlns='DAV:'>False</isCollection>" +
                "</d:prop>" +
                "</d:set>"
                + "<g:remove>"
                + "<g:prop><m:date/></g:prop>"
                + "</g:remove>" 
                + "</d:propertyupdate>";

                PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);
                PROPPATCHRequest.Credentials = cred;
                PROPPATCHRequest.Headers.Set("Translate", "f");
                PROPPATCHRequest.ContentType = "text/xml";
                PROPPATCHRequest.ContentLength = strxml.Length;
                PROPPATCHRequest.Method = "PROPPATCH";
                byte[] PROPPATCHbytes = Encoding.UTF8.GetBytes(strxml);
                PROPPATCHRequest.ContentLength = PROPPATCHbytes.Length;
                PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
                PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length);
                PROPPATCHRequestStream.Close();
                PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();


                foreach (KeyValuePair<string, byte[]> attachment in message.Attachments)
                {
                    System.IO.Stream PUTAttachRequestStream = null;
                    System.Net.HttpWebRequest PUTAttachRequest;
                    System.Net.WebResponse PUTAttachResponse;
                    //Attach File: This could be put in a loop to attach more than one file.
                    string FileName = attachment.Key; //@"2842498_794802035296_Label1.pdf";
                    string attachURI = tgtUri + "/" + FileName;
                    PUTAttachRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI);
                    PUTAttachRequest.Credentials = cred;
                    PUTAttachRequest.Method = "PUT";

                    PUTAttachRequest.ContentLength = attachment.Value.Length; //binaryData.Length;
                    PUTAttachRequestStream = PUTAttachRequest.GetRequestStream();
                    PUTAttachRequestStream.Write(attachment.Value, 0, attachment.Value.Length); //(binaryData, 0, binaryData.Length);
                    PUTAttachRequestStream.Close();

                    PUTAttachResponse = (System.Net.HttpWebResponse)PUTAttachRequest.GetResponse();
                    PUTAttachResponse.Close();
                }

                PROPPATCHResponse.Close();
            }

            // Clean up.
            PUTResponse.Close();

I am having the following code to Save message to Exchange WEBDAV Drafts folder. IT saves the message but If open it in Outlook, Send button is Disabled and the message is readonly. Please help me find what am i missing in this code...

thanks
Bhuvan

 strBody = "To: " + strTo + "\n" +
                "Subject: " + (string.IsNullOrEmpty(message.Subject) ? "" : message.Subject) + "\n" +
            "Date: " + System.DateTime.Now + "\n" +
            "X-Mailer: test mailer" + "\n" +
            "MIME-Version: 1.0" + "\n" +
            "Content-Type: text/html;" + "\n" +
            "Charset = \"iso-8859-1\"" + "\n" +
            "Content-Transfer-Encoding: 8bit" + "\n" +
            "\n" + (string.IsNullOrEmpty(message.HtmlBody) ? "" : message.HtmlBody.Replace("<head />","").Replace("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",""));

            // Create the HttpWebRequest object.
            PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);

            // Add the network credentials to the request.
            // Use default credentials of the service to access the server.
            PUTRequest.Credentials = cred;


            // Specify the PUT method.
            PUTRequest.Method = "PUT"; //PROPPATCH

            // Encode the body using UTF-8.
            byte[] bytes = Encoding.UTF8.GetBytes((string)strBody);

            // Set the content header length.  This must be
            // done before writing data to the request stream.
            PUTRequest.ContentLength = bytes.Length;

            // Get a reference to the request stream.
            PUTRequestStream = PUTRequest.GetRequestStream();

            // Write the message body to the request stream.
            PUTRequestStream.Write(bytes, 0, bytes.Length);

            // Close the Stream object to release the connection
            // for further use.
            PUTRequestStream.Close();

            // Set the Content-Type header to the RFC 822 message format.
            PUTRequest.ContentType = "message/rfc822";

            // PUT the message in the Drafts folder of the
            // sender's mailbox.
            PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();

            if (message.Attachments != null)
            {
                //Do the PROPPATCH
                System.Net.HttpWebRequest PROPPATCHRequest;
                System.Net.WebResponse PROPPATCHResponse;
                System.IO.Stream PROPPATCHRequestStream = null;

                string strxml = "<?xml version='1.0'?>" +
                "<d:propertyupdate xmlns:d='DAV:'>" +
                "<d:set>" +
                "<d:prop>" +
                "<isCollection xmlns='DAV:'>False</isCollection>" +
                "</d:prop>" +
                "</d:set>"
                + "<g:remove>"
                + "<g:prop><m:date/></g:prop>"
                + "</g:remove>" 
                + "</d:propertyupdate>";

                PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(tgtUri);
                PROPPATCHRequest.Credentials = cred;
                PROPPATCHRequest.Headers.Set("Translate", "f");
                PROPPATCHRequest.ContentType = "text/xml";
                PROPPATCHRequest.ContentLength = strxml.Length;
                PROPPATCHRequest.Method = "PROPPATCH";
                byte[] PROPPATCHbytes = Encoding.UTF8.GetBytes(strxml);
                PROPPATCHRequest.ContentLength = PROPPATCHbytes.Length;
                PROPPATCHRequestStream = PROPPATCHRequest.GetRequestStream();
                PROPPATCHRequestStream.Write(PROPPATCHbytes, 0, PROPPATCHbytes.Length);
                PROPPATCHRequestStream.Close();
                PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();


                foreach (KeyValuePair<string, byte[]> attachment in message.Attachments)
                {
                    System.IO.Stream PUTAttachRequestStream = null;
                    System.Net.HttpWebRequest PUTAttachRequest;
                    System.Net.WebResponse PUTAttachResponse;
                    //Attach File: This could be put in a loop to attach more than one file.
                    string FileName = attachment.Key; //@"2842498_794802035296_Label1.pdf";
                    string attachURI = tgtUri + "/" + FileName;
                    PUTAttachRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI);
                    PUTAttachRequest.Credentials = cred;
                    PUTAttachRequest.Method = "PUT";

                    PUTAttachRequest.ContentLength = attachment.Value.Length; //binaryData.Length;
                    PUTAttachRequestStream = PUTAttachRequest.GetRequestStream();
                    PUTAttachRequestStream.Write(attachment.Value, 0, attachment.Value.Length); //(binaryData, 0, binaryData.Length);
                    PUTAttachRequestStream.Close();

                    PUTAttachResponse = (System.Net.HttpWebResponse)PUTAttachRequest.GetResponse();
                    PUTAttachResponse.Close();
                }

                PROPPATCHResponse.Close();
            }

            // Clean up.
            PUTResponse.Close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2024-09-23 14:20:16

我在 msdn 上的Akash 的博客中找到了答案。人们必须仔细阅读本文才能得到实际答案,但该解决方案有效。

I found the answer in Akash's blog on msdn. One has to carefully read this to get the actual answer, but the solution worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文