代码之家  ›  专栏  ›  技术社区  ›  Blerta

使用WebDAV引发的操作超时

  •  3
  • Blerta  · 技术社区  · 15 年前

    我在C中构建了一个桌面应用程序,向一组订阅用户发送电子邮件通知。我正在使用 韦德达夫 实现此功能并使用 奥瓦 (Outlook Web Access)在33封邮件之前一切正常 '操作超时' 引发异常。 我可以重新运行我的应用程序向其余的用户发送电子邮件,但是在一些电子邮件再次发生异常之后。当然,我可以在一次运行中执行此操作。 我尝试向http web request对象添加一些超时值,并在发送每个邮件后放置一些thread.spleep值,以便每个HTTP Web请求不会干扰以下请求。 但是现在我被这个超时异常所困扰,不知道如何处理它。

    一个想法是,我在OWA中的登录会话已过期,但每次发送新电子邮件时,我都会登录。

    如果你有网络DAV的经验,能给我一些帮助吗?

    编辑:将所有目标电子邮件地址放入数据表后,我将循环访问它们,并调用此方法通过WebDAV发送电子邮件

     private bool SendingEmailWithDavAttachment(string subject, string body, string destionationEmailAddress, string filePath)
            {
                try
                {
                    System.Net.HttpWebRequest PUTRequest;
                    System.Net.HttpWebRequest PUTRequest1;
                    System.Net.WebResponse PUTResponse;
                    System.Net.WebResponse PUTResponse1;
                    System.Net.HttpWebRequest PROPPATCHRequest;
                    System.Net.WebResponse PROPPATCHResponse;
                    System.Net.HttpWebRequest MOVERequest;
                    System.Net.WebResponse MOVEResponse;
                    System.Net.CredentialCache MyCredentialCache;
    
                    string strMailboxURI = "";
                    string strSubURI = "";
                    string strTempURI = "";
                    string strServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
    
                    string strPassword = ConfigurationSettings.AppSettings["EmailPassword"].ToString();
                    // "Mailbox folder where email is being sent from";
                    string strAlias = ConfigurationSettings.AppSettings["EmailUsername"].ToString();
    
                    string strTo = destionationEmailAddress;
    
                    string strSubject = subject;
                    string strBody = "";
                    byte[] bytes = null;
    
                    System.IO.Stream PUTRequestStream = null;
                    System.IO.Stream PROPPATCHRequestStream = null;
                    System.IO.Stream PUTRequestStream1 = null;
    
                    strMailboxURI = "http://" + strServer + "/exchange/" + strAlias;
                    strSubURI = "http://" + strServer + "/exchange/" + strAlias + "/##DavMailSubmissionURI##/";
                    strTempURI = "http://" + strServer + "/exchange/" + strAlias + "/drafts/" + strSubject + ".eml/";
    
                    strBody = "To: " + strTo + "\n";
                    strBody += "Subject: " + strSubject + "\n" +
                    "Date: " + System.DateTime.Now +
                    "X-Mailer: test mailer" + "\n" +
                    "MIME-Version: 1.0" + "\n" +
                    "Content-Type: text/html;" + "\n" +
                    "Charset = \"iso-8859-1\"" + "\n" +
                    "\n" + body;
    
                    MyCredentialCache = new System.Net.CredentialCache();
                    MyCredentialCache.Add(new System.Uri(strMailboxURI), "Basic", new System.Net.NetworkCredential(strAlias, strPassword));
    
                    PUTRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
    
                    PUTRequest.Credentials = MyCredentialCache;
    
                    PUTRequest.Method = "PUT";
                    bytes = Encoding.UTF8.GetBytes((string)strBody);
                    PUTRequest.ContentLength = bytes.Length;
                    PUTRequestStream = PUTRequest.GetRequestStream();
                    PUTRequestStream.Write(bytes, 0, bytes.Length);
                    PUTRequestStream.Close();
                    PUTRequest.ContentType = "message/rfc822";
                    PUTRequest.KeepAlive = true;
                    PUTResponse = (System.Net.HttpWebResponse)PUTRequest.GetResponse();
    
                    //Do the PROPPATCH 
                    string strxml = "<?xml version='1.0'?>" +
                    "<d:propertyupdate xmlns:d='DAV:'>" +
                    "<d:set>" +
                    "<d:prop>" +
                    "<isCollection xmlns='DAV:'>False</isCollection>" +
                    "</d:prop>" +
                    "</d:set>" +
                    "</d:propertyupdate>";
    
                    PROPPATCHRequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
                    PROPPATCHRequest.KeepAlive = true;
                    PROPPATCHRequest.Credentials = MyCredentialCache;
                    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();
    
                    string fileName = path.Substring(path.LastIndexOf("\\") + 1);
    
                    string attachURI = strTempURI + fileName;
                    PUTRequest1 = (System.Net.HttpWebRequest)HttpWebRequest.Create(attachURI);
                    PUTRequest1.Credentials = MyCredentialCache;
    
    
                    PUTRequest1.Method = "PUT";
                    PUTRequest1.KeepAlive = true;
                    System.IO.FileStream inFile;
                    byte[] binaryData;
                    inFile = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    binaryData = new Byte[inFile.Length];
                    long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
                    inFile.Close();
                    PUTRequest1.ContentLength = binaryData.Length;
                    PUTRequestStream1 = PUTRequest1.GetRequestStream();
                    PUTRequestStream1.Write(binaryData, 0, binaryData.Length);
                    PUTRequestStream1.Close();
                    PUTResponse1 = (System.Net.HttpWebResponse)PUTRequest1.GetResponse();
    
    
                    //Move File 
                    MOVERequest = (System.Net.HttpWebRequest)HttpWebRequest.Create(strTempURI);
                    MOVERequest.Credentials = MyCredentialCache;
                    MOVERequest.Method = "MOVE";
                    MOVERequest.Headers.Add("Destination", strSubURI);
                    MOVEResponse = (System.Net.HttpWebResponse)MOVERequest.GetResponse();
    
                    return true;
                }
                catch (Exception ex)
                {
                    //Log error occurrance 
                    DataLayer.DataLayer.WriteErrorLog("MySource", String.Format("Email sending failed.Exception: {0}",ex.Message ), DateTime.Now, destionationEmailAddress,"Failure");
                    return false;
                }
            }
    
    3 回复  |  直到 13 年前
        1
  •  3
  •   Tomas Voracek    14 年前

    用小提琴看看是怎么回事。同时检查EventViewer。

    http://forums.iis.net/p/1156153/1897467.aspx 只是总结一下我已经说过的话。

        2
  •  1
  •   plaes    14 年前

    尝试通过设置同时发送“保持活动”标题 HttpWebRequest.KeepAlive True

        3
  •  0
  •   jmozko    13 年前

    尝试调用请求关闭方法。

    对于您的代码:

    PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();
    PROPPATCHResponse.Close();  //<-- Invoke Close Method
    
    PUTResponse1 = (System.Net.HttpWebResponse)PUTRequest1.GetResponse();
    PUTResponse1.Close();       //<-- Invoke Close Method
    

    在我的例子中,它是有效的。