User Tools

Site Tools


notes:uwp:internet

Internet in UWP

Check the Internet connectivity:

public async Task<bool> IsInternetAvailable()
{
    await Task.Delay(0); // an easy way to make the method async
    var profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
    if (profile == null)
        return false;
    var net = Windows.Networking.Connectivity.NetworkConnectivityLevel.InternetAccess;
    return profile.GetNetworkConnectivityLevel().Equals(net);
}

Example: Send an email using the compose email dialog:

using System.Threading.Tasks;
using Windows.ApplicationModel.Email;
using Windows.Storage;
using Windows.Storage.Streams;
...
public async Task SendEmail(string recipientEmail, StorageFile fileAttachment)
{
    // Create an EmailMessage object. It is located in the Windows.ApplicationModel.Email namespace.
    var email = new EmailMessage();
 
    // Pre-populate the fields of an email.
    email.Subject = "Test Email";
    email.Body = "This is a test email ...";
 
    // Create a file attachment. 
    var stream = RandomAccessStreamReference.CreateFromFile(fileAttachment);
    var attachment = new EmailAttachment(fileAttachment.Name, stream);
    email.Attachments.Add(attachment);
 
    // Add the recipients. There are properties for Sender, To, CC, and Bcc.
    email.To.Add(new EmailRecipient(recipientEmail));
 
    // Use the compose email dialog to allow the user to send the email message. 
    await EmailManager.ShowComposeNewEmailAsync(email);
}
...
// Obtain the file attachment.  
var localFolder = ApplicationData.Current.LocalFolder;
string path = System.IO.Path.Combine(localFolder.Path, "test.txt");
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
 
// Send an email.
await SendEmail("myemail@something.com", file);  

MSDN: Constructing and sending emails

notes/uwp/internet.txt · Last modified: 2017/06/06 by leszek