Hi all,
I'm facing a problem with saving attachment content stream to a file, the problem is that the saved files contains leading bytes, specifically in my case three question marks ??? .
The problem seems simple but i'm not sure what is behind it , is it BOM (Byte Order Mark) written to the file or is it prefix length of the stream ?
I've tried several ways , but with all had the same results:
Here is my used methods:
First one:
public void saveAttachment(System.Net.Mail.Attachment attachment)
{
StreamReader reader = new StreamReader(attachment.ContentStream);
File.WriteAllText(attachment.Name, reader.ReadToEnd());
}
Second one (similar to the first):
public void saveAttachment(System.Net.Mail.Attachment attachment)
{
StreamWriter writer = new StreamWriter(attachment.Name);
StreamReader reader = new StreamReader(attachment.ContentStream);
writer.Write(reader.ReadToEnd());
writer.Close();
}
Third method based on the
link :
public void saveMailAttachment(System.Net.Mail.Attachment attachment)
{
byte[] allBytes = new byte[attachment.ContentStream.Length];
int bytesRead = attachment.ContentStream.Read(allBytes, 0, (int)attachment.ContentStream.Length);
string destinationFile = attachment.Name;
BinaryWriter writer = new BinaryWriter(new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
writer.Write(allBytes);
writer.Close();
}
I tried playing with the bytes array , but i don't think it's a very reliable way to solve the problem , any help would be very appreciated.
Thanks in advance.