Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 292
atticus3000 Avatar asked Jul 09 '14 06:07

atticus3000


People also ask

How do I send an email from UWP?

Launch the compose email dialogCreate a new EmailMessage object and set the data that you want to be pre-populated in the compose email dialog. Call ShowComposeNewEmailAsync to show the dialog. Attachments that you add to an email by using the EmailAttachment class will appear only in the Mail app.

What is the difference between Outlook and Windows Mail?

The main difference between Outlook and the Mail app is the target audience. The bundled app with Windows is meant to cater to consumers and those who check their emails on a daily basis. Outlook on-the-other-hand is for business and professionals who rely on email.

How do I use Universal Windows app?

Step 1: Go to Visual Studio 2015 update 3. Open Visual Studio New-->New project -->select Visual C# --->select Windows -->Universal -->select Blank Windows Universal App and give your app; a name (ex: sample). Step 2: Next step is to select the target version of Windows 10 SDK (Windows 10 Build 10240).


1 Answers

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);
  }
like image 177
Jürgen Bayer Avatar answered Oct 13 '22 09:10

Jürgen Bayer