Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have tried the following code to send an email from an Universal Windows Platform app. It works fine when I use EmailMessageBodyKind::PlainText. However, as indicated in the code below, EmailMessageBodyKind::Html seems to launch the email client with no content. Does anyone know what else needs to be set to get this to 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"[email protected]"));
        email->Subject = L"Email Report";
        auto randomAccessStreamReference = RandomAccessStreamReference::CreateFromStream(memStream);
        email->SetBodyStream(EmailMessageBodyKind::Html, randomAccessStreamReference);
        EmailManager::ShowComposeNewEmailAsync(email);
    }
);
like image 997
Kookei Avatar asked Aug 25 '15 07:08

Kookei


1 Answers

Well, I got some bad news for you.

It is not possible to do so using EmailManager.ShowComposeNewEmailAsync

Regarding using SetBodyStream with EmailMessageBodyKind.Html, we have this from MSDN forum:

Currently, the EmailMessageBodyKind.Html won't work for create a new HTML e-mail and there is no other way as a workaround, I've checked the internal resource, this API is used for populating messages from App server and save e-mail message into local folder.

The thing is: EmailManager.ShowComposeNewEmailAsync uses mailto to send the message and, as stated in some other question already answered here:

Section 2 of RFC 2368 says that the body field is supposed to be in text/plain format, so you can't do HTML.

However even if you use plain text it's possible that some modern mail clients would render the resulting link as a clickable link anyway, though.

That being said, you're relying on the mail client to render that HTML for you.
I've tested this using Windows 10 Mail Client, Gmail and Outlook (both the later on a web browser), and all of them failed to render a simple HTML <b> tag on the mail body, showing it as plain text instead.

Now, for the alternatives (from that same MSDN forum thread):

Note that if I use the ShareDataContract (DataTransferManager), I am able to set the HTML in the request and it will appear in the email body if the user chooses to share via Mail. However I would like to skip the Share UI and go directly with composing an email with recipient already populated, HTML body, and image attachments.

One alternative is to persist the HTML body to a file and then include that file as an additional attachment, however that is not ideal

The DataTransferManager successfully formatted the HTML message. Here's a small sample of how your sample code would look like, adapted from MSDN:

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

The limitations of this approach are:

  1. You cannot force the user to select e-mail as the sharing option
  2. You cannot previously specify the mail recipient.
like image 55
Gabriel Rainha Avatar answered Nov 15 '22 05:11

Gabriel Rainha