Сохранение вложений с помощью библиотеки MailKit?
Я пытаюсь узнать, как использовать библиотеку MailKit, но я пытаюсь получить вложения. Пока мой код открывает почтовый ящик, просматривает каждое сообщение и хранит такие данные, как отправитель, тема, тело, дата и т. Д., Но я не могу иметь дело с вложениями.
Я пытался использовать решения других людей, найденные здесь, на github и других сайтах, но я до сих пор не понимаю точно, что они делают в своем коде, и когда я приближаюсь к тому, чтобы заставить работать решение, это вызывает больше ошибок, поэтому я подчеркиваю и удалите весь код. Я не хочу показаться ленивым, но я был бы рад, если бы кто-нибудь объяснил, как мне этого добиться. Я в основном пытаюсь создать почтовый клиент для приложения веб-форм.
Ниже мой код, так что, как вы можете видеть, я довольно невежественен :)
// Open the Inbox folder
client.Inbox.Open(FolderAccess.ReadOnly, cancel.Token);
//get the full summary information to retrieve all details
var summary = client.Inbox.Fetch(0, -1, MessageSummaryItems.Full, cancel.Token);
foreach (var msg in summary)
{
//this code originally downloaded just the text from the body
var text = msg.Body as BodyPartText;
//but I tried altering it so that it will get attachments here also
var attachments = msg.Body as BodyPartBasic;
if (text == null)
{
var multipart = msg.Body as BodyPartMultipart;
if (multipart != null)
{
text = multipart.BodyParts.OfType<BodyPartText>().FirstOrDefault();
}
}
if (text == null)
continue;
//I hoped this would get the messages where the content dispositon was not null
//and let me do something like save the attachments somewhere but instead it throws exceptions
//about the object reference not set to an instance of the object so it's very wrong
if (attachments.ContentDisposition != null && attachments.ContentDisposition.IsAttachment)
{
//I tried to do the same as I did with the text here and grab the body part....... but no
var attachedpart = client.Inbox.GetBodyPart(msg.Index, attachments, cancel.Token);
}
else
{
//there is no plan b :(
}
// this will download *just* the text
var part = client.Inbox.GetBodyPart(msg.Index, text, cancel.Token);
//cast main body text to Text Part
TextPart _body = (TextPart)part;