c# - 在 Windows 通用应用程序中发送电子邮件

标签 c# windows windows-phone-8 win-universal-app

是否可以在 Windows 8.1 和 Windows Phone 8.1 的 Windows 通用应用程序中发送电子邮件?

await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));

使用此代码行我可以发送电子邮件,但我想发送带附件的消息。

最佳答案

由于 Microsoft 未能将 EmailMessage 和 EmailManager 添加到 Windows 应用商店应用程序库中,因此似乎只有两个不令人满意的解决方案:您可以使用共享或通过 mailto 协议(protocol)启动电子邮件发送。这是我的做法:

  /// <summary>
  /// Initiates sending an e-mail over the default e-mail application by 
  /// opening a mailto URL with the given data.
  /// </summary>
  public static async void SendEmailOverMailTo(string recipient, string cc, 
     string bcc, string subject, string body)
  {
     if (String.IsNullOrEmpty(recipient))
     {
        throw new ArgumentException("recipient must not be null or emtpy");
     }
     if (String.IsNullOrEmpty(subject))
     {
        throw new ArgumentException("subject must not be null or emtpy");
     }
     if (String.IsNullOrEmpty(body))
     {
        throw new ArgumentException("body must not be null or emtpy");
     }

     // Encode subject and body of the email so that it at least largely 
     // corresponds to the mailto protocol (that expects a percent encoding 
     // for certain special characters)
     string encodedSubject = WebUtility.UrlEncode(subject).Replace("+", " ");
     string encodedBody = WebUtility.UrlEncode(body).Replace("+", " ");

     // Create a mailto URI
     Uri mailtoUri = new Uri("mailto:" + recipient + "?subject=" +
        encodedSubject +
        (String.IsNullOrEmpty(cc) == false ? "&cc=" + cc : null) +
        (String.IsNullOrEmpty(bcc) == false ? "&bcc=" + bcc : null) +
        "&body=" + encodedBody);

     // Execute the default application for the mailto protocol
     await Launcher.LaunchUriAsync(mailtoUri);
  }

关于c# - 在 Windows 通用应用程序中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646815/

相关文章:

audio - 将WP8应用最小化后,背景音频将停止,然后恢复

c++ - 如何解决 OpenCV 3 和 C++ 中的 GpuAcc_64.dll 异常?

XAMPP 运行速度太慢 100 倍的 Windows 上的 PHP

windows - Windows 服务可以接收 Windows 消息吗?

c# - 为什么某些字体文件不能在 Windows Phone 8 上运行?

c# - 在 x64 中从 C# 调用 C++ 代码,所有参数都移动 1

c# - 在不同于二进制文件的位置访问 App.config

c# - 接收 eBay SOAP 通知消息

c# - Enumerator.Current 为空但集合有数据。我究竟做错了什么

c# - 如何检查用户是否在 WP8 设备上安装了特定的应用程序