本文介绍了发送邮件在Windows通用应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to send an email message in a Windows Universal App for Windows 8.1 and Windows Phone 8.1?

await Launcher.LaunchUriAsync(new Uri("mailto:[email protected]?subject=MySubject&body=MyContent"));



有了这个代码行我可以发送电子邮件,但我想发送带有附件。

With this code line I can send an email, but I want to send a message with attachment.

推荐答案

由于微软错过了EmailMessage和EmailManager添加到它好像只有两个解决方案不能令人满意Windows应用商店应用程序库:您可以使用共享或电子邮件开始通过mailto协议发送。这是我如何做的:

Since Microsoft missed to add EmailMessage and EmailManager to the Windows Store Apps libraries it seems as if there are only two unsatisfactory solutions: You could use sharing or initiate email sending via the mailto protocol. Here is how I did it:

  /// <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);
  }

这篇关于发送邮件在Windows通用应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:18