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

5
在 Windows 8.1 和 Windows Phone 8.1 的通用应用程序中,是否可以发送电子邮件?
await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));

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

4个回答

5

由于微软没有将EmailMessage和EmailManager添加到Windows Store应用程序库中,似乎只有两个不太令人满意的解决方案:您可以使用共享或通过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);
  }

狡猾。我喜欢这个。 - Algorath
不知怎么的,我错过了附件。应该通过在URI中添加“&attachment=<filepath>”来解决。 - Jürgen Bayer
如果您尝试从UWP应用程序创建新电子邮件并添加附件,则无法正常工作,因为附件将被忽略。 - pixel

1

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上,没有办法发送带附件的电子邮件。您只能使用mailto协议,但它不支持正式的附件。不过,您可以按照以下方式添加附件:

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

or

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


1
在 Windows 10 桌面环境中使用 Outlook 作为默认电子邮件应用程序,使用 EmailManager.ShowComposeNewEmailAsync 将无法正常工作。 - pixel
你只需要在主 UI 线程上运行它,就可以启动 Outlook。将消息逻辑包装在 Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 中。 - michael g

0
在此页面上:https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh871373.aspx 微软提供了一个示例,说明如何与您的应用程序共享。下载源示例应用程序: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;
    }

}

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


0

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

1. EmailMessage:

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

2. EmailManager:

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


2
目前这个通用应用程序无法正常工作。因为命名空间Windows.ApplicationModel.Email不存在。所以无法访问EmailMessage和EmailManager类。 - atticus3000
EmailMessage和EmailManager显然只能在Silverlight应用程序中使用。Windows.ApplicationModel.Email在Windows Store应用程序中不存在,因此通用应用程序项目无法访问此命名空间。 - Jürgen Bayer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接