|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Saving Mail attachments problem in C#
Hi,
I've got a tricky one here. I'm retrieving mail attachments from an exchange server in C# using a service. At a given interval the service kicks off to look and see if there are any unread messages for a predefined mail account. If there are, then the unread mails are searched for attachments. These attachments are saved to disk I'm using a webrequest to access the attachments (the uri has been previously located), and a binaryreader to read it in. Everything works fine in debug, the attachments are saved perfectly. When running outside of debug, the attachments are being incompletely saved. I've tried flushing the buffers but that doesn't work. I'm at a bit of a dead end figureing out whats going on. Here's the code that sets up the webrequest and writes out the data: Code:
// Create a request for the file
//
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strInputFileName );
// We need to pre authenticate the request with our credentials
// or we won't be given anything
//
request.PreAuthenticate = true;
request.Credentials = MyCredentialCache;
// Get a response and a stream from it
//
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
// Create a Binary Reader for the steam we are getting
//
BinaryReader readStream = new BinaryReader(receiveStream);
// Set up an array of bytes to hold the contents of the file we
// are going to read in. Set the array to the same length as the file
//
byte[] binaryData = new byte [response.ContentLength];
// Now we finally read the file into the binary data array
//
readStream.Read(binaryData,0,(int)response.Content Length);
// Set up the output file name
//
strTempName = strInputFileName.Substring(strInputFileName.LastIn dexOf("/")+1);
strTempName = strTempName.Replace("%20"," ");
strOutputFileName = "C:\\Robs\\Dev\\MailRig\\" + strTempName;
// Open the output file
//
FileStream fs2 = new FileStream(strOutputFileName, FileMode.CreateNew, FileAccess.Write);
// Output directly to the new binary file
//
fs2.Write(binaryData, 0, binaryData.Length);
fs2.Flush();
fs2.Close();
readStream.Close();
receiveStream.Close();
Thanks, Rob |
|
#2
|
|||
|
|||
|
Rob,
Did you ever figure this out? I need to the same thing and having the same problem you had. Diana Quote:
|
|
#3
|
|||
|
|||
|
saving mail attachments in c#
hi bob,
please let me know if you found a way to succesfully save the mails. I have the same requirements. thanks CK hyderabad |
|
#4
|
|||
|
|||
|
It seems that there is something strange going on with the "read" method on the read stream
If you "manually" read the data in from the binary stream it isn't a problem as per below. // Now we finally read the file into the binary data array //readStream.Read(binaryData,0,(int)response.Content Length); for (int j=0; j<response.ContentLength; j++) { binaryData[j] = readStream.ReadByte(); } |
|
#5
|
|||
|
|||
|
yeah i agree with the above one
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > .NET Development > Saving Mail attachments problem in C# |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|