I would like to ask you the following question:
I have two codes to read emails with their respective attachments that have:
The first one, I use the S22.IMAP dll:
using (ImapClient Client = new ImapClient(imap, 993, usuario, psw, AuthMethod.Login, true))
{
IEnumerable<uint> uids = Client.Search(SearchCondition.Unseen());//Correos no leĆdos
IEnumerable<MailMessage> messages = Client.GetMessages(uids, FetchOptions.Normal);
conexion.stringconeccion = stringconeccion;
conexion.conectar();
String ts = "start transaction";
MySqlCommand datos_ts = new MySqlCommand(ts, conexion.con);
datos_ts.ExecuteScalar();
DataTable dt_existeXML = new DataTable();
int insercion = 0;
foreach (MailMessage msg in messages)
{
foreach (Attachment atc in msg.Attachments)
{
if (System.IO.Path.GetExtension(msg.Attachments[0].Name) == ".xml")
{
String archivoXML_texto = "";
byte[] allBytes = new byte[msg.Attachments[0].ContentStream.Length];
int bytesRead = msg.Attachments[0].ContentStream.Read(allBytes, 0, (int)msg.Attachments[0].ContentStream.Length);
using (MemoryStream memory = new MemoryStream(allBytes))
{
StreamReader archivoXML = new StreamReader(memory);
archivoXML_texto = archivoXML.ReadToEnd();
archivoXML.Close();
memory.Dispose();
}
}
}
}
second code, using the MailKit DLL:
using (var client = new ImapClient ()) {
client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
client.Authenticate ("correo@gmail.com", "clave");
client.Inbox.Open (FolderAccess.ReadOnly);
var uids = client.Inbox.Search(SearchQuery.NotSeen);
foreach (var uid in uids)
{
var message = client.Inbox.GetMessage(uid);
foreach (var attachment in message.Attachments.OfType<MimePart>())
{
byte[] allBytes = new byte[attachment.Content.Stream.Length];
int bytesRead = attachment.Content.Stream.Read(allBytes, 0, (int)attachment.Content.Stream.Length);
string texto_definitivo = "";
String archivoXML_textoBase64 = "";
using (MemoryStream memory = new MemoryStream(allBytes))
{
StreamReader archivoXML = new StreamReader(memory);
archivoXML_textoBase64 = archivoXML.ReadToEnd();
byte[] temp_backToBytes = Convert.FromBase64String(archivoXML_textoBase64);
texto_definitivo = Encoding.ASCII.GetString(temp_backToBytes);
archivoXML.Close();
memory.Dispose();
}
}
}
client.Disconnect (true);
}
But I realize that there are attachments that do not read them, but also, I have noticed something in common of the attachments that are not read, do not have the attachment icon. But when I open the mail, I see that they have attachments:
For example the image that is marked with red, the attachment icon to its right is not shown. But when I open it, I verify that it has an attached document. Just those documents, either of the two codes, do not read the attachments.
My question are:
How could I also read those attachments? Should I configure or enable any option in the mail? What and how? Is it a mistake to send mail? How could it be solved?
UPDATE
I hope I have understood. The following code tried to obtain the text / content of the attachment for the cases that it could. I would like it if I'm wrong, please correct me.
public static void DownloadBodyParts ()
{
using (var client = new ImapClient ()) {
client.Connect ("imap.gmail.com", 993, SecureSocketOptions.SslOnConnect);
client.Authenticate("correo@gmail.com", "clave");
client.Inbox.Open(FolderAccess.ReadWrite);
var uids = client.Inbox.Search(SearchQuery.NotSeen);
foreach (var uid in uids)
{
var message = client.Inbox.GetMessage(uid);
var attachments = message.BodyParts.OfType<MimePart>().Where(part => !string.IsNullOrEmpty(part.FileName));
foreach (MimePart atch in attachments)
{
using (var memory = new MemoryStream())
{
atch.Content.DecodeTo(memory);
var buffer = memory.ToArray();
var text = Encoding.UTF8.GetString(buffer);
}
}
}
client.Disconnect (true);
}