Can’t open attached file – Xamarin.Forms App

I’m making Xamarin.Forms app.I generate a PDF with simple example with help of Syncfusion to generate PDF and convert in stream. The PDF is generated,send and is about 27KB. But when i go to my mail and open it i get We can't open this file by Adobe Reader:

//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page.
PdfPage page = doc.Pages.Add();
//Create a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
//Add values to list
List<object> data1 = new List<object>();
Object row1 = new { ID = "E01", Name = "Clay" };
Object row2 = new { ID = "E02", Name = "Thomas" };
Object row3 = new { ID = "E03", Name = "Andrew" };
Object row4 = new { ID = "E04", Name = "Paul" };
Object row5 = new { ID = "E05", Name = "Gray" };
//Add list to IEnumerable
IEnumerable<object> dataTable = data;
//Assign data source.
pdfGrid.DataSource = dataTable;
//Draw grid to the page of PDF document.
pdfGrid.Draw(page, new PointF(10, 10));
//Save the PDF document to stream.
MemoryStream stream = new MemoryStream();
//Close the document.

var message = new MailMessage();
message.From = new MailAddress("");
message.To.Add("[email protected]");
message.Subject = "Subject";
message.Body = "Body";
message.Attachments.Add(new Attachment(stream, "Test.pdf", "application/pdf"));

SmtpClient SmtpServer = new SmtpClient("");
SmtpServer.Host = "";
SmtpServer.Port = 587;
SmtpServer.EnableSsl = false;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("", "1234567");

How to find what is the problem of opening?


The MemoryStream class can be used both for writing and reading but it has only a single Position property used for both tasks. Thus, if one uses a single instance both for stream writing and reading, one usually has to reset the Position property to the start after executing the writing code to allow the reading code to retrieve all the content of the stream.

In your case that means that you should set stream.Position to 0 after doc.Save(stream).

Leave a Reply

Your email address will not be published. Required fields are marked *