c# - MailKit:如何迭代最近的电子邮件以获取具有给定主题的电子邮件

标签 c# .net selenium gmail 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;

          }

方法(在核心类中)我无法完成迭代 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.检查邮件内容

最佳答案

你可以尝试类似的事情

          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;

          }

关于c# - MailKit:如何迭代最近的电子邮件以获取具有给定主题的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59003926/

相关文章:

c# - C# 如何让进度条在不同的线程中运行

python - 如何处理 splinter (python) 中的 Selenium 异常?

python - 使用 Selenium webdriver 运行时是否可以使用 Chrome 的开发工具?

.net - 方法摘要XML注释的良好做法

c# - 每个测试 session 启动和停止一次 chromedriver

c# - 从 JSON Api WinRT 反序列化对象

c# - 将 DirectShow 视频窗口附加到 WPF 控件

c# - 为什么 List<string>.Sort() 很慢?

c# - 带 C# 的 SFML,启动麻烦

c# - System.Action<T> 作为 EventHandler