是否可以在 Windows 8.1 和 Windows Phone 8.1 的 Windows 通用应用程序中发送电子邮件?
await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));
使用此代码行我可以发送电子邮件,但我想发送带附件的消息。
最佳答案
由于 Microsoft 未能将 EmailMessage 和 EmailManager 添加到 Windows 应用商店应用程序库中,因此似乎只有两个不令人满意的解决方案:您可以使用共享或通过 mailto 协议(protocol)启动电子邮件发送。这是我的做法:
/// <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);
}
关于c# - 在 Windows 通用应用程序中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646815/