public static List <Group> LoadData(EmailAccount inboxOwner, ILogger logger) { Dictionary <string, Group> groups = new Dictionary <string, Group>(); using (var client = new ImapClient()) { client.ServerCertificateValidationCallback = (s, c, h, e) => true; // Accept all certificates client.Connect(inboxOwner.EmailHost, inboxOwner.ImapPort, useSsl: false); client.Authenticate(inboxOwner.EmailAddress, inboxOwner.EmailPassword); var inbox = client.Inbox; // Always available inbox.Open(FolderAccess.ReadOnly); SearchQuery query = SearchQuery .DeliveredAfter(DateTime.Parse("2018-07-28")) .And(SearchQuery.SubjectContains(TeatimeEmail.SubjectTag)); foreach (UniqueId messageId in inbox.Search(query)) { MimeMessage mimeMessage = inbox.GetMessage(messageId); TeatimeEmail te = JsonConvert.DeserializeObject <TeatimeEmail>(mimeMessage.TextBody); Participant sender = new Participant(te.FromEmailAddress); Group g = AddOrGetGroup(groups, sender.EmailAddress, inboxOwner.EmailAddress, te.ToEmailAddresses); Topic t = AddOrGetTopic(g, sender, te.TopicName); AddMessage(t, sender, te.MessageText); logger.LogInfo($"Loaded message from \"{te.FromEmailAddress}\" to \"{string.Join(", ", te.ToEmailAddresses)}\" for group \"{g.DisplayText}\" and topic \"{t.Name}\"."); } client.Disconnect(true); } return(groups.Values.ToList()); }
public static void SendMessage(EmailAccount sender, Participant recipient, ILogger logger) { using (var client = new SmtpClient()) { client.Connect(sender.EmailHost, sender.SmtpPort); client.Authenticate(sender.EmailAddress, sender.EmailPassword); var message = new MimeMessage(); message.From.Add(new MailboxAddress(sender.Name, sender.EmailAddress)); message.To.Add(new MailboxAddress(recipient.Name, recipient.EmailAddress)); message.Subject = TeatimeEmail.SubjectTag + " " + DateTime.Now.ToString("o"); TeatimeEmail te = new TeatimeEmail(); te.FromEmailAddress = sender.EmailAddress; te.ToEmailAddresses = new List <string>(new [] { recipient.EmailAddress }); te.TopicName = "Topic1"; te.MessageText = "Message" + DateTimeOffset.Now.ToUnixTimeMilliseconds(); message.Body = new TextPart("plain") { Text = JsonConvert.SerializeObject(te, Formatting.Indented) }; //message.Body = new TextPart("plain") { Text = XmlConvert.SerializeObject(te) }; client.Send(message); logger.LogInfo($"Message sent."); client.Disconnect(quit: true); } }
public static void SendMessage(EmailAccount sender, SortedSet <Participant> recipients, string topicName, string messageText, ILogger logger) { using (var client = new SmtpClient()) { client.Connect(sender.EmailHost, sender.SmtpPort); client.Authenticate(sender.EmailAddress, sender.EmailPassword); MimeMessage mimeMessage = new MimeMessage(); mimeMessage.From.Add(new MailboxAddress(sender.Name, sender.EmailAddress)); foreach (Participant r in recipients) { mimeMessage.To.Add(new MailboxAddress(r.Name, r.EmailAddress)); } mimeMessage.Subject = TeatimeEmail.SubjectTag + " " + DateTime.Now.ToString("o"); TeatimeEmail te = new TeatimeEmail(); te.FromEmailAddress = sender.EmailAddress; te.ToEmailAddresses = recipients.Select(r => r.EmailAddress).ToList(); te.TopicName = topicName; te.MessageText = messageText; mimeMessage.Body = new TextPart("plain") { Text = JsonConvert.SerializeObject(te, Formatting.Indented) }; client.Send(mimeMessage); logger.LogInfo($"E-Mail with topic \"{topicName}\" sent to \"{string.Join(", ", te.ToEmailAddresses)}\"."); client.Disconnect(quit: true); } }
public static void ListInboxMessages(EmailAccount inboxOwner, ILogger logger) { using (var client = new ImapClient()) { client.ServerCertificateValidationCallback = (s, c, h, e) => true; // Accept all certificates client.Connect(inboxOwner.EmailHost, inboxOwner.ImapPort, useSsl: false); client.Authenticate(inboxOwner.EmailAddress, inboxOwner.EmailPassword); var inbox = client.Inbox; // Always available inbox.Open(FolderAccess.ReadOnly); logger.LogInfo($"Total messages: {inbox.Count}"); logger.LogInfo($"Recent messages: {inbox.Recent}"); for (int i = 0; i < inbox.Count; i++) { var message = inbox.GetMessage(i); logger.LogInfo($"Subject: {message.Subject}"); string body = message.TextBody; TeatimeEmail te = JsonConvert.DeserializeObject <TeatimeEmail>(body); //TeatimeEmail te = XmlConvert.DeserializeObject<TeatimeEmail>(body); logger.LogInfo($"From: {te.FromEmailAddress}"); foreach (string toEmailAddress in te.ToEmailAddresses) { logger.LogInfo($"To: {toEmailAddress}"); } logger.LogInfo($"Topic: {te.TopicName}"); logger.LogInfo($"Message: {te.MessageText}"); } foreach (var summary in inbox.Fetch(0, -1, MessageSummaryItems.Full | MessageSummaryItems.UniqueId)) { logger.LogInfo($"[summary] {summary.Index:D2}: {summary.Envelope.Subject}"); } client.Disconnect(true); } }