I build a desktop application in C# to send an email notification to a set of subscribed users. I am using Webdav to implement this function and using OWA (Outlook web access) and everything works fine till the 33 mail then 'Operation Timeout' exception is thrown. I can rerun my application to send emails to the remaining users but again after some emails again exception occurrs. Of course i could like to perform this operation in one run. I've tried adding some timeout value to my HttpWebRequest object and also put some Thread.Spleep value after sending each mail so that each http web request doesn't interfere with the following request. But now i am stuck with this timeout exception and no idea how to handle it.
One idea would be that my logged in session in owa expired but i am loggin in each time a send a new email.
Please can you provide me some help if you experienced with Web dav.
Edit: After putting in datatable all destination email addresses i am looping throught them and calling this method to send emails by 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;
}
}