await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));
使用这行代码,我可以发送电子邮件,但我想发送带有附件的消息。
await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));
使用这行代码,我可以发送电子邮件,但我想发送带有附件的消息。
由于微软没有将EmailMessage和EmailManager添加到Windows Store应用程序库中,似乎只有两个不太令人满意的解决方案:您可以使用共享或通过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);
}
您可以使用以下内容发送带附件的电子邮件:
var email = new EmailMessage();
email.To = ...;
email.Body = ...;
email.Attachments.Add( ... );
var ignore = EmailManager.ShowComposeNewEmailAsync(email);
很遗憾,在Windows 8.1上,没有办法发送带附件的电子邮件。您只能使用mailto协议,但它不支持正式的附件。不过,您可以按照以下方式添加附件:
mailto:xxx@xxx.com?subject=xxx&body=xxx&attach=C:\path\to\file
or
mailto:xxx@xxx.com?subject=xxx&body=xxx&Attachment=C:\path\to\file
但是由客户端决定是否处理附件。有关更多详细信息,请参见此线程https://msdn.microsoft.com/en-us/library/aa767737(v=vs.85).aspx。
public sealed partial class ShareText : SDKTemplate.Common.SharePage
{
public ShareText()
{
this.InitializeComponent();
LoadList();
}
List<Windows.Storage.IStorageItem> list { get; set; }
public async void LoadList()
{
var uri = new Uri("ms-appx:///test.txt");
var item = await StorageFile.GetFileFromApplicationUriAsync(uri);
list = new List<IStorageItem>();
list.Add(item);
}
protected override bool GetShareContent(DataRequest request)
{
bool succeeded = false;
string dataPackageText = TextToShare.Text;
if (!String.IsNullOrEmpty(dataPackageText))
{
DataPackage requestData = request.Data;
requestData.Properties.Title = TitleInputBox.Text;
requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional.
requestData.Properties.ContentSourceApplicationLink = ApplicationLink;
requestData.SetText(dataPackageText);
requestData.SetStorageItems(list);
succeeded = true;
}
else
{
request.FailWithDisplayText("Enter the text you would like to share and try again.");
}
return succeeded;
}
}
可能不是最好的代码,但它帮助了我 :)
发送带附件的电子邮件,您需要使用EmailMessage
和EmailManager
类。
1. EmailMessage:
EmailMessage类定义将要发送的实际电子邮件。您可以指定收件人(To,CC,BC),主题和电子邮件正文。
2. EmailManager:
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