简体   繁体   English

在Windows Universal App中发送电子邮件

[英]Send eMail in Windows Universal App

Is it possible to send an email message in a Windows Universal App for Windows 8.1 and Windows Phone 8.1? 是否可以在适用于Windows 8.1和Windows Phone 8.1的Windows Universal App中发送电子邮件?

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

With this code line I can send an email, but I want to send a message with attachment. 使用此代码行,我可以发送电子邮件,但我想发送带附件的邮件。

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. 由于Microsoft错过了将EmailMessage和EmailManager添加到Windows应用商店库中,似乎只有两个不令人满意的解决方案:您可以使用共享或通过mailto协议发起电子邮件发送。 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 Phone 8.1 Windows Phone 8.1

You could use the following to send email with attachment: 您可以使用以下内容发送带附件的电子邮件:

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

Windows 8.1 Windows 8.1

On Windows 8.1, unfortunately, there is no way to send email with attachment. 不幸的是,在Windows 8.1上,无法发送带附件的电子邮件。 The mailto protocol is all you have and it doesn't not officially supports attachment. mailto协议就是你所拥有的,它并没有正式支持附件。 However, you can add attachment as the following: 但是,您可以添加附件,如下所示:

mailto:xxx@xxx.com?subject=xxx&body=xxx&attach=C:\\path\\to\\file 的mailto:?xxx@xxx.com受试者= XXX&体= XXX&ATTACH = C:\\路径\\到\\文件

or 要么

mailto:xxx@xxx.com?subject=xxx&body=xxx&Attachment=C:\\path\\to\\file 的mailto:?xxx@xxx.com受试者= XXX&体= XXX&附件= C:\\路径\\到\\文件

But it is up to the client to decide whether it will handle the attachment or not. 但是由客户决定是否会处理附件。 See this thread for more detail https://msdn.microsoft.com/en-us/library/aa767737(v=vs.85).aspx 有关更多详细信息,请参阅此主题https://msdn.microsoft.com/en-us/library/aa767737(v=vs.85).aspx

To send emails with attachment You would be needed to use the EmailMessage and EmailManager class. 发送带附件的电子邮件您需要使用EmailMessageEmailManager类。

1. EmailMessage: 1. EmailMessage:

The EmailMessage class defines the actual email that will be sent. EmailMessage类定义将要发送的实际电子邮件。 You can specify the recipients (To , CC , BC) , Subject and the Body of the email . 您可以指定收件人(收件人,抄送,BC),主题和电子邮件正文。

2. EmailManager: 2. EmailManager:

The EmailManager class is defined in the Windows.ApplicationModel.Email namespace . EmailManager类在Windows.ApplicationModel.Email命名空间中定义。 The EmailManager class provides a static method ShowComposeNewEmailAsync which accepts the EmailMessage as argument . EmailManager类提供了一个静态方法ShowComposeNewEmailAsync,它接受EmailMessage作为参数。 The ShowComposeNewEmailAsync will launch the Compose email Screen with the EmailMessage which allows the users to send an email message. ShowComposeNewEmailAsync将使用EmailMessage启动Compose email屏幕,允许用户发送电子邮件。

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

On this page: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871373.aspx 在此页面上: https//msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871373.aspx

Microsoft supplies a example how to share with your application. Microsoft提供了一个如何与您的应用程序共享的示例。 Download the source example app: http://go.microsoft.com/fwlink/p/?linkid=231511 下载源示例应用程序: http//go.microsoft.com/fwlink/p/?linkid = 231511

Open the solution and add a file: test.txt to the project root. 打开解决方案并将文件:test.txt添加到项目根目录。

Then open ShareFiles.xaml.cs and replace the class with: 然后打开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;
    }

}

Might not be the best code, but it helped me :) 可能不是最好的代码,但它帮助了我:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM