public static string SendWeather(string toAddres, string weather) { try { var fromAddress = new MailAddress("YourGmailAddres", "Name"); var toAddress = new MailAddress(toAddres, "Name"); const string fromPassword = "******"; const string subject = ""; string body = weather; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; using (var message = new MailMessage(fromAddress, toAddress) { Subject = subject, Body = body }) { smtp.Send(message); } return("Success"); } catch (Exception e) { Program.WriteLog("Can't send out text. error: " + e.Message); // just one more chance try { Program.GetGmailInbox(); } catch (Exception) { ParseWeather.RunParser(false); Program.OnFailure(); throw; } throw; } }
public static void GetGmailInbox() { int milliseconds = 1000; System.Threading.Thread.Sleep(milliseconds); // just put a second between runs. try { GmailService service = InitGmailConnection(); var re = service.Users.Messages.List("me"); re.LabelIds = "INBOX"; re.Q = "is:unread"; //only get unread; var res = re.Execute(); if (res != null && res.Messages != null) { foreach (var email in res.Messages) { var emailInfoReq = service.Users.Messages.Get("me", email.Id); var emailInfoResponse = emailInfoReq.Execute(); if (emailInfoResponse != null) { String from = ""; String date = ""; String subject = ""; String body = ""; //loop through the headers and get the fields we need... foreach (var mParts in emailInfoResponse.Payload.Headers) { if (mParts.Name == "Date") { date = mParts.Value; } else if (mParts.Name == "From") { from = mParts.Value; } else if (mParts.Name == "Subject") { subject = mParts.Value; } if (date != "" && from != "") { if (emailInfoResponse.Payload.Parts == null && emailInfoResponse.Payload.Body != null) { body = DecodeBase64String(emailInfoResponse.Payload.Body.Data); } else { body = GetNestedBodyParts(emailInfoResponse.Payload.Parts, ""); } } } var days = 0; if (body.Contains("1\r\n")) { days = 1; GetWeather.Parse(days, from, email.Id); } else if (body.Contains("2\r\n")) { days = 2; GetWeather.Parse(days, from, email.Id); } else if (body.Contains("3\r\n")) { days = 3; GetWeather.Parse(days, from, email.Id); } } } } service = null; re = null; res = null; GC.Collect(); GetGmailInbox(); } catch (Exception e) { WriteLog("Can't get Gmail Inbox. error: " + e.Message); try // just one more chance { GetGmailInbox(); } catch (Exception) { ParseWeather.RunParser(false); OnFailure(); throw; } throw; } }
static void Main(string[] args) { ParseWeather.ParseAndSave(); // just start the program with fresh results ParseWeather.RunParser(true); // then start parsing every 20 minuts GetGmailInbox(); }