In the following code snippet i attempt to save (with rename) an attachment to an email to a disk location.
Most of the time this works correctly but occasionaly it fails in the fAttachment.Load(filename) statement and an empty file is saved to disk.
When this occurs I get this exception:-
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.0.0.1:443
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) By System On BMXS04
To get to the positionn where fAttachment.Load is being used requires that
a) I have already established a valid ExchangeService connection
b) I have sucessfully obtained a list of emails with attachments
c) The particular email I am working on has been sucessfully loaded
d) I am able to check the properties of the attachment to validate that it is of the type I want to save
so, when it comes to issue the fAttachment.Load everything should be good.
The exchange server is sitting on an internal SBS2011 box with Exchange 2010 SP3. All fully patched.
The machine (live) or development are both domain computers.
The attachments never exceed 4kb and are plain text.
Any advice would be gratefully accepted.
(P.S. First post here so I hope I did it correctly)
I have sanitised the following code follows:
public static void HandleWeborder(string mhSavePath, string mhEmail)
{
try
{
var service = GetExchangeService();
if (service != null)
{
var StartTime = DateTime.Now;
bool wasOrder;
var view = new ItemView(150);
var lookUp = mhEmail;
var searchFilter = new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true);
var results = service.FindItems(new FolderId(WellKnownFolderName.Inbox, new Mailbox(lookUp)), searchFilter, view);
if (results.TotalCount > 0)
{
foreach (EmailMessage email in results)
{
wasOrder = false;
email.Load(new PropertySet(ItemSchema.Attachments));
foreach (Microsoft.Exchange.WebServices.Data.Attachment attachment in email.Attachments)
{
FileAttachment fAttachment = attachment as FileAttachment;
var test = fAttachment.Name.Substring(attachment.Name.Length - 3);
if (test == "xtx")
{
wasOrder = true;
var newName = fAttachment.Name.Substring(0, fAttachment.Name.IndexOf(".", StringComparison.Ordinal));
if (newName.Substring(0, 5) == "Order")
{
newName = newName.Substring(6);
newName = mhSavePath + "W" + newName;
var suf = ".txt";
if (File.Exists(newName + suf)) suf = ".dup";
if (File.Exists(newName + suf + ".bak")) suf = ".dup";
newName += suf;
bool dountiltrue = false;
int attempts = 0;
while (!dountiltrue)
{
try
{
fAttachment.Load(newName);
wasOrder = true;
dountiltrue = true;
}
catch (Exception ex)
{
if (attempts < 5)
{
attempts += 1;
var innerEx = ex.InnerException.ToString();
RunParms[PosLogMsg] = "Attempt " + attempts + " - Error " + newName + " May Have timed out - " + innerEx;
RunParms[PosLogType] = LogError;
AddActivity();
}
}
}
}
}
}
}
if (wasOrder)
{
email.Delete(DeleteMode.HardDelete);
}
}
}
}
}
catch (Exception ex)
{
var innerEx = ex.InnerException.ToString();
RunParms[PosLogMsg] = "Error 264-9 - " + innerEx;
RunParms[PosLogType] = LogError;
AddActivity();
}
}
What I have tried:
I have added all these Try/Catch and manually stepped through but it does not error when stepping, only when in full run mode :-(
Note: Edited to remove all error recording as the Try/Catch handling was confusing some commenters. The Try/Catch were created simply to get a physical recording of the exception occurrence because it would take a day to step through the code to wait for the intermittent failure (the failure only occurs 3-5% of the times the code is called and always occurs at the fAttachment.Load(newName) statement