Free Support Forum - aspose.com

MapiTask creation from mimeContent throwing Exception

Hello,

I’m trying to create a MapiTask using the following code:

InputStream stream = new ByteArrayInputStream(rawTaskDecoded,0,rawTaskDecoded.length);
				
MailMessage mailMessage = MailMessage.load(stream);
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage);

MapiTask task1 = (MapiTask) mapiMessage.toMapiMessageItem();

When running it, the following exception is happening:

com.aspose.email.MapiMessage cannot be cast to com.aspose.email.MapiTask

I can create a MapiNote using this code, but when using it to create a MapiTask it does not work.
What can I do to solve this?

Thanks

@kelberuc

I suggest you to please visit this documentation link for your kind reference. I hope the shared information will be helpful.

@mudassir.fayyaz

I’m afraid the code in the shared documentation link is throwing exception too

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
MapiMessage msg = MapiMessage.FromFile(dataDir + "Contact.msg");
MapiContact mapiContact = (MapiContact)msg.ToMapiMessageItem();

The above code throws the same exception as mine.

com.aspose.email.MapiMessage cannot be cast to com.aspose.email.MapiContact

I’m using Aspose.Email for Java 20.9

@kelberuc

Can you please be kind enough to share the source file so that I may try reproducing this on our end and help you out.

Hi @mudassir.fayyaz,

Please find below my code:

File file = new File("MimeContent");
InputStream stream = new FileInputStream(file);
MailMessage mailMessage = MailMessage.load(stream);
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage);
MapiTask task1 = (MapiTask) mapiMessage.toMapiMessageItem();

MimeContent.zip (501 Bytes)

Thanks

@kelberuc

I have created an issue with ID EMAILJAVA-34752 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.

I am having the same issue that kelberuc reported above. Is there any update on resolving this?

In the meantime I have concluded the only way to proceed is by creating a MapiTask and then invoking the setter methods for the fields not populated by the constructor (since my source data is not in .ics format).

@cdeangelis

The issue was fixed with need of addingTask information to MimeContent:

// load MIME content
MailMessage mailMessage = MailMessage.load("MimeContent");

// prepare and add task to MAIL message
Task task = new Task();
task.setMethod(TIPMethod.Request);
task.setOrganizer(new MailAddress("org@host.com"));
task.setSubject("subject");
task.setBody("Body");
java.util.Calendar cal = java.util.Calendar.getInstance(); 
task.setStartDate(cal.getTime());
cal.add(java.util.Calendar.DATE, 1);
task.setDueDate(cal.getTime());
MailAddressCollection attendees = new MailAddressCollection();
attendees.add("att1@host.com");
task.setAttendees(attendees);
mailMessage.addAlternateView(task.request());

// convert to MAPI
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage);
MapiTask task1 = (MapiTask) mapiMessage.toMapiMessageItem();

Thank you for the reply. But if you need to create a Task object and populate it before converting it in this way, I do not understand the advantage over creating the MapiTask object via its constructor & invoking any necessary setters.

@cdeangelis

Actually, the solution was shared with regards to usages scenario shared by @kelberuc above. The MimeContent.zip attached to the ticket did not contain any task data and we have shared code how to add Task info to empty MailMessage.

If you ought to add using MapiTask, please visit this documentation link.