protected async void ListEmailsCommand_Click(object sender, EventArgs e) { var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationHelper authenticationHelper = new AuthenticationHelper(); authenticationHelper.EnsureAuthenticationContext(new ADALTokenCache(signInUserId)); MailHelper mailHelper = new MailHelper(authenticationHelper); var mails = await mailHelper.GetMessages(); List<String> results = new List<String>(); commandResult.Text = String.Format("Found {0} mails! Showing first 10, if any.", mails.Count()); foreach (var item in mails.Take(10)) { results.Add(String.Format( "From: {0} - Subject: {1}", item.From != null ? item.From.EmailAddress.Address : "", !String.IsNullOrEmpty(item.Subject) ? item.Subject : String.Empty)); } resultsList.DataSource = results; resultsList.DataBind(); }
public async Task<ActionResult> ListMyMessages() { HomeViewModel model = new HomeViewModel(); var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationHelper authenticationHelper = new AuthenticationHelper(); authenticationHelper.EnsureAuthenticationContext(new ADALTokenCache(signInUserId)); MailHelper mailHelper = new MailHelper(authenticationHelper); var mails = await mailHelper.GetMessages(); model.Office365ActionResult = String.Format("Found {0} mails! Showing first 10, if any.", mails.Count()); foreach (var item in mails.Take(10)) { model.Items.Add(String.Format( "From: {0} - Subject: {1}", item.From != null ? item.From.EmailAddress.Address : "", !String.IsNullOrEmpty(item.Subject) ? item.Subject : String.Empty)); } return View("UseOffice365API", model); }
public async Task<ActionResult> SendMail(String targetEMail) { HomeViewModel model = new HomeViewModel(); model.TargetEMail = targetEMail; var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationHelper authenticationHelper = new AuthenticationHelper(); authenticationHelper.EnsureAuthenticationContext(new ADALTokenCache(signInUserId)); MailHelper mailHelper = new MailHelper(authenticationHelper); await mailHelper.SendMail(targetEMail, "Let's Hack-A-Thon - Office365Api.MVCDemo", "This will be <B>fun...</B>"); model.Office365ActionResult = "Email sent!"; return View("UseOffice365API", model); }
protected async void SendMailCommand_Click(object sender, EventArgs e) { var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; AuthenticationHelper authenticationHelper = new AuthenticationHelper(); authenticationHelper.EnsureAuthenticationContext(new ADALTokenCache(signInUserId)); MailHelper mailHelper = new MailHelper(authenticationHelper); await mailHelper.SendMail(TargetEmail.Text, "Let's Hack-A-Thon - Office365Api.WebForms", "This will be <B>fun...</B>"); commandResult.Text = "Email sent!"; resultsList.DataSource = new List<String>(); resultsList.DataBind(); }
private async void Button_Click(object sender, RoutedEventArgs e) { Uri authorityUri; Uri sharePointTenantUri; Uri siteCollectionUri; Regex mailRegex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); // Input parameters sanity check if ((String.IsNullOrEmpty(this.Authority.Text) || !Uri.TryCreate(this.Authority.Text, UriKind.Absolute, out authorityUri)) || (String.IsNullOrEmpty(this.SharePointTenantUri.Text) || !Uri.TryCreate(this.SharePointTenantUri.Text, UriKind.Absolute, out sharePointTenantUri)) || (String.IsNullOrEmpty(this.SiteCollectionUri.Text) || !Uri.TryCreate(this.SiteCollectionUri.Text, UriKind.Absolute, out siteCollectionUri)) || (String.IsNullOrEmpty(this.MailAddressTo.Text) || !mailRegex.IsMatch(this.MailAddressTo.Text)) || (String.IsNullOrEmpty(this.FileToUploadPath.Text) || !File.Exists(this.FileToUploadPath.Text))) { MessageBoxResult msgBoxResult = MessageBox.Show("Please fill all the input parameters!"); return; } try { PrintHeader("Authentication Phase"); AuthenticationHelper authenticationHelper = new AuthenticationHelper(); authenticationHelper.EnsureAuthenticationContext(this.Authority.Text); PrintHeader("Discovery API demo"); DiscoveryHelper discoveryHelper = new DiscoveryHelper(authenticationHelper); var t = await discoveryHelper.DiscoverMyFiles(); PrintSubHeader("Current user information"); PrintAttribute("OneDrive URL", t.ServiceEndpointUri); var t3 = await discoveryHelper.DiscoverMail(); PrintAttribute("Mail URL", t3.ServiceEndpointUri); PrintHeader("Files API demo"); // Read all files on your onedrive PrintSubHeader("List TOP 20 files and folders in the OneDrive"); MyFilesHelper myFilesHelper = new MyFilesHelper(authenticationHelper); var allMyFolders = await myFilesHelper.GetMyFolders(); var allMyFiles = await myFilesHelper.GetMyFiles(); foreach (var item in allMyFiles.Take(20)) { PrintAttribute("URL", item.WebUrl); } // Upload a file to the "Shared with everyone" folder PrintSubHeader("Upload a file to OneDrive"); if (allMyFolders.Any()) { await myFilesHelper.UploadFile(this.FileToUploadPath.Text, allMyFolders.First().Id); } else { await myFilesHelper.UploadFile(this.FileToUploadPath.Text); } // Shared with everyone // Iterate over the "Shared with everyone" folder PrintSubHeader("List all files and folders in the Shared with everyone folder"); var myFiles = await myFilesHelper.GetMyFiles(allMyFolders.First().Id); foreach (var item in myFiles) { PrintAttribute("URL", item.WebUrl); } PrintHeader("Mail API demo"); //Get mails PrintSubHeader("Retrieve mails from INBOX"); MailHelper mailHelper = new MailHelper(authenticationHelper); var mails = await mailHelper.GetMessages(); PrintSubHeader(String.Format("Printing TOP 10 mails of {0}", mails.Count())); foreach (var item in mails.Take(10)) { PrintAttribute("From ", String.Format("{0} / {1}", item.From != null ? item.From.EmailAddress.Address : "", item.Subject)); } //Send mail PrintSubHeader("Send a mail"); await mailHelper.SendMail(this.MailAddressTo.Text, "Let's Hack-A-Thon - Office365Api.Demo", "This will be <B>fun...</B>"); //Create message in drafts folder PrintSubHeader("Store a mail in the drafts folder"); await mailHelper.DraftMail(this.MailAddressTo.Text, "Let's Hack-A-Thon - Office365Api.Demo", "This will be fun (in draft folder)..."); PrintHeader("Active Directory API demo"); ActiveDirectoryHelper activeDirectoryHelper = new ActiveDirectoryHelper(authenticationHelper); var allADUsers = await activeDirectoryHelper.GetUsers(); PrintSubHeader(String.Format("Printing TOP 10 users of {0}", allADUsers.Count())); foreach (var user in allADUsers.Take(10)) { PrintAttribute("User", user.UserPrincipalName); } PrintHeader("All done..."); } catch (Exception ex) { string message = ""; if (ex is AggregateException) { message = ex.InnerException.Message; } else { message = ex.Message; } PrintException(message); } }