att*_*000 5 c# windows windows-phone-8 win-universal-app
是否可以在适用于Windows 8.1和Windows Phone 8.1的Windows Universal App中发送电子邮件?
await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));
Run Code Online (Sandbox Code Playgroud)
使用此代码行,我可以发送电子邮件,但我想发送带附件的邮件.
由于Microsoft错过了将EmailMessage和EmailManager添加到Windows应用商店库中,似乎只有两个不令人满意的解决方案:您可以使用共享或通过mailto协议发起电子邮件发送.我是这样做的:
/// <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);
}
Run Code Online (Sandbox Code Playgroud)
要发送带有附件的电子邮件,您需要使用EmailMessage和EmailManager类。
1. 邮件留言:
EmailMessage 类定义将发送的实际电子邮件。您可以指定电子邮件的收件人(收件人、抄送、BC)、主题和正文。
2. 电子邮件管理器:
EmailManager 类在 Windows.ApplicationModel.Email 命名空间中定义。EmailManager 类提供了一个静态方法 ShowComposeNewEmailAsync,它接受 EmailMessage 作为参数。ShowComposeNewEmailAsync 将启动带有 EmailMessage 的撰写电子邮件屏幕,允许用户发送电子邮件。
您可以在此处找到更多参考windows-phone-8-1-and-windows-runtime-apps-how-to-2-send-emails-with-attachment-in-wp-8-1
| 归档时间: |
|
| 查看次数: |
4481 次 |
| 最近记录: |