I'm trying to make a program to recover email attachments, I'm using the IMAPX2 library and a self-mail test account on my own mail server, the topic is programmed in c #, in fact it's my first program in c #.
I have been able to retrieve information from emails such as the one that sent it, the recipient, the subject, the body of the email, even the attachments embedded in the body (images in HTML template), but the array of attachments is always empty.
Below I put the code, I do not know what to do to get the attachments in the array of attachments that always appears empty, it would be great if someone could help me out, thank you very much
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using ImapX;
using ImapX.Collections;
using ImapX.Enums;
using ImapX.Parsing;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ImapX.ImapClient client = new ImapClient("mailhost", true);
client.IsDebug = true;
client.Behavior.AutoPopulateFolderMessages = true;
client.Behavior.MessageFetchMode = MessageFetchMode.Full;
if (client.Connect())
{
Console.WriteLine("conectado");
if (client.Login("[email protected]", "test"))
{
// login successful
Console.WriteLine("login ok");
// MAIL SEARCH
foreach (ImapX.Message m in client.Folders["INBOX"].Search("ALL", MessageFetchMode.Full,-1))
{
try
{
Console.WriteLine("reading email from" + m.From.DisplayName + m.From.Address + "TO:" + m.To[0].DisplayName + " " + m.To[0].Address);
Console.WriteLine("subject:" + m.Subject);
m.Download(MessageFetchMode.Full,true);
foreach (var adjunto in m.Attachments) {
Console.WriteLine("Adjunto:"+ adjunto.FileName);
}
var attachments = m.BodyParts; /downloading embbebed attachments
if (attachments.Count() > 0)
{
foreach (var adjunto in attachments)
{
Console.WriteLine("content type:" + adjunto.ContentType + " Descripcion: " + adjunto.Description + "parameters:" + adjunto.Parameters);
adjunto.Download(); // Downloading the attachment
//*****************************************************************************************
string path = @"c:\temp\MyTest.txt";
// This text is added only once to the file.
if (!File.Exists(path))
{
// Create a file to write to.
string createText = utf8.GetString(Convert.FromBase64String(adjunto.ContentStream));
File.WriteAllText(path, createText);
}
else {
Console.WriteLine("El fichero ya existe");
}
// This text is always added, making the file longer over time
// if it is not deleted.
// Open the file to read from.
string readText = File.ReadAllText(path);
Console.WriteLine(readText);
//******************************************************************************************
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error while reading email. " + ex.Message);
}
}
}
else
{
//lgin no sucesfull
Console.WriteLine("no logueado");
}
}
else {
// connection not successful
Console.WriteLine("error en conexion");
}
Console.ReadKey();
}
}
}