Win 10 UWP EmailMessage API 是否支持 HTML 正文?

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

我已尝试使用以下代码从通用 Windows 平台应用程序发送电子邮件。当我使用 EmailMessageBodyKind::PlainText 时它工作正常。但是,如下面的代码所示,EmailMessageBodyKind::Html 似乎启动了没有内容的电子邮件客户端。有谁知道还需要设置什么才能让它工作 - 文档是稀疏的 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);
    }
);

嗯,我有一些坏消息要告诉你。

无法使用 EmailManager.ShowComposeNewEmailAsync

关于将 SetBodyStreamEmailMessageBodyKind.Html 一起使用,我们有此 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.

事情是:EmailManager.ShowComposeNewEmailAsync 使用 mailto 发送消息,并且如 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.

也就是说,您依赖邮件客户端为您呈现 HTML。
我已经使用 Windows 10 邮件客户端、Gmail 和 Outlook(后者都在网络浏览器上)对此进行了测试,但它们都未能在邮件正文,将其显示为纯文本。

现在,对于备选方案(来自 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

DataTransferManager 成功地格式化了 HTML 消息。 这是您的示例代码的一个小示例,改编后 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);
}

这种方法的局限性是:

  1. 您不能强制用户select将电子邮件作为共享选项
  2. 您之前无法指定邮件收件人。