• M3: 发送邮件附件(2)


    本小节介绍如何通过邮件将生成的贺卡发送给朋友。使用到了EmailMessageAPI, 需要引入的命名空间为Windows.ApplicationModel.Email

    请确保完成了以前的章节。

    MainPage.xaml.cs页面,在SendMail_Click方法中,新定义EmailMessage的对象,将前一节中生成的Wishes.jpg文件作为邮件附件发送给其他人。 添加如下代码:

    EmailMessage emailMessage = new EmailMessage();
    emailMessage.Body = "Thanksgiving Card from your friend, this is generated by Card App.";
    var stream1 = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(file);
    var attachment = new Windows.ApplicationModel.Email.EmailAttachment(file.Name, stream1);
    
    emailMessage.Attachments.Add(attachment);
    await EmailManager.ShowComposeNewEmailAsync(emailMessage);
    

    修改后, SendMail_Click方法的完整代码如下:

    private async void SendMail_Click(object sender, RoutedEventArgs e)
    {
        RenderTargetBitmap renderTrgBitmap = new RenderTargetBitmap();
        await renderTrgBitmap.RenderAsync(gridMsg);
    
        var pixelBuffer = await renderTrgBitmap.GetPixelsAsync();
        var file = await KnownFolders.PicturesLibrary.CreateFileAsync("Wishes.jpg", CreationCollisionOption.ReplaceExisting);
    
        using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
            encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Straight,
                (uint)renderTrgBitmap.PixelWidth,
                (uint)renderTrgBitmap.PixelHeight,
                96d, 96d,
                pixelBuffer.ToArray());
    
            await encoder.FlushAsync();
        }
    
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.Body = "Thanksgiving Card from your friend, this is generated by Card App.";
        var stream1 = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(file);
        var attachment = new Windows.ApplicationModel.Email.EmailAttachment(file.Name, stream1);
    
        emailMessage.Attachments.Add(attachment);
        await EmailManager.ShowComposeNewEmailAsync(emailMessage);
    }
    

    运行程序,单击Get a Wishes按钮,再单击Send to Friend按钮。App将打开邮件程序,新建一个邮件,并将Wishes.jpg添加为邮件附件。

  • 相关阅读:
    《构建之法》1.2.3章读后感
    0302思考并回答一些问题
    1231 实验四 递归下降语法分析程序设计
    1211 有穷自动机的构造与识别
    1112-评论
    C语言文法定义及C程序的推导过程
    词法分析
    0909对编译原理的理解
    团队合作2.0
    "数学口袋精灵"bug的发现及单元测试
  • 原文地址:https://www.cnblogs.com/qixue/p/4992245.html
Copyright © 2020-2023  润新知