.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 21st, 2004, 04:50 AM
bermudabob bermudabob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 1 bermudabob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old February 1st, 2007, 04:17 PM
wimbishd wimbishd is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2007
Posts: 1 wimbishd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 29 sec
Reputation Power: 0
Rob,

Did you ever figure this out? I need to the same thing and having the same problem you had.

Diana
Quote:
Originally Posted by bermudabob
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

Reply With Quote
  #3  
Old August 9th, 2007, 12:00 PM
ckok ckok is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2007
Posts: 1 ckok User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 21 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old October 17th, 2007, 09:32 PM
mxchaos mxchaos is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 1 mxchaos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 31 sec
Reputation Power: 0
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();
}

Reply With Quote
  #5  
Old October 28th, 2007, 08:33 AM
jey jey is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 16 jey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 54 sec
Reputation Power: 0
yeah i agree with the above one

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgramming.NET Development > Saving Mail attachments problem in C#


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT