Send eMail in Windows Universal App

2020-07-13 09:27发布

问题:

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: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.

回答1:

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


回答2:

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

On Windows 8.1, unfortunately, there is no way to send email with attachment. The mailto protocol is all you have and it doesn't not officially supports attachment. However, you can add attachment as the following:

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

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



回答3:

To send emails with attachment You would be needed to use the EmailMessage and EmailManager class.

1. EmailMessage:

The EmailMessage class defines the actual email that will be sent. You can specify the recipients (To , CC , BC) , Subject and the Body of the email .

2. EmailManager:

The EmailManager class is defined in the Windows.ApplicationModel.Email namespace . The EmailManager class provides a static method ShowComposeNewEmailAsync which accepts the EmailMessage as argument . The ShowComposeNewEmailAsync will launch the Compose email Screen with the EmailMessage which allows the users to send an email message.

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



回答4:

On this page: https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871373.aspx

Microsoft supplies a example how to share with your application. Download the source example app: http://go.microsoft.com/fwlink/p/?linkid=231511

Open the solution and add a file: test.txt to the project root.

Then open ShareFiles.xaml.cs and replace the class with:

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 :)