MailKit:如何遍历最近的电子邮件以获取具有给定主题的电子邮件

我正在使用.NET core 2.1创建框架。我使用MailKit并尝试了几种方法,如其页面和StackOverflow中所述,但仍然没有运气。

到目前为止,我已经能够获取最近邮件的UID,但似乎无法遍历列表以获取包含特定主题的电子邮件。

测试调用方法:

                 Core core = new Core(driver);

                 bool isEmailPresent = core.EnsureUnreadEmailExists("Your application details","emailaddress");

                 Assert.True(isEmailPresent);

核心(类):

          public bool EnsureUnreadEmailExists(string emailSubject,string emailBox)

          {

                 bool emailExists = true;

                 //start stopwatch to monitor elapsed time

                 Stopwatch sw = Stopwatch.StartNew();

                 //long maximumTimeout = 180000; //exit loop when maximumTimeout is reached

                 long maximumTimeout = 10000; //exit loop when maximumTimeout is reached



                 int sleepWait = 500;

                 //check if email exists and is readable

                 while (CheckEmailExistsAndIsReadable(GetGmailMessage(emailBox),emailSubject) && sw.ElapsedMilliseconds <= maximumTimeout)

                 {

                       //loop until email has arrived and is activated or maximumTimeout exceeded

                       Thread.Sleep(sleepWait);

                       Console.WriteLine("Time elapsed : {0}ms",sw.ElapsedMilliseconds);

                 }



                 try

                 {

                        //try

                       emailExists = false;

                 }

                 catch (Exception ae)

                 {
                     //exception message
                       emailExists = false;

                 }

                 sw.Stop();

                 return emailExists;

          }

          private IList<UniqueId> GetGmailMessage(string emailBox)

          {

                 // Switch on the string - emailBox.

                 switch (emailBox.ToUpper())

                 {

                        case "emailaddress1":

                              GMailHandler gmh1 = new GMailHandler("imap.gmail.com",993,true,"emailaddress","password");

                              return gmh1.GetRecentEmails();

                       default:

                              throw new Exception("Mail box not defined");

                 }



          }

GMailHandler(类)

          public GMailHandler(string mailServer,int port,bool ssl,string login,string password)

          {

                 if (ssl)

                       Client.Connect(mailServer,port);

                 else

                       Client.Connect(mailServer,port);

                 Client.Authenticate(login,password);

                 Client.Inbox.Open(Folderaccess.ReadOnly);

          }



          public IList<UniqueId> GetRecentEmails()

          {

                 IList<UniqueId> uids = client.Inbox.Search(SearchQuery.Recent);

                 return uids;

          }

方法(在Core类中)我无法完成通过UID进行迭代以获取具有给定主题的电子邮件

          public static bool CheckEmailExistsAndIsReadable(IList<UniqueId> uids,string emailSubject)

          {

                 try

                 {

                       Console.WriteLine("Attempt to read email messages .....");

                       string htmlBody = "";

                       string Subject = "";

                       bool EmailExists = false;

                       foreach (UniqueId msgId in uids)

                       {

                        //Incomplete
                              if (msgId.)

                              {

                                     htmlBody = email.BodyHtml.Text;

                                     Subject = email.Subject.ToString();

                                     Console.WriteLine("Subject: " + Subject);

                                     Match match = Regex.Match(Subject,emailSubject);

                                     Console.WriteLine("match: " + match.Groups[0].Value.ToString());

                                     if (match.Success)

                                     {

                                            Console.WriteLine("Email exists with subject line: " + emailSubject);

                                            EmailExists = true;

                                     }

                                     break;

                              }



                       }

                       if (EmailExists)

                       {

                              //email found so exit poll

                              Console.WriteLine("email found return false ");

                              return false;

                       }

                       else

                       {

                              //email not found so contiue to poll

                              Console.WriteLine("email not found return true ");

                              return true;

                       }





                 }

                 catch (IOException)

                 {

                       //the email with specific subject line has not yet arrived:

                       //may be still in transit

                       //or being processed by another thread

                       //or does not exist (has already been processed)

                       Console.WriteLine("Email has not arrived or still in transit. Continue to poll ... ");

                       return true;

                 }



          }

我想做的是添加一个或两个类并具有可重用的方法,以便我可以在以后的任何测试中使用它们。 1.访问gmail并验证包含特定主题的电子邮件 2.从该电子邮件中提取某些内容(例如安全密码),然后将其添加到文本文件中。 3.检查电子邮件的内容

dubuzui 回答:MailKit:如何遍历最近的电子邮件以获取具有给定主题的电子邮件

您可以遍历uid并下载消息,如下所示:

entityManager.find()

或者您可以提高效率,并使用如下方法:

foreach (uid in uids) {
    var message = client.GetMessage (uid);
    if (message.Subject == subject) {
        // we found the message
    }
}

如果uids.Count> 0,则该消息存在。

下载邮件后,您可以遍历所有身体部位并像这样提取它们:

var uids = client.Inbpx.Search (SearchQuery.SubjectContains (subject).And (SearchQuery.Recent));
,

您可以尝试类似

          public GMailHandler(string mailServer,int port,bool ssl,string login,string password)

          {

                  if (ssl)

                  {

                         client.Connect(mailServer,port);

                  }

                  else

                         client.Connect(mailServer,port);

                  client.Authenticate(login,password);

                  inbox = client.Inbox;

          }



          public IEnumerable<string> GetEmailWithSubject (string emailSubject)

          {

                  var messages = new List<string>();



                  inbox.Open(FolderAccess.ReadWrite);

                  var results = inbox.Search(SearchOptions.All,SearchQuery.Not(SearchQuery.Seen));

                  foreach (var uniqueId in results.UniqueIds)

                  {

                         var message = client.Inbox.GetMessage(uniqueId);



                         if (message.Subject.Contains(emailSubject))

                         {

                                messages.Add(message.HtmlBody);

                         }



                         //Mark message as read

                         inbox.AddFlags(uniqueId,MessageFlags.Seen,true);

                  }



                  client.Disconnect(true);



                  return messages;

          }
本文链接:https://www.f2er.com/3047335.html

大家都在问