public bool CheckIncomingMessages(string mailTitle, string contactEmail) { UserCredential credential; using (FileStream stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) { string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); credPath = Path.Combine(credPath, ".credentials"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, GmailOps.Scopes, "*****@*****.**", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } GmailService service = new GmailService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "Gmail API Quickstart" }); List <Message> messageList = GmailOps.ListMessages(service, "me", "subject: " + mailTitle); foreach (Message message in messageList) { Message fullMessage = GmailOps.GetMessage(service, "me", message.Id); MessagePartHeader messageInfo = fullMessage.Payload.Headers.FirstOrDefault((MessagePartHeader x) => x.Name == "Delivered-To"); if (messageInfo != null && string.Equals(messageInfo.Value, contactEmail)) { return(true); } } return(false); }
public string ExtactLinkFromMessage(string mailTitle, string contactEmail, string enviorment) { UserCredential credential; using (FileStream stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) { string credPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); credPath = Path.Combine(credPath, ".credentials"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, GmailOps.Scopes, "*****@*****.**", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } GmailService service = new GmailService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "Gmail API Quickstart" }); List <Message> messageList = GmailOps.ListMessages(service, "me", "subject: " + mailTitle); foreach (Message message in messageList) { Message fullMessage = GmailOps.GetMessage(service, "me", message.Id); MessagePartHeader messageInfo = fullMessage.Payload.Headers.FirstOrDefault((MessagePartHeader x) => x.Name == "Delivered-To"); if (messageInfo != null && string.Equals(messageInfo.Value, contactEmail)) { string bodyData = fullMessage.Payload.Body.Data.Replace("-", "+").Replace("_", "/"); byte[] data64Base = Convert.FromBase64String(bodyData); string htmlCode = Encoding.UTF8.GetString(data64Base); Regex linkParser = new Regex("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?", RegexOptions.IgnoreCase | RegexOptions.Compiled); foreach (object link in linkParser.Matches(htmlCode)) { if (link.ToString().Contains(enviorment)) { return(link.ToString()); } } } } return(null); }