protected override void Execute(NativeActivityContext context) { string username = Email.Get(context); //发送端账号 string password = Password.Get(context); //发送端密码(这个客户端重置后的密码) string server = Server.Get(context); //邮件服务器 Int32 port = Port.Get(context); //端口号 Int32 counts = Counts.Get(context); List <object> configList = new List <object>(); Pop3Client emailClient = new Pop3Client(); List <MimeMessage> emails = new List <MimeMessage>(); string mailTopicKey = MailTopicKey.Get(context); string mailSenderKey = MailSenderKey.Get(context); string mailTextBodyKey = MailTextBodyKey.Get(context); try { emailClient.Connect(server, port, SecureConnection); emailClient.Authenticate(username, password); for (int i = emailClient.Count - 1, j = 0; i >= 0 && j < counts; i--, j++) { MimeMessage message = emailClient.GetMessage(i); InternetAddressList Sender = message.From; string SenderStr = Sender[0].Name; string Topic = message.Subject; if (mailTopicKey != null && mailTopicKey != "") { if (Topic == null || Topic == "") { j--; continue; } if (!Topic.Contains(mailTopicKey)) { j--; continue; } } if (mailSenderKey != null && mailSenderKey != "") { if (SenderStr == null || SenderStr == "") { j--; continue; } if (!SenderStr.Contains(mailSenderKey)) { j--; continue; } } if (mailTextBodyKey != null && mailTextBodyKey != "") { if (message.TextBody == null || message.TextBody == "") { j--; continue; } if (!message.TextBody.Contains(mailTextBodyKey)) { j--; continue; } } emails.Add(message); if (DeleteMessages) { emailClient.DeleteMessage(i); } } MailMsgList.Set(context, emails); emailClient.Disconnect(true); } catch (Exception e) { SharedObject.Instance.Output(SharedObject.enOutputType.Error, "获取POP3邮件失败", e.Message); emailClient.Disconnect(true); } configList.Add(server); configList.Add(port); configList.Add(SecureConnection); configList.Add(username); configList.Add(password); configList.Add(""); if (Body != null) { object[] buff = configList.ToArray(); context.ScheduleAction(Body, emails, emailClient, buff); } }
protected override void Execute(NativeActivityContext context) { string username = Email.Get(context); //发送端账号 string password = Password.Get(context); //发送端密码(这个客户端重置后的密码) string server = Server.Get(context); //邮件服务器 Int32 port = Port.Get(context); //端口号 Int32 counts = Counts.Get(context); //检索邮件数 string mainFolder = MainFolder.Get(context); //邮件文件夹 string mailTopicKey = MailTopicKey.Get(context); string mailSenderKey = MailSenderKey.Get(context); string mailTextBodyKey = MailTextBodyKey.Get(context); List <object> configList = new List <object>(); List <MimeMessage> emails = new List <MimeMessage>(); ImapClient client = new ImapClient(); SearchQuery query; IList <UniqueId> uidss; try { client.CheckCertificateRevocation = false; client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(server, port, SecureConnection); client.Authenticate(username, password); if (OnlyUnreadMessages || _AllUnreadMessages) { query = SearchQuery.NotSeen; } else { query = SearchQuery.All; } List <IMailFolder> mailFolderList = client.GetFolders(client.PersonalNamespaces[0]).ToList(); IMailFolder folder = client.GetFolder(mainFolder); folder.Open(FolderAccess.ReadWrite); emails = new List <MimeMessage>(); if (_AllUnreadMessages) { query = SearchQuery.NotSeen; uidss = folder.Search(query); for (int i = uidss.Count - 1; i >= 0; i--) { MimeMessage message = folder.GetMessage(new UniqueId(uidss[i].Id)); emails.Add(message); if (MarkAsRead) { folder.AddFlags(new UniqueId(uidss[i].Id), MessageFlags.Seen, true);//如果设置为true,则不会发出MessageFlagsChanged事件 } if (DeleteMessages) { folder.AddFlags(new UniqueId(uidss[i].Id), MessageFlags.Deleted, true);//如果设置为true,则不会发出MessageFlagsChanged事件 } } } else { uidss = folder.Search(query); for (int i = uidss.Count - 1, j = 0; i >= 0 && j < counts; i--, j++) { MimeMessage message = folder.GetMessage(new UniqueId(uidss[i].Id)); InternetAddressList Sender = message.From; string SenderStr = Sender.Mailboxes.First().Address; string Topic = message.Subject; if (mailTopicKey != null && mailTopicKey != "") { if (Topic == null || Topic == "") { j--; continue; } if (!Topic.Contains(mailTopicKey)) { j--; continue; } } if (mailSenderKey != null && mailSenderKey != "") { if (SenderStr == null || SenderStr == "") { j--; continue; } if (!SenderStr.Contains(mailSenderKey)) { j--; continue; } } if (mailTextBodyKey != null && mailTextBodyKey != "") { if (message.TextBody == null || message.TextBody == "") { j--; continue; } if (!message.TextBody.Contains(mailTextBodyKey)) { j--; continue; } } emails.Add(message); if (MarkAsRead) { folder.AddFlags(new UniqueId(uidss[i].Id), MessageFlags.Seen, true);//如果设置为true,则不会发出MessageFlagsChanged事件 } if (DeleteMessages) { folder.AddFlags(new UniqueId(uidss[i].Id), MessageFlags.Deleted, true);//如果设置为true,则不会发出MessageFlagsChanged事件 } } } //获取搜索结果的摘要信息(我们需要UID和BODYSTRUCTURE每条消息,以便我们可以提取文本正文和附件)(获取全部邮件) //var items = folder.Fetch(uidss, MessageSummaryItems.UniqueId | MessageSummaryItems.BodyStructure); MailMsgList.Set(context, emails); client.Disconnect(true); configList.Add(server); configList.Add(port); configList.Add(SecureConnection); configList.Add(username); configList.Add(password); configList.Add(mainFolder); } catch (Exception e) { client.Disconnect(true); SharedObject.Instance.Output(SharedObject.enOutputType.Error, "获取IMAP邮件失败", e.Message); } if (Body != null) { object[] buff = configList.ToArray(); context.ScheduleAction(Body, emails, client, buff); } }