Attachment filename with strange Characters


#1

Hi,

we downloaded an EML from mail provider. When try to extract attachments we got a strange filename: the “à” char was converted into “�” ones.

Below the snippet of EML, containing the filename:

------=_NextPart_000_0067_01D4E86C.C73B95F0
Content-Type: application/octet-stream;
name="=?utf-8?Q?Comunicazione_Cambio_Indirizzo_P?=
=?utf-8?Q?EC_Cilento_Reti_Gas=5FSociet=EF=BF=BD_di_V?=
=?utf-8?Q?endita.pdf?="
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="=?utf-8?Q?Comunicazione_Cambio_Indirizzo_P?=
=?utf-8?Q?EC_Cilento_Reti_Gas=5FSociet=EF=BF=BD_di_V?=
=?utf-8?Q?endita.pdf?="

I expect to have a filename like “Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Società di Vendita.pdf” instead of “Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Societ� di Vendita.pdf”.
It seems that “Societ=EF=BF=BD” is not correctly converted into string. Maybe some Encoding conversion issues?

We are using Aspose.EMail v. 19.3.0.

Thanks


#2

@gdiedoardo,

Can you please share source file along with sample code so that we may further investigate to help you out.


#3

Hi,

This is the EML file.
234069.zip (179.5 KB)

In my DB I have this:
image.png (2.9 KB)

And this is the test code source:

private void TestEMLBis()
{
  byte[] EML = File.ReadAllBytes(@"c:\temp\234069.eml");

  EmlLoadOptions lo = new EmlLoadOptions();
  lo.PreserveTnefAttachments = true;
  lo.PrefferedTextEncoding = Encoding.UTF8;

  MailMessage mm;

  using (MemoryStream ms = new MemoryStream(EML))
  {
    mm = MailMessage.Load(ms, lo);

    Attachment att = mm.Attachments.FirstOrDefault(a => a.Name.ToLower() == "postacert.eml".ToLower());

    MailMessage messaggioCertificato = MailMessage.Load(att.ContentStream, lo);

    //Here att2 is null because "Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Societ? di Vendita.pdf" is different from “Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Societ� di Vendita.pdf”
    Attachment att2 = messaggioCertificato.Attachments.FirstOrDefault(a => a.Name.ToLower() == "Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Societ? di Vendita.pdf".ToLower());
  }
}

Remember that the character that should have been saved is this: “à”
Thank you


#4

@gdiedoardo,

I have used following statement for extracting the attachment and it worked on my end. Can you please try using the same on your end.

        Attachment att2 = messaggioCertificato.Attachments.FirstOrDefault(a => a.Name.ToLower() == "Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Societ� di Vendita.pdf".ToLower());

#5

Hi,

this is not the problem. The test source code above is for understand the problem. In our system the mail is saved on DB in this way:

          client.SaveMessage(msg.UniqueId, $@"{messagePath}\{msg.UniqueId}.eml");
          byte[] imapRawMessage;
          MailMessage imapParsedMessage;
          using (MemoryStream ms = new MemoryStream())
          {
            client.SaveMessage(msg.UniqueId, ms);
            imapRawMessage = ms.ToArray();
            imapParsedMessage = MailMessage.Load(ms);
          }

          //Controlla la presenza nell'intestazione del campo X-Ricevuta
          if (string.IsNullOrEmpty(imapParsedMessage.Headers["X-Ricevuta"])) //Se il campo non è presente, allora si tratta di una mail
          {
            childLogger.LogPecController(taskID, $"Messaggio identificato come POSTA IN INGRESSO", LogControllerModelDirezioneEnum.Ricezione, mbx.Mailbox.ID, idMail);
            //Genera il model della mail da salvare
            MailModel mailToSave = childMailService.CreateModelFromEML(imapRawMessage);
            mailToSave.DataRicezione = DateTime.Now;
            mailToSave.ServerUniqueID = idMail;

            //Imposta la cartella in cui salvarla
            switch (mailToSave.EnumTipoMail)
            {
              case MailModelTipoMailEnum.Certificata: //Nel caso di posta certificata
                childLogger.LogPecController(taskID, $"Messaggio di posta certificata", LogControllerModelDirezioneEnum.Ricezione, mbx.Mailbox.ID, idMail);
                mailToSave.FolderID = childFolderService.GetFolderByRole(FolderModelRuoloEnum.Inbox).ID;
                mailToSave.IsCertificata = true;
                break;

              case MailModelTipoMailEnum.NoCertificata: //Nel caso di posta non certificata
                childLogger.LogPecController(taskID, $"Messaggio di posta non certificata", LogControllerModelDirezioneEnum.Ricezione, mbx.Mailbox.ID, idMail);
                mailToSave.FolderID = childFolderService.GetFolderByRole(FolderModelRuoloEnum.NotCertified).ID;
                mailToSave.IsCertificata = false;
                break;
            }

            childMailService.Save(mailToSave);
            newID = mailToSave.ID;

where class of “client” is Aspose.Email.Client.Imap.ImapClient.

The problem is that on the DB the name of the attachment is this:

“Comunicazione Cambio Indirizzo PEC Cilento Reti Gas_Societ? di Vendita.pdf”

Therefore the equality between the two strings is not verified.

We cannot change the EML, It seems that “Societ=EF=BF=BD” is not correctly converted into string.
The correct string should be “Società”, neither “Societ?” nor “Societ�”.

How can we convert correctly the char?

Thank you


#6

@gdiedoardo,

I have further investigated the issue on our end and an issue with ID EMAILNET-39437 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.