コード例 #1
0
ファイル: Program.cs プロジェクト: breshch/AIS
		private static void Initialize()
		{
			using (var client = new ImapClient())
			{
				client.Connect("imap.gmail.com", 993, true);

				client.AuthenticationMechanisms.Remove("XOAUTH");

				client.Authenticate("breshch", "Mp~7200~aA");

				var query = SearchQuery.DeliveredAfter(DateTime.Now.AddDays(-1)).And(SearchQuery.All);

				var cardsFolder = client.GetFolder("Cards");
				cardsFolder.Open(FolderAccess.ReadOnly);


				foreach (var uid in cardsFolder.Search(query))
				{
					var message = cardsFolder.GetMessage(uid);
					var textPart = message.BodyParts.First() as TextPart;

					var date = message.Date.LocalDateTime;
					var body = textPart.GetText(Encoding.UTF8);

					using (var bc = new BusinessContext())
					{
						if (!bc.IsNewMessage(date, body))
						{
							continue;
						}
					}

					var subject = message.Subject;

					string bankName = subject.Substring(subject.LastIndexOf(" ")).Trim();
					
					CardBase card = null;

					switch (bankName)
					{
						case "VTB24":
							card = new CardVTB24(body);
							break;
						case "PSB":
							card = new CardPSB(body);
							break;
					}

					double? sum = card.GetSum();

					if (sum != null)
					{
						using (var bc = new BusinessContext())
						{
							bc.AddInfoSafeCard(date, sum.Value, Currency.RUR, body, bankName);
						}
					}

					using (var sw = new StreamWriter("mails.txt", true))
					{
						sw.WriteLine(DateTime.Now + "\t" + date + "\t" + sum + "\t" + body);
					}
				}

				client.Disconnect(true);
			}
		}