PST | Not able to convert .ics files to PST | Proxy error(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

Not able to convert .ics file to PST format using aspose lib getting below error.

C:\Users\Tejas>python calendarissue.py
Traceback (most recent call last):
File “mailissue.py”, line 57, in
run()
File “mailissue.py”, line 49, in run
appointment = Appointment.load(f.file_directory)
Proxy error(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.

Code

from aspose.email import MailMessage
from aspose.email.mime import HeaderCollection
import aspose.email.mapi.msg as msg
from aspose.email.mapi import MapiMessage, MapiContact
from aspose.email.personalinfo.vcard import VCardContact
import aspose.email as ae
import os
import shutil
import tempfile
from aspose.email.calendar import Appointment

class TempFileManager:
    """
    Manager that creates a file in temporary directory. Removes temporary directory on exit.
    """

    def __init__(self, file_content, file_name):
        self.content = file_content
        self.file_name = file_name

    def __enter__(self):
        self.folder_directory = tempfile.mkdtemp()
        self.file_directory = os.path.join(self.folder_directory, self.file_name)
        with open(self.file_directory, "w+", encoding="utf-8-sig") as f:
            f.write(self.content)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        shutil.rmtree(self.folder_directory)



def prepare_file(file_dir):
	with open(file_dir, "r+", encoding="utf-8-sig") as f:
		readed_file = f.read()
		readed_file = readed_file.replace("ACTION:Display\n", "ACTION:DISPLAY\n")
	return readed_file

def run():

	#dataDir = "Data/"
	#ExStart: LoadingContactFromVCardWithSpecifiedEncoding
	#print(MailMessage.load('Undeliverable_ repro test-1727695419 (2).eml'))
	#result = MapiMessage.from_mail_message(MailMessage.load('Undeliverable_ repro test-1727695419 (2).eml'))
	#print(result)
	#ExEnd: LoadingContactFromVCardWithSpecifiedEncoding
	prepared_file = prepare_file('testinvalidmail.ics')
	with TempFileManager(prepared_file, os.path.basename('testinvalidmail.ics')) as f:
  		appointment = Appointment.load(f.file_directory)
	mail_message = ae.MailMessage()
	mail_message.add_alternate_view(appointment.request_apointment())
	mapi_message = MapiMessage.from_mail_message(mail_message)
 

	
if __name__ == '__main__':
    run()

@TejasA

The error you’re encountering suggests that the .ics file you are trying to load may not be formatted correctly or may contain invalid characters. Here are several steps you can take to troubleshoot and resolve the issue:

  1. Check the .ics File Format: Ensure that the .ics file adheres to the iCalendar format specifications. You can validate the file using an iCalendar validator tool.

  2. Encoding Issues: Verify that the file is saved with the correct encoding (UTF-8 without BOM). Open the file in a text editor and re-save it with the appropriate encoding if necessary.

  3. Modify the Code: Consider adding error handling around the Appointment.load() method to catch and log any specific issues that arise during loading. This can help identify if the problem lies within the file content.

  4. Sample Code Adjustment: Here’s a modified version of your run() function that includes error handling:

    def run():
        prepared_file = prepare_file('testinvalidmail.ics')
        with TempFileManager(prepared_file, os.path.basename('testinvalidmail.ics')) as f:
            try:
                appointment = Appointment.load(f.file_directory)
                mail_message = ae.MailMessage()
                mail_message.add_alternate_view(appointment.request_appointment())
                mapi_message = MapiMessage.from_mail_message(mail_message)
            except Exception as e:
                print(f"Error loading appointment: {e}")
    
  5. Debugging: If the issue persists, consider logging the content of the .ics file before loading it to identify any obvious formatting issues.

If you continue to experience problems, please provide more details about the content of the .ics file or any specific lines that might be causing the issue.

Can we checking further on this ? We have checked all the above things.

Yes, we can check, but could you please provide the original ICS?