0

我试图让 MimeKit 检索 IMAP 服务器上的未读电子邮件列表

这是我的代码

using (var client = new ImapClient())
{
    client.ServerCertificateValidationCallback = (s, c, h, e) => true;
    client.Connect("mail.zebracars.co.uk", 993, true);
    client.AuthenticationMechanisms.Remove("XOAUTH2");
    client.Authenticate("email@email.co.uk", "12345");

    var inbox = client.Inbox;
    inbox.Open(FolderAccess.ReadOnly);

    SearchQuery query;
    query = SearchQuery.SubjectContains("test");

    foreach (var uid in inbox.Search(query))
    {
        var message = inbox.GetMessage(uid);

        List<String> formEmails = null;
        List<DateTime> messageDate = null;
        formEmails.Add(message.TextBody);
        messageDate.Add(message.Date.LocalDateTime);
    }

    client.Disconnect(true);
}

运行此行时该方法意外存在且没有任何错误消息

var message = inbox.GetMessage(uid);

“消息”变量永远不会被填充

然而,收件箱变量被填充(仅在 getmessage 调用之后)并包含一个结果视图,其中包含来自电子邮件服务器的所有消息。

这向我保证连接信息是有效的。但是,如果没有错误消息,我就被卡住了。MimeKit 还有一个 Async 方法,我与 await 一起调用了它,以防结果需要时间返回。

更新下面是从上面生成的 imap.log 文件

Connected to imaps://mail.server.co.uk:993/
S: * OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE QUOTA SORT SORT=DISPLAY AUTH=PLAIN AUTH=LOGIN AUTH=CRAM-MD5 AUTH=DIGEST-MD5] Dovecot ready.
C: A00000000 AUTHENTICATE 
S: A00000000 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS SPECIAL-USE QUOTA SORT SORT=DISPLAY QUOTA] Logged in
C: A00000001 NAMESPACE
S: * NAMESPACE (("" ".")) NIL NIL
S: A00000001 OK Namespace completed.
C: A00000002 LIST "" "INBOX"
S: * LIST (\HasNoChildren) "." "INBOX"
S: A00000002 OK List completed.
C: A00000003 LIST (SPECIAL-USE) "" "*"
S: * LIST (\Sent) "." "Sent"
S: * LIST (\Drafts) "." "Drafts"
S: * LIST (\Trash) "." "Trash"
S: A00000003 OK List completed.
C: A00000004 EXAMINE INBOX (CONDSTORE)
S: * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: * OK [PERMANENTFLAGS ()] Read-only mailbox.
S: * 2 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 1] First unseen.
S: * OK [UIDVALIDITY 1506447851] UIDs valid
S: * OK [UIDNEXT 3] Predicted next UID
S: * OK [HIGHESTMODSEQ 2] Highest
S: A00000004 OK [READ-ONLY] Select completed.
C: A00000005 UID SEARCH RETURN () SUBJECT test
S: * ESEARCH (TAG "A00000005") UID ALL 1:2
S: A00000005 OK Search completed (0.011 secs).
C: A00000006 UID FETCH 1 (BODY.PEEK[])
S: * 1 FETCH (UID 1 BODY[] {5087}
S: Return-path: <email@email.com>
S: Envelope-to: email@email.com
S: Delivery-date: Tue, 26 Sep 2017 18:44:11 +0100
S: Received: from mail by mail114.server.co.uk with spamvirus-scanned (Exim 4.87)
S:  id 1dwttz-000381-Ox
S:  for email@email.com; Tue, 26 Sep 2017 18:44:11 +0100
S: X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on
S:  mail114.server.co.uk
S: X-Spam-Level: 
S: X-Spam-Status: No, score=0.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID,
S:  DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_SORBS_SPAM shortcircuit=no
S:  autolearn=disabled version=3.3.1
S: Received: from mail-wr0-f177.google.com ([209.85.128.177])
S:  by mail114.server.co.uk with esmtps (TLSv1.2:ECDHE-RSA-AES128-SHA:128)
S:  (Exim 4.87)
S:  id 1dwttz-00037Z-NC
S:  for email@email.com; Tue, 26 Sep 2017 18:44:11 +0100
S: Received: by mail-wr0-f177.google.com with SMTP id a43so13382310wrc.0
S:         for <email@email.com>; Tue, 26 Sep 2017 10:44:11 -0700 (PDT)
S: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
S:         d=gmail.com; s=20161025;
S:         h=message-id:mime-version:to:from:subject:date:importance;
S:         bh=kdGDYwbddyE9ukcooED6VhTd4ZHq47tHQNNwiXGeubc=;
S:         b=JR7erLZLcvTxAmFUkUUBLgPsaSWUicupakG44I3wGcBkOEij2pC/LjoHmBfXLHA/l/
S:          xhBxZ4YLgg/74YdBsGIusxAWAwVLUcqmXCQhG2xI8GLx2j8Kup/ttyV0RDb7+vYZjzOC
S:          PTOCfYsbE7zvx0dW5V64xoc0WSbXRpjAKQdzqCfnLZnjs7afdu19qTs/jOKQU5+aJ+H0
S:          lbEsiMKNlhxmB+n4KlJlE9Q3kiY7csZ3YXGXI0IdEX9LySmnM0Q90opOC1BSp7O/QqM2
S:          t0XrJj7hir/JjbncVgs/KEr6jilsVlzXurKoTqZBkH9/igzW0yDeDO9oHC/M/PLxRdxw
S:          zr4Q==
S: X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
S:         d=1e100.net; s=20161325;
S:         h=x-gm-message-state:message-id:mime-version:to:from:subject:date
S:          :importance;
S:         bh=kdGDYwbddyE9ukcooED6VhTd4ZHq47tHQNNwiXGeubc=;
S:         b=bgSxyHECPJ35wgGoir+gJV0RIvUhk3hX2RIR9sH+GgosiEsPoSisf6gst9d1P6Vj1D
S:          kXMfOD6A1KXukyLNga9gJ9T4X3IHUJSHYhWK5YLzYKpt9bQffTTtmb72Zg4xSpW0wAVp
S:          aQ9UFZ/6E6YPEzeIoRqKHZE9QqCEC/NUTzOaj3gaWRIjuyM2ROSoOGjLPIDYD1zyngM3
S:          R2IPtexajKUf2Zj9KiqlUaFh0GW1AEqvV6EEO1KxU+k8AZV7wzyjihgQfbolFSxtYyQU
S:          N9hYbSmfekCeHQEnl54JgWuCqpT/p9ZL0223ba2UA57iBQ8TwfagTDo8qNruCNsVB29m
S:          V+eA==
S: X-Gm-Message-State: AHPjjUgYzu3L/nWcZ6+sUeQCtxUXfrA48l09I7OK5vrVcpXZbcfAQ8E4
S:  gER31TV8XOOI/KWPcFBtucTfMaCR
S: X-Google-Smtp-Source: AOwi7QDf8N/N6/6ChsfOGgDRBdtq6KNFTU59ZkBCloLolhIL7pzvxKWWN8oGvpyRa1NuCPNx3rJCxA==
S: X-Received: by 10.223.176.13 with SMTP id f13mr8600118wra.220.1506447851022;
S:         Tue, 26 Sep 2017 10:44:11 -0700 (PDT)
S: Received: from ?IPv6:::ffff:192.168.0.103? (cpc81910-lon-2-0-cust393.1-1.cable.virginm.net. [83.11.115.178])
S:         by smtp.gmail.com with ESMTPSA id k18sm2616400wmd.22.2017.09.26.10.44.09
S:         for <crm@zebracars.co.uk>
S:         (version=TLS1_2 cipher=ECDHE-RSA-AER128-GCM-SHA256 bits=128/128);
S:         Tue, 26 Sep 2017 10:44:10 -0700 (PDT)
S: Message-ID: <59ca91ea.12591c0a.ed925.edb7@mx.google.com>
S: MIME-Version: 1.0
S: To: "" <>
S: From:  <>t: test
S: Date: Tue, 26 Sep 2017 18:44:10 +0100
S: Importance: normal
S: X-Priority: 3
S: Content-Type: multipart/alternative;
S:  boundary="_CE2445A7-C117-4ED4-B205-4C40AD703C0C_"
S: 
S: --_CE2445A7-C117-4ED4-B205-4C40AD703C0C_
S: Content-Transfer-Encoding: quoted-printable
S: Content-Type: text/plain; charset="utf-8"
S: 
S: test
S: 
S: Sent from Mail for Windows 10
S: 
S: 
S: --_CE2445A7-C117-4ED4-B205-4C40AD703C0C_
S: Content-Transfer-Encoding: quoted-printable
S: Content-Type: text/html; charset="utf-8"
S: 
S: <html xmlns:o=3D"urn:schemas-microsoft-com:office:office" xmlns:w=3D"urn:sc=
S: hemas-microsoft-com:office:word" xmlns:m=3D"http://schemas.microsoft.com/of=
S: fice/2004/12/omml" xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta ht=
S: tp-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8"><meta name=
S: =3DGenerator content=3D"Microsoft Word 15 (filtered medium)"><style><!--
S: /* Font Definitions */
S: @font-face
S:  {font-family:"Cambria Math";
S:  panose-1:2 4 5 3 5 4 6 3 2 4;}
S: @font-face
S:  {font-family:Calibri;
S:  panose-1:2 15 5 2 2 2 4 3 2 4;}
S: /* Style Definitions */
S: p.MsoNormal, li.MsoNormal, div.MsoNormal
S:  {margin:0cm;
S:  margin-bottom:.0001pt;
S:  font-size:11.0pt;
S:  font-family:"Calibri",sans-serif;}
S: a:link, span.MsoHyperlink
S:  {mso-style-priority:99;
S:  color:blue;
S:  text-decoration:underline;}
S: a:visited, span.MsoHyperlinkFollowed
S:  {mso-style-priority:99;
S:  color:#954F72;
S:  text-decoration:underline;}
S: .MsoChpDefault
S:  {mso-style-type:export-only;}
S: @page WordSection1
S:  {size:612.0pt 792.0pt;
S:  margin:72.0pt 72.0pt 72.0pt 72.0pt;}
S: div.WordSection1
S:  {page:WordSection1;}
S: --></style></head><body lang=3DEN-GB link=3Dblue vlink=3D"#954F72"><div cla=
S: ss=3DWordSection1><p class=3DMsoNormal>test</p><p class=3DMsoNormal><o:p>&n=
S: bsp;</o:p></p><p class=3DMsoNormal>Sent from <a href=3D"https://go.microsof=
S: t.com/fwlink/?LinkId=3D550986">Mail</a> for Windows 10</p><p class=3DMsoNor=
S: mal><o:p>&nbsp;</o:p></p></div></body></html>=
S: 
S: --_CE2445A7-C117-4ED4-B205-4C40AD703C0C_--
S: 
S: )
S: A00000006 OK Fetch completed.

堆栈跟踪如下

System.NullReferenceException: Object reference not set to an instance of an object.
   at ZebraCRM.Web.Controllers.LeadsController.GetEmail() in C:\Users\User\Documents\Visual Studio 2017\Projects\ZebraCRM\ZebraCRM.Web\Controllers\LeadsController.cs:line 186
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__25.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextResourceFilter>d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ResourceExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware`1.<Invoke>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.Web.BrowserLink.BrowserLinkMiddleware.<ExecuteWithFilter>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.<Invoke>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.<Invoke>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()
4

1 回答 1

1

您获得 a 的原因NullReferenceException是您正在初始化formEmailsnull不是new List<string> ()然后尝试将项目添加到空列表。

同样,您也需要初始化messageDatetonew List<DateTime> ()而不是null

于 2017-09-27T13:24:37.020 回答