简体   繁体   English

无法打开附件 - Xamarin.Forms 应用程序

[英]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. 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. PDF生成,发送,大约27KB。 But when i go to my mail and open it i get We can't open this file by Adobe Reader:但是当我将 go 发送到我的邮件并打开它时,我得到了We can't open this file

//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" };
data1.Add(row1);
data1.Add(row2);
data1.Add(row3);
data1.Add(row4);
data1.Add(row5);
//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();
doc.Save(stream);
//Close the document.
doc.Close(true);

var message = new MailMessage();
message.From = new MailAddress("admin.server.com");
message.To.Add("test@hotmail.com");
message.Subject = "Subject";
message.Body = "Body";
message.Attachments.Add(new Attachment(stream, "Test.pdf", "application/pdf"));

SmtpClient SmtpServer = new SmtpClient("mail.server.com");
SmtpServer.Host = "mail.server.com";
SmtpServer.Port = 587;
SmtpServer.EnableSsl = false;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("admin.server.com", "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. MemoryStream class 可用于写入和读取,但它只有一个Position属性用于这两个任务。 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.因此,如果一个实例同时用于 stream 写入和读取,则通常必须在执行写入代码后将Position属性重置为开始,以允许读取代码检索 ZF7B44CFAFD5C52223D7491E 的所有内容。

In your case that means that you should set stream.Position to 0 after doc.Save(stream) .在您的情况下,这意味着您应该在doc.Save(stream) stream.Position0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM