public static bool MoveEmail(string archiveMailFolderID, string graph_api_endpoint, string username, string emailID, string token) { var moveEmailBody = new MoveEmailBody { DestinationId = archiveMailFolderID }; try { ApiCallers.API_Action_Caller( graph_api_endpoint, string.Format("/users/{0}/messages/{1}/move", username, emailID), token, "post", moveEmailBody ); return(true); } catch (System.Exception) { return(false); } }
public static bool MarkAsRead(string graph_api_endpoint, string username, string emailID, string token) { var markAsReadBody = new JsonBody { IsRead = true }; try { ApiCallers.API_Action_Caller( graph_api_endpoint, string.Format("/users/{0}/messages/{1}", username, emailID), token, "patch", markAsReadBody ); return(true); } catch (System.Exception) { return(false); } }
public virtual void R7Alerts(object sender, ElapsedEventArgs e) { if (testing) { slackWebHook = ConfigurationManager.AppSettings["TestSlackWebhook"]; username = ConfigurationManager.AppSettings["TestUsername"]; } SlackClient <string> slackClient = new SlackClient <string>(slackWebHook); try { string dateTime = DateTime.Now.ToString(); var adalResults = ServicePrincipal.GetS2SAccessTokenForProdMSAAsync().GetAwaiter().GetResult(); string token = null; token = adalResults.AccessToken; if (token != null) { dynamic mailFolders = null; mailFolders = ApiCallers.Get_API_Results( graph_api_endpoint, string.Format("/users/{0}/mailFolders", username), token ); string inboxMailFolderID = null; string archiveMailFolderID = null; if (mailFolders != null) { foreach (var mailFolder in mailFolders) { if (mailFolder["displayName"] == "Inbox") { inboxMailFolderID = mailFolder["id"]; } else if (mailFolder["displayName"] == "Archive") { archiveMailFolderID = mailFolder["id"]; } } dynamic emailJsonData = null; emailJsonData = ApiCallers.Get_API_Results( graph_api_endpoint, string.Format("/users/{0}/mailFolders/{1}/messages?$top=200", username, inboxMailFolderID), token ); if (emailJsonData != null && emailJsonData.Count >= 1) { foreach (var email in emailJsonData) { string emailID = Convert.ToString(email["id"]); if (email["isRead"] == false) { EmailObject emailObject = new EmailObject() { BodyPreview = Convert.ToString(email["bodyPreview"]), EmailSubject = Convert.ToString(email["subject"]), EmailSentDateTime = Convert.ToDateTime(email["sentDateTime"]), EmailBodyHtml = Convert.ToString(email["body"]["content"]), EmailSentFrom = Convert.ToString(email["sender"]["emailAddress"]["address"]) }; HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(emailObject.EmailBodyHtml); HtmlNode htmlBody = htmlDocument.DocumentNode.SelectSingleNode("//body"); Converter converter = new Converter(); string htmlMarkdown; if (htmlBody != null) { try { htmlMarkdown = converter.Convert(htmlBody.InnerHtml); } catch (Exception) { htmlMarkdown = htmlBody.InnerText; } } else { htmlMarkdown = converter.Convert(emailObject.EmailBodyHtml); } string slackColor; string slackChannel = ""; if (testing) { switch (emailObject.EmailSentFrom) { case "*****@*****.**": slackColor = SlackColors.Amber; break; case "*****@*****.**": slackColor = SlackColors.Red; break; default: slackColor = SlackColors.White; break; } } else { switch (emailObject.EmailSentFrom) { case "*****@*****.**": slackColor = SlackColors.Amber; slackChannel = "r7_phish_alerts"; // This can be changed to another channel name break; case "*****@*****.**": slackColor = SlackColors.Red; slackChannel = "r7_idr_alerts"; // This can be changed to another channel name break; default: slackColor = SlackColors.White; break; } } List <Attachment> slackAttachments = new List <Attachment>(); Attachment attachment = new Attachment() { text = htmlMarkdown.Replace("**", "*"), color = slackColor, pretext = string.Format($"`{emailObject.EmailSubject}` sent on *{emailObject.EmailSentDateTime.ToLocalTime().ToShortDateString()}* at *{emailObject.EmailSentDateTime.ToLocalTime().TimeOfDay} EST*") }; slackAttachments.Add(attachment); bool postMessage = slackClient.PostMessage( channel: slackChannel, username: "******", attachments: slackAttachments ); if (postMessage) { // Marks email as read ApiCallers.MarkAsRead(graph_api_endpoint, username, emailID, token); // Moves email to Archive folder in mailbox ApiCallers.MoveEmail(archiveMailFolderID, graph_api_endpoint, username, emailID, token); } else { logger.Info("Failed to post message. Leaving it unread and in the Inbox."); } } else { // Moves email to Archive folder in mailbox ApiCallers.MoveEmail(archiveMailFolderID, graph_api_endpoint, username, emailID, token); } } } } } } catch (System.NullReferenceException nullRef) { logger.Error($"Null Reference Exception: {nullRef}"); } catch (Exception ex) { List <Attachment> slackErrorAttachments = new List <Attachment>(); Attachment errorAttachment = new Attachment() { text = ex.ToString(), color = SlackColors.Purple, pretext = "*Program Exception - Halting service*" }; slackErrorAttachments.Add(errorAttachment); slackClient.PostMessage( username: "******", attachments: slackErrorAttachments ); Stop(); } }