我在C#中构建一个桌面应用程序,以向一组订阅用户发送电子邮件通知。我正在使用 Webdav 来实现此功能并使用 OWA (Outlook网络访问),一切正常,直到33邮件然后'操作超时'抛出异常。 我可以重新运行我的应用程序以向其余用户发送电子邮件,但在一些电子邮件再次发生异常后再次发送。当然,我想在一次运行中执行此操作。 我已经尝试向我的HttpWebRequest对象添加一些超时值,并在发送每个邮件后也放入一些Thread.Spleep值,以便每个http Web请求不会干扰以下请求。 但是现在我遇到了这个超时异常并且不知道如何处理它。
一个想法是我在owa中的登录会话已过期,但每次发送新电子邮件时我都会登录。
如果您有使用Web 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;
}
}
答案 0 :(得分:3)
使用Fiddler查看正在发生的事情。还要检查EventViewer。
http://forums.iis.net/p/1156153/1897467.aspx只是在总结我已经说过的内容。
答案 1 :(得分:1)
通过将HttpWebRequest.KeepAlive
设置为True
答案 2 :(得分:0)
尝试调用Request Close方法。
代码:
PROPPATCHResponse = (System.Net.HttpWebResponse)PROPPATCHRequest.GetResponse();
PROPPATCHResponse.Close(); //<-- Invoke Close Method
PUTResponse1 = (System.Net.HttpWebResponse)PUTRequest1.GetResponse();
PUTResponse1.Close(); //<-- Invoke Close Method
就我而言,它有效。