[UWP]UWP中获取联系人/邮件发送/SMS消息发送操作

这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作。

1. 获取联系人

UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间。

ContactStore contactStore =
await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); IReadOnlyList<Contact> contacts = await contactStore.FindContactsAsync();

NOTE:

1). 获取的联系人列表是当前OS关联的微软账户(Hotmail/Outlook/Live邮箱)的联系人。

2). 获取联系人需要在Package.appxmanifest中将Contacts勾选上。

[UWP]UWP中获取联系人/邮件发送/SMS消息发送操作

另外UWP提供了ContactPicker控件用来选取一个或者多个联系人,

ContactPicker contactPicker = new ContactPicker();

contactPicker.SelectionMode = ContactSelectionMode.Fields;

contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Address);
contactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber); //Select one or more contacts
IList<Contact> contacts = await contactPicker.PickContactsAsync(); if (contacts != null &&
contacts.Count > )
{
foreach (Contact contact in contacts)
{
// TODO:
}
} //// Select only one contact
//Contact contact = await contactPicker.PickContactAsync(); //if (contact != null)
//{
// //TODO:...
//}

使用ContactPicker获取联系人时,不需要在Package.appxmanifest中勾选Contacts。

2. 邮件发送

private async void SendMail(Contact recipient, StorageFile attchmentFile)
{
// Windows.ApplicationModel.Email
EmailMessage message = new EmailMessage();
// Mail subject
message.Subject = "This is a test mail."; message.Body = "This is a test mail, please ignore."; if(attchmentFile != null)
{
RandomAccessStreamReference stream =
RandomAccessStreamReference.CreateFromFile(attchmentFile); EmailAttachment attachment = new EmailAttachment(attchmentFile.Name, stream); // Set mail's attachment
message.Attachments.Add(attachment);
} ContactEmail email = recipient.Emails.FirstOrDefault(); if(email != null)
{
EmailRecipient emailRecipient = new EmailRecipient(email.Address); message.To.Add(emailRecipient);
} await EmailManager.ShowComposeNewEmailAsync(message);
}

3. SMS消息发送

private async void ComposeSms(Contact recipient,
string messageBody,
StorageFile attachmentFile,
string mimeType)
{
var chatMessage = new Windows.ApplicationModel.Chat.ChatMessage(); chatMessage.Body = messageBody; if (attachmentFile != null)
{
var stream = RandomAccessStreamReference.CreateFromFile(attachmentFile); var attachment = new Windows.ApplicationModel.Chat.ChatMessageAttachment(
mimeType,
stream); chatMessage.Attachments.Add(attachment);
} var phone = recipient.Phones.FirstOrDefault<Windows.ApplicationModel.Contacts.ContactPhone>();
if (phone != null)
{
chatMessage.Recipients.Add(phone.Number);
}
await Windows.ApplicationModel.Chat.ChatMessageManager.ShowComposeSmsMessageAsync(chatMessage);
}

感谢您的阅读。

上一篇:Jfinal QuartzPlugin 简单使用案例


下一篇:php7安装及配置