Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am have on table which stores uploaded file with guid and file also saved with guid name.
I want to send this file as attachment. When I send file as it is in attachment with guid name then it is working fine.(below code works great)

C#
Attachment attachResource = new Attachment(Server.MapPath("/public/_pdf/" + strFILEGUID + ".pdf"));
mail.Attachments.Add(attachResource);


But i want to send this file with some different name so i used below code.

C#
System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = File.OpenRead(Server.MapPath("/public/_pdf/" + strFILEGUID  + ".pdf"));
str.CopyTo(data);
data.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
Attachment attachResource = new Attachment(data, "ChangedFileName" + ".pdf");
mail.Attachments.Add(attachResource);


This code send file with name 'ChangedFileName.pdf' but while opening file it says file is damaged. I am not getting the problem here. Any help will be appreciated.
Thanks
Posted

1 solution

Use BinaryReader to read the PDF File
System.IO.BinaryReader bw = new BinaryReader(str);

byte[] buf = bw.ReadBytes((int)str.Length);

Thanks
--RA
 
Share this answer
 
Comments
pradiprenushe 20-Jun-13 7:37am    
Thank you very much. It works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900