This project is read-only.

Cannot save Attachements

Topics: Issues
Jan 14, 2014 at 12:00 PM
Hi,

I am using the latest build of ImapX, trying to retrieve .txt attachments from an outlook box.

When trying to save the attachment I get:

System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64_Decode


on a trace a see that the exception is thrown on attachment.FileData

The code I use is as per the documentation:
                         foreach (var attachment in msg.Attachments)
                                       {
                                          
                                           attachment.Download();
                                      
                                           attachment.Save(@"C:\Files\");
                
                                       }
Jan 14, 2014 at 3:20 PM
Hi ,

I am just trying the ImapX library and it seems to be very useful.

I do have exactly the same problem as mwcfm and we use obviously the same code as in the documentation.

What is funny is that I am able to download few files first and at some point it crashes.

Here is the stack trace
at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64CharArray(Char[] inArray, Int32 offset, Int32 length)
at System.Security.Cryptography.FromBase64Transform.TransformBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[] outputBuffer, Int32 outputOffset)
at ImapX.EncodingHelpers.Base64.FromBase64(String s) in c:\Users\Pavel\Projects\ImapX\ImapX\EncodingHelpers\Base64.cs:line 105
at ImapX.Attachment.get_FileData() in c:\Users\Pavel\Projects\ImapX\ImapX\Attachment.cs:line 47
at ImapX.Attachment.Save(String folder, String fileName) in c:\Users\Pavel\Projects\ImapX\ImapX\Attachment.cs:line 118
at XXXAutomaticProcessing.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\jleconte\Documents\XXX\XXXAutomaticProcessing\XXXAutomaticProcessing\Form1.cs:line 55
Jan 15, 2014 at 12:24 AM
Ok it only occurs in one email. Since I am processing emails coming from a specific server it does not really matter.

Thank you for this great library.
I got some troubles to understand at some point that attached files could be as well in embedded resources. Anyway I ve figured it out and everything is working perfectly. Thanks again.
Coordinator
Jan 15, 2014 at 12:03 PM
Hi mwcfm, jleconte,

thank you! I will take a look at what the possible reason for the exception could be.

To jleconte: The attachments are split into two categories, usual attachments (See Message.Attachments) and inline attachments (See Message.EmbeddedResources), such as inline images. This is done for better usability.

Greets,

Pavel
Jan 16, 2014 at 8:11 AM
Hi Pavel,

Thanks for your prompt response.

Have you had time to check out the possible problem?

If I cannot get this working with ImapX in the next day, I would have to use an alternative, which is not ideal.

Thanks
Coordinator
Jan 16, 2014 at 9:10 AM
Hi mwcfm,

yes, I took a quick look over it, however it would be helpful if I could get some test credentials. Is it possible? You can send them to p13a92@gmail.com

Greets,

Pavel
Feb 25, 2014 at 4:38 PM
Hey Pavel,

Thanks for the great library.

I'm also getting this error my code is :
foreach (var attachment in currentMessage.Attachments)
{
    attachment.Download();
    attachmentName = attachment.FileName;
    if (!Directory.Exists(fullPath))
        Directory.CreateDirectory(fullPath);
    attachment.Save(fullPath);                               
}
Looking at the attachment properties I also see that FileData and FileSize throw an exception of type 'System.FormatException' byte[] {System.FormatException}
with base {"The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. "} System.SystemException {System.FormatException}

The attachment I'm trying to save is a small png file I'm just using to test whether or not saving attachments work.
Feb 28, 2014 at 1:55 PM
Hi,

Just to let you know: I had the same problem today. I couldn't retrieve the attachements. Here's what I did:

In Base64.cs on line 92 I added:
s = s.Replace("* 1 FETCH (", string.Empty);

int outlookInt = s.IndexOf("UID");
if (outlookInt > 5)
{
    s = s.Substring(0, outlookInt);
}
The problem is: there was a ( added to the first part: (JVBERi0x. And at the end was the text: UID 75. So that results in a bad base64 string.

The fix is very ugly, but for a friday, it works.

SIncerely,

Johannes
Coordinator
Apr 8, 2014 at 8:18 PM
Good evening!

Please have a look at the latest code in the repository. Various fixes have been done. I'd be happy if you can test it and give some feedback!

Best regards,

Pavel
Feb 21, 2015 at 2:00 PM
Was this problem ever fixed? I'm getting the same error (stack trace below) when I try to save attachments sent via GMail, Yahoo, etc. I have the 5/1/2014 version of ImapX.dll so I'm wondering if the problem was not fixed, or if I might be doing something wrong. My download/save code is the same as the original post above so I'm thinking it might not be me.
   at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)
   at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
   at System.Convert.FromBase64CharArray(Char[] inArray, Int32 offset, Int32 length)
   at System.Security.Cryptography.FromBase64Transform.TransformBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[] outputBuffer, Int32 outputOffset)
   at ImapX.EncodingHelpers.Base64.FromBase64(String s) in c:\Users\Pavel\Projects\ImapX\ImapX\EncodingHelpers\Base64.cs:line 107
   at ImapX.Attachment.get_FileData() in c:\Users\Pavel\Projects\ImapX\ImapX\Attachment.cs:line 47
   at ImapX.Attachment.Save(String folder, String fileName) in c:\Users\Pavel\Projects\ImapX\ImapX\Attachment.cs:line 136
   at WindowsApplication1.clsEmailMessages.saveMessageAttachments(Int32 dbID, String[] FileToSaveList, String SaveToFolder, Boolean AutoOverwrite) in C:\Users\Rick\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\clsEmailMessages.vb:line 170
   at WindowsApplication1.SaveAttachments.btnSave_Click(Object sender, EventArgs e) in C:\Users\Rick\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\SaveAttachments.vb:line 67
Coordinator
Feb 22, 2015 at 1:37 PM
Hi rickgriffith,

there were some fixes for this issue after the release on May 1st 2014. Take a look at the latest source code: https://imapx.codeplex.com/SourceControl/latest

If you don't want to compile the library yourself, just let me know which .Net version you are using.

Greets,

Pavel
Feb 24, 2015 at 1:49 AM
That did the trick. I was able to download and compile the source code and all seems well.

Thanks Pavel!

Rick
Mar 6, 2015 at 8:13 PM
Павел, добрый вечер!
Очень понравилась ваша библиотека!

Но эта же ошибка у меня, скачал исходный код последний скомпилировал, но ошибка осталась
System.FormatException: Входные данные не являются действительной строкой Base-64, поскольку содержат символ в кодировке, отличной от Base 64, больше двух символов заполнения или недопустимый символ среди символов заполнения. 
System.FormatException: 
   в System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)
   в System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
   в System.Convert.FromBase64CharArray(Char[] inArray, Int32 offset, Int32 length)
   в System.Security.Cryptography.FromBase64Transform.TransformBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[] outputBuffer, Int32 outputOffset)
   в ImapX.EncodingHelpers.Base64.FromBase64(String s)
   в ImapX.EncodingHelpers.StringDecoder.DecodeBase64(String value, Encoding encoding)
   в ImapX.EncodingHelpers.BodyDecoder.DecodeMessageContent(MessageContent content)
   в ImapX.MessageBody.get_Text()
   в MailDetali.MailReader(String Server, Int32 Port, String User, String Pass, Int32 Command)
Не подскажите что делать?
Aug 31, 2015 at 2:47 PM
Edited Sep 1, 2015 at 4:05 PM
Can I ask you for help about this issue with attachment !
Is there any in line solution, I mean without changing the source code ? I currently don't know how to compile the source...if you can give some guides...
                            fl_at.Download();
                            try
                            {
                                fl_at.Save(folderpath); 
// gives me an  The input is not a valid Base-64 string as it contains a non- 
//base 64 character, more than two padding characters, or an illegal character among the padding characters.
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Error : " + ex.Message);
                            }
I am connecting to the server, reading all msgs, but in current case I am trying to download a .pdf attachment and I get this error!
C# VS Express 2013-Desktop/ WIN10,64BIT

EDIT: I re-build the libs, remove the ImapX installation from the nuget and manually added the newly created .dll so now it's ok...following JKMurray advise...Thanks.