【问题标题】:Send eMail in Windows Universal App在 Windows 通用应用程序中发送电子邮件
【发布时间】:2014-07-09 06:33:14
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 偷偷摸摸。我喜欢这个。
    • 不知何故我错过了附件。应该通过将“&attachment=”添加到 URI 来工作。
    • 如果您有附件,这将不起作用。如果您尝试从 UWP 应用创建新电子邮件并添加附件,则会忽略附件。
    【解决方案2】:

    Windows Phone 8.1

    您可以使用以下方式发送带有附件的电子邮件:

    var email = new EmailMessage(); 
    email.To = ...;
    email.Body = ...;
    email.Attachments.Add( ... );
    var ignore = EmailManager.ShowComposeNewEmailAsync(email);
    

    Windows 8.1

    不幸的是,在 Windows 8.1 上,无法发送带有附件的电子邮件。 ma​​ilto 协议就是你所拥有的,它并不正式支持附件。但是,您可以按以下方式添加附件:

    mailto:xxx@xxx.com?subject=xxx&body=xxx&attach=C:\path\to\file

    mailto:xxx@xxx.com?subject=xxx&body=xxx&Attachment=C:\path\to\file

    但由客户端决定是否处理附件。有关更多详细信息,请参阅此线程https://msdn.microsoft.com/en-us/library/aa767737(v=vs.85).aspx

    【讨论】:

    • 使用 EmailManager.ShowComposeNewEmailAsync 无法在 Windows 10 桌面环境中使用 Outlook 作为默认电子邮件应用程序。
    • 您只需在主 ui 线程上运行它即可启动 Outlook。将消息逻辑包装在 Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    【解决方案3】:

    要发送带有附件的电子邮件,您需要使用 EmailMessageEmailManager 类。

    1.电子邮件消息:

    EmailMessage 类定义将要发送的实际电子邮件。您可以指定收件人(收件人、抄送、BC)、主题和电子邮件正文。

    2。电子邮件管理器:

    EmailManager 类在 Windows.ApplicationModel.Email 命名空间中定义。 EmailManager 类提供了一个静态方法 ShowComposeNewEmailAsync,它接受 EmailMessage 作为参数。 ShowComposeNewEmailAsync 将使用允许用户发送电子邮件的 EmailMessage 启动撰写电子邮件屏幕。

    你可以在这里找到更多参考windows-phone-8-1-and-windows-runtime-apps-how-to-2-send-emails-with-attachment-in-wp-8-1

    【讨论】:

    • 目前这不适用于通用应用程序。因为命名空间 Windows.ApplicationModel.Email 不存在。所以无法访问EmailMessage和EmailManager这两个类。
    • EmailMessage 和 EmailManager 显然只在 Silverlight 应用程序中可用。 Windows.ApplicationModel.Email 在 Windows 应用商店应用中不存在,因此通用应用项目无法访问此命名空间。
    【解决方案4】:

    在这个页面上:https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871373.aspx

    Microsoft 提供了一个如何与您的应用程序共享的示例。下载源示例应用:http://go.microsoft.com/fwlink/p/?linkid=231511

    打开解决方案并将文件:test.txt 添加到项目根目录。

    然后打开 ShareFiles.xaml.cs 并将类替换为:

    public sealed partial class ShareText : SDKTemplate.Common.SharePage
    {
        public ShareText()
        {
            this.InitializeComponent();
            LoadList();
        }
    
        List<Windows.Storage.IStorageItem> list { get; set; }
    
        public async void LoadList()
        {
            var uri = new Uri("ms-appx:///test.txt");
            var item = await StorageFile.GetFileFromApplicationUriAsync(uri);
            list = new List<IStorageItem>();
            list.Add(item);
        }
    
    
        protected override bool GetShareContent(DataRequest request)
        {
            bool succeeded = false;
    
            string dataPackageText = TextToShare.Text;
            if (!String.IsNullOrEmpty(dataPackageText))
            {
                DataPackage requestData = request.Data;
                requestData.Properties.Title = TitleInputBox.Text;
                requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
                requestData.Properties.ContentSourceApplicationLink = ApplicationLink;
                requestData.SetText(dataPackageText);
                requestData.SetStorageItems(list);
                succeeded = true;
            }
            else
            {
                request.FailWithDisplayText("Enter the text you would like to share and try again.");
            }
            return succeeded;
        }
    
    }
    

    可能不是最好的代码,但它帮助了我:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      相关资源
      最近更新 更多