我正在使用James在扎马林基本要素上的本教程,介绍如何在使用IEmailService时附加文档。如何将同步PDFDocument作为电子邮件附件附加?
using (MyTestPdfDocument document = new MyTestPdfDocument())
{
//Save the document
using (MemoryStream pdfStream = new MemoryStream())
{
document.Save(pdfStream);
FormattableString formattedEmail = $"\n-ExWU version-\nExpress WriteUp";
try
{
var message = new EmailMessage
{
Subject = "Hello",
Body = "World",
};
//var fn = "Attachment.pdf";
var file = Path.Combine(FileSystem.CacheDirectory, document);//Close the document
message.Attachments.Add(new EmailAttachment(file));
await _emailService.ComposeAsync(message);
}
catch (FeatureNotSupportedException)
{
await PageDialogService.DisplayAlertAsync("", "Email is not supported on this device", "Ok");
}
catch (Exception ex)
{
}
}
}
}发布于 2019-08-30 08:13:37
我使用下面的DependencyService解决了我的问题。EmailAttachment构造函数需要您保存的文档的文件路径,而不是实际的文档。在对James的代码做了一些修改之后,我能够为我的pdf创建一个文件路径,并创建一个依赖服务来将我的pdf内容绘制到流中。
try
{
var message = new EmailMessage
{
Subject = "Hello",
Body = "World",
};
var fn = "attachment.pdf";
var filePath = Path.Combine(FileSystem.CacheDirectory, fn);
string folderPath = DependencyService.Get<IDirectoryService>().SavePath(pdfStream, filePath);
message.Attachments.Add(new EmailAttachment(folderPath));
await _emailService.ComposeAsync(message);
}
catch (FeatureNotSupportedException)
{
await PageDialogService.DisplayAlertAsync("", "Email is not supported on this device", "Ok");
}
catch (Exception ex)
{
}iOS实现
public class DirectoryService : IDirectoryService
{
public string SavePath(Stream inputStream, string fileName)
{
//Create a new file with the input file name in the Library folder
var m_filepath = fileName;
//Write the contents of the pdf to the newly created file
using (MemoryStream tempStream = new MemoryStream())
{
inputStream.Position = 0;
inputStream.CopyTo(tempStream);
File.WriteAllBytes(m_filepath, tempStream.ToArray());
}
return m_filepath;
}
}发布于 2019-10-01 13:45:33
通过使用以下步骤,我们可以将同步PDF文档附加为电子邮件附件。
我们已经创建了相同的示例,可以从下面的链接下载,
https://stackoverflow.com/questions/57719280
复制相似问题