首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用EmailAttachment作为Xamarin.Essentials附加PDF?

如何使用EmailAttachment作为Xamarin.Essentials附加PDF?
EN

Stack Overflow用户
提问于 2019-08-30 01:02:02
回答 2查看 905关注 0票数 0

我正在使用James在扎马林基本要素上的本教程,介绍如何在使用IEmailService时附加文档。如何将同步PDFDocument作为电子邮件附件附加?

代码语言:javascript
复制
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)
            {

            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-30 08:13:37

我使用下面的DependencyService解决了我的问题。EmailAttachment构造函数需要您保存的文档的文件路径,而不是实际的文档。在对James的代码做了一些修改之后,我能够为我的pdf创建一个文件路径,并创建一个依赖服务来将我的pdf内容绘制到流中。

代码语言:javascript
复制
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实现

代码语言:javascript
复制
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;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2019-10-01 13:45:33

通过使用以下步骤,我们可以将同步PDF文档附加为电子邮件附件。

  • 创建PDF文档并保存到文件夹中
  • 获取PDF文件的整个路径
  • 将该PDF文档发送到电子邮件附件,使用Xamarin基本要素

我们已经创建了相同的示例,可以从下面的链接下载,

将PDF作为电子邮件附件附加的示例

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57719280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档