¿Guardar archivos adjuntos usando la biblioteca MailKit?
Estoy tratando de aprender a usar la biblioteca MailKit pero estoy luchando por recuperar archivos adjuntos. Hasta ahora, mi código abrirá un buzón, revisará cada mensaje y almacenará datos como el remitente, el asunto, el cuerpo, la fecha, etc., pero no puedo manejar los archivos adjuntos.
He tratado de usar las soluciones de otras personas que se encuentran aquí, en github y otros sitios, pero todavía no entiendo exactamente qué están haciendo en su código y cuando me acerco a que una solución funcione, causa más errores, así que me estreso y borra todo el código. No quiero parecer perezoso, pero me encantaría que alguien pudiera explicar cómo puedo lograr esto. Básicamente estoy tratando de construir un cliente de correo para una aplicación de formularios web.
A continuación se muestra mi código, así que como pueden ver, no tengo ni idea :)
// 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;