static void AddEmail() { EmailContext db = new EmailContext(); Email email = new Email(); email.emailbody = "123124234q45q345q"; email.EmailRecipientsId = 1; email.fromaddress = "*****@*****.**"; email.IdItem = 114; email.ItemDescription = "asdfwef24234234"; email.subject = "asdfasdf525asdfasdfasdfasdfasdf11123525"; email.toaddress = "*****@*****.**"; try { for (int x = 0; x < 250; x++) { System.Threading.Thread.Sleep(2000); db.Emails.Add(email); db.SaveChanges(); Console.WriteLine("Added email " + email.Id.ToString()); logger.Debug("Added email " + email.Id.ToString() + "\n\n"); } } catch (Exception e) { Console.WriteLine("Could not add email " + e.ToString()); logger.Debug("Could not add email " + e.ToString() + "\n\n"); } }
public static void SendEmail() { SmtpClient client = new SmtpClient("smtp.gmail.com", 587) { Credentials = new NetworkCredential("*****@*****.**", "passWord321$"), EnableSsl = true }; while (true) { //System.Threading.Thread.Sleep(10000); EmailContext db = new EmailContext(); Email email = null; bool success2; try { while (checkemaildb(db, out email)) { SendThroughGmail(db, email, client); } } catch (Exception e) { logger.Debug("failed to send email :" + e + "-------------------\n\n"); } } }
public static bool checkhistoryiddb(EmailContext db, out ulong?outhistoryid) { outhistoryid = 0; try { HistoryID historyid = db.HistoryIDs.FirstOrDefault(); if (historyid != null) { outhistoryid = (ulong)historyid.History; return(true); } else { outhistoryid = null; return(false); } } catch (Exception e) { logger.Debug("Error in checkmaildb:" + e + "-------------------\n\n"); return(false); } }
public static void SendThroughGmail(EmailContext db, Email email, SmtpClient client) { client.Send(email.fromaddress, email.toaddress, email.subject, email.emailbody); //loggerwrapper.PickAndExecuteLogging("send email record " + email.Id); db.Emails.Remove(email); db.SaveChanges(); Console.WriteLine("Deleted email " + email.Id); }
static void ReadMail() { EmailContext db = new EmailContext(); Email email = new Email(); try { for (int x = 0; x < 250; x++) { System.Threading.Thread.Sleep(2000); email = db.Emails.FirstOrDefault(); Console.WriteLine("Read:" + email.Id.ToString()); logger.Debug("Read:" + email.Id.ToString()); } } catch (Exception e) { Console.WriteLine("Could not read email: " + e.ToString()); logger.Debug("Could not read email: " + e.ToString()); } }
// used to read database and send messages public static bool checkemaildb(EmailContext db, out Email outemail) { Email email = db.Emails.FirstOrDefault(); outemail = null; try { if (email != null) { outemail = email; return(true); } else { return(false); } } catch (Exception e) { logger.Debug("Error in checkmaildb:" + e + "------------------\n\n"); return(false); } }
public static void history() { EmailContext db = new EmailContext(); List <HistoryID> histories = db.HistoryIDs.ToList(); List <Message> messagelist = new List <Message>(); ulong? newesthistoryid; //email all history ids being processed while (true) { //System.Threading.Thread.Sleep(2000); while (checkhistoryiddb(db, out newesthistoryid)) { bool synced = fullsync(out messagelist, out newesthistoryid); bool allrecordsmarkedasprocessed = true; if (synced && messagelist.Count > 0) { foreach (Message message in messagelist) { string subjectline = message.Payload.Headers[0].Value; int index = subjectline.IndexOf("AwolrID:"); string awolrid = message.Payload.Headers[0].Value.Substring(index + 8); string rebuild = subjectline.Substring(0, index - 1); //write to database // Decode string body = " "; var encodedString2 = message.Payload.Parts[0].Body.Data; if (encodedString2 != null) { var bytes2 = Decode(encodedString2); body = System.Text.Encoding.UTF8.GetString(bytes2); // Hello Base64Url encoding! } string to = null; try { EmailRecipients emailRecipients = db.EmailRecipients.Where(er => er.bidfakeemailaddress.Contains(awolrid) || er.pidfakeemailaddress.Contains(awolrid)).FirstOrDefault(); if (emailRecipients.bidfakeemailaddress == awolrid) { subjectline = rebuild + " AwolrID:" + emailRecipients.pidfakeemailaddress; to = emailRecipients.bidrealemailaddress; } else { subjectline = rebuild + " AwolrID:" + emailRecipients.bidfakeemailaddress; to = emailRecipients.pidrealemailaddress; } try { Email email = new Email(); email.emailbody = body; email.EmailRecipientsId = emailRecipients.Id; email.fromaddress = "*****@*****.**"; email.toaddress = to; email.IdItem = emailRecipients.IdItem; email.subject = subjectline; email.ItemDescription = subjectline; db.Emails.Add(email); db.SaveChanges(); Console.WriteLine("New Email Record Generated For Email " + email.Id); } catch (Exception e) { logger.Debug("Could not create relay response email. Returning method" + e.ToString() + "--------------\n\n"); return; } //ModifyMessageRequest mods = new ModifyMessageRequest(); //List<String> addedlabels = new List<String> { "Label_5558979356135685998" }; //List<String> removedlabels = new List<String> { }; //mods.AddLabelIds = addedlabels; //mods.RemoveLabelIds = removedlabels; //try //{ // service.Users.Messages.Modify(mods, userId, message.Id).Execute(); //} //catch (Exception e) //{ // allrecordsmarkedasprocessed = false; // throw new Exception("message could not be marked as processed.messageid " + message.Id + e.ToString()); //} try { service.Users.Messages.Delete(userId, message.Id).Execute(); } catch (Exception e) { logger.Debug("Could not delete processed email" + e.ToString()); } } catch (Exception e) { logger.Debug("message could not be marked as processed. messageid: " + message.Id + ":" + e.ToString() + "--------------\n\n"); } } } } } //foreach (HistoryID historyid in histories) //{ // long id = historyid.History; // try // { // ulong historyid_start_conversionresult; // List<History> result = null; // try // { // historyid_start_conversionresult = Convert.ToUInt64(id); // result = ListHistory(service, "*****@*****.**", historyid_start_conversionresult, messagelist); // } // catch (OverflowException) // { // Console.WriteLine("{0} is outside the range of the UInt64 type.", historyid); // } // if (result != null) // { // //loggerWrapper.PickAndExecuteLogging("Was able to retrieve history" + id.ToString()); // } // } // catch (Exception e) // { // } //} }
public static bool fullsync(out List <Message> messagelist, out ulong?historyid) { historyid = 666; messagelist = null; List <Message> result = new List <Message>(); List <Message> fullmessageobjects = new List <Message>(); EmailContext db = new EmailContext(); UsersResource.MessagesResource.ListRequest request = service.Users.Messages.List(userId); try { db.Database.ExecuteSqlCommand("TRUNCATE TABLE [HistoryID]"); } catch (Exception e) { logger.Debug("Could not truncate HistoryID table! " + e.ToString() + "--------------\n\n"); return(false); } historyid = 0; do { try { ListMessagesResponse response = request.Execute(); result.AddRange(response.Messages); request.PageToken = response.NextPageToken; } catch (Exception e) { logger.Debug("An error occurred in full sync: " + e.ToString() + "--------------\n\n"); return(false); } } while (!String.IsNullOrEmpty(request.PageToken)); if (result != null) { bool once = false; foreach (Message message in result) { try { var fullmessageobject = service.Users.Messages.Get(userId, message.Id).Execute(); if (!once) { historyid = fullmessageobject.HistoryId; once = true; } Google.Apis.Gmail.v1.Data.MessagePartHeader subjectheader = fullmessageobject.Payload.Headers.Where(h => h.Name == "Subject").FirstOrDefault(); fullmessageobject.Payload.Headers[0] = subjectheader; if (!fullmessageobject.LabelIds.Contains("SENT") && subjectheader.Value.Contains("AwolrID:")) { fullmessageobjects.Add(fullmessageobject); } } catch (Exception e) { logger.Debug("An error occurred trying to get message from api or assign to internal list: " + e.ToString() + "-------------------\n\n"); return(false); } } } messagelist = fullmessageobjects; return(true); }