Does the Win 10 UWP EmailMessage API support an HTML body?

I tried the following code to send email from the Universal Platform application. It works great when I use EmailMessageBodyKind :: PlainText . However, as indicated in the code below, EmailMessageBodyKind :: Html seems to start the mail client without content. Does anyone know what else needs to be set up to make this work - the documentation is sparse 8 (

using namespace Windows::Storage::Streams; using namespace Windows::ApplicationModel::Email; using namespace Windows::Security::Cryptography; auto bin = CryptographicBuffer::ConvertStringToBinary( L"<html><body>this <b>is</b> text</body></html>", BinaryStringEncoding::Utf16LE); auto memStream = ref new InMemoryRandomAccessStream(); concurrency::create_task(memStream->WriteAsync(bin)).then( [memStream](unsigned) { auto email = ref new EmailMessage(); email->To->Append(ref new EmailRecipient(L" test@gmail.com ")); email->Subject = L"Email Report"; auto randomAccessStreamReference = RandomAccessStreamReference::CreateFromStream(memStream); email->SetBodyStream(EmailMessageBodyKind::Html, randomAccessStreamReference); EmailManager::ShowComposeNewEmailAsync(email); } ); 
+6
source share
1 answer

Ok, I have some bad news for you.

This cannot be done using EmailManager.ShowComposeNewEmailAsync

Regarding the use of SetBodyStream with EmailMessageBodyKind.Html , we have this from the MSDN forum:

Currently, EmailMessageBodyKind.Html will not work to create a new HTML email address, and there is no other way as a workaround, I checked the internal resource, this API is used to populate messages from the Application Server and save the email message in a local folder.

The fact is that EmailManager.ShowComposeNewEmailAsync uses mailto to send a message and, as indicated in another question already asked :

Section 2 of RFC 2368 says that the body field should be in text / plain, so you cannot do HTML.

However, even if you use plain text, it’s possible that some modern emails clients would have provided the resulting link as a clickable link anyway.

In doing so, you rely on the email client to display this HTML code for you.
I tested this using the Windows 10, Gmail and Outlook mail client (as later in the web browser), and all of them could not display a simple HTML <b> in the body of the message, showing it as plain text.

Now for alternatives (from the same MSDN forum forum ):

Please note that if I use ShareDataContract (DataTransferManager), I am able to set the HTML in the request, and it will appear in the letter if the user decides to share via Mail. However, I would like to skip the general user interface and go directly with composing the email with the recipient already filled in, the HTML body and image attachments.

One option is to save the HTML body to a file and then include this file as an additional application, however this is not ideal

DataTransferManager successfully formatted an HTML message. Here is a small example of what your sample code adapted from MSDN will look like:

 void YourView::ShareHtml() { DataTransferManager^ dataTransferManager = DataTransferManager::GetForCurrentView(); auto dataRequestedToken = dataTransferManager->DataRequested += ref new TypedEventHandler<DataTransferManager^, DataRequestedEventArgs^>( this, &YourView::OnShareHtml); DataTransferManager::ShowShareUI(); } void YourView::OnShareHtml(DataTransferManager^ sender, DataRequestedEventArgs^ e) { DataRequest^ request = e->Request; request->Data->Properties->Title = "Email Report"; String^ html = L"<html><body>this <b>is</b> text</body></html>"; String^ htmlFormat = HtmlFormatHelper::CreateHtmlFormat(html); request->Data->SetHtmlFormat(htmlFormat); } 

Limitations of this approach are as follows:

  • You cannot force the user to select email as a sharing option.
  • You cannot pre-specify the mail recipient.
+3
source

All Articles