Search code examples
c#asp.net-mvcmailkit

MailKit From Address


Im using mailKit in asp mvc core to collect email from a IMAP mailbox.

I return the message using the command

var message = inbox.GetMessage(uid) 

This returns all the results of the message. From here i want to access the sender email address (not including the name). After breakpointing on the above line i can see that the variable message has the following property

message
-From
--From(Array)
---From(item)
----Name (name of the sender)
----Address(email of the sender)

When referencing the above above using the message i am able to receive the name, however the address is not listed (within intelisence, nor will it build)

var name = message.From[0].Name.ToString()

Does anyone know why this would be visible as properties of the variable but not accessible via the code?

i simply want to

var name = message.From[0].Name.ToString()

Solution

  • The MimeMessage.From property is a InternetAddressList (more-or-less List<InternetAddress>).

    InternetAddress is an abstract base class for MailboxAddress and GroupAddress which only contains a Name property as you've discovered.

    In order to get the Address, you first need to cast it to a MailboxAddress... but only if it is actually a MailboxAddress or you'll get a cast exception.

    InternetAddressList has a convenience property called Mailboxes which can be used to iterate over a flattened list of the MailboxAddresses contained within.