MailKit From Address

£可爱£侵袭症+ 提交于 2021-01-27 12:08:53

问题


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

回答1:


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.




回答2:


You can use this code block.

message.From.OfType<MailboxAddress>().Single().Address;



来源:https://stackoverflow.com/questions/46474030/mailkit-from-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!