PowerMan2015
PowerMan2015

Reputation: 1418

MimeKit Exits Unexpectedly

Im attempting to us MimeKit to retrieve a list of unread emails on a IMAP server

Here is my code

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 protected]", "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);
}

The method exists unexpectedly without any error messages while running this line

var message = inbox.GetMessage(uid);

The "message" variable is never populated

However the inbox variable is populated (only after the getmessage call) and contains a results view with all the messages from the email server.

This assures me that the connection information is valid. However without an error message to go off I'm stuck. The MimeKit also has a Async method which I have called along with await, in case the results needed time to return.

UPDATE Below is the imap.log file produced from the above

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 protected]>
S: Envelope-to: [email protected]
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 protected]; 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 protected]; Tue, 26 Sep 2017 18:44:11 +0100
S: Received: by mail-wr0-f177.google.com with SMTP id a43so13382310wrc.0
S:         for <[email protected]>; 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 <[email protected]>
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: <[email protected]>
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.

stack trace is as follows

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()

Upvotes: 0

Views: 511

Answers (1)

jstedfast
jstedfast

Reputation: 38593

The reason that you are getting a NullReferenceException is that you are initializing formEmails to null instead of new List<string> () and then trying to add items to a null list.

Likewise, you'll need to initialize messageDate to new List<DateTime> () instead of null as well.

Upvotes: 1

Related Questions