I have to send PDF file as attachement, I am having an HTML file with some text in it and I am adding values from database to this file at runtime, Like this
var path = Server.MapPath("~/StaticFile/UserDetails.htm");
string readFile = System.IO.File.ReadAllText(path);
readFile.Replace("%Name%",username_from_db);
readFile.Replace("%Age%",age_from_db);
Now I have to generate PDF file and send it as attachement on runtime,I cant save the pdf file physically.This is what I tried
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(readFile);
then attaching it like this
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, "file.pdf");
but when I send it as attachement, it is not opening the file which I am getting in email. I also tried this
mailMessage.Attachments.Add(System.Net.Mail.Attachment.CreateAttachmentFromString(readFile, "application/pdf"));
mailMessage.Attachments.Last().ContentDisposition.FileName = "myFile.pdf";
I have tried this too
var byts = GetBytes(readFile);
System.IO.MemoryStream ms = new System.IO.MemoryStream(byts);
var att = new System.Net.Mail.Attachment(ms, "MyFile.pdf");
mailMessage.Attachments.Add(att);
private static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
But its not opening the pdf file after I recieve it in email.
How can I generate and send PDF on runtime?