We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Mail attachment name not retrieved after extraction

Hi,

We are using aspose code for extracting attachments from msg file.

Environment : Java

Issue : An msg file with attachment name сканирование 2.pdf after saving is named as 2.pdf . Can you please help us with this.

Code :

String tempFilePath = "D:\\Test\\";

String fileName = "FW Tajik Air.msg";

int index=0;

//Instantiate an MSG file to load an MSG file from disk

MailMessage message = MailMessage.

load(tempFilePath + fileName, MessageFormat.getMsg());

MapiMessage mapiMessage=MapiMessage.fromMailMessage(message);

for (index = 0; index < message.getAttachments().size(); index++) {

MapiAttachment att = (MapiAttachment) mapiMessage.getAttachments().get(index);

System.out.println("Attachment Name: " + att.getLongFileName());

String strMSGAttachmentName = att.getLongFileName().replace('\\', ' ').replace('/', ' ')

.replace(':', ' ').replace('?', ' ').replace('*', ' ').replace('"', ' ').replace('<', ' ')

.replace('<', ' ').replace('>', ' ').replace('|', ' ');

att.save(tempFilePath + strMSGAttachmentName);

}

Files : Are Attached

Hi,


Thank you for your inquiry.

I am able to extract the attachment with its proper file name from your provided email message. Please use the latest version of Aspose.Email for Java v1.1.0 and also check the below source code for your reference.

[Java]
MapiMessage mapiMessage = MapiMessage.fromFile(“C:\temp\FW Tajik Air.msg”);
for (int index = 0; index < mapiMessage.getAttachments().size(); index++)
{
MapiAttachment att = (MapiAttachment) mapiMessage.getAttachments().get(index);
System.out.println("Attachment Name: " + att.getLongFileName());
String strMSGAttachmentName = att.getLongFileName();
att.save(“C:\temp\” + strMSGAttachmentName);
}

In case of further queries, please feel free to write back
Hi,
Thank You for your quick reply. We are currently are having license only for Aspose.Network and the component you mentioned is Aspose.Email. Will license be valid ?

Hi,


Yes, your Aspose.Network license would be valid for Aspose.Email.

Hi ,

I am getting below error while using Aspose.Email jar files (aspose-email-1.1.0-jdk14.jar and aspose-email-1.1.0-jdk15) . Am i missing something ?

Line is throwed in line :

MapiMessage mapiMessage = MapiMessage.fromFile("C:\\temp\\FW Tajik Air.msg");

Exception in thread "main" java.lang.NoClassDefFoundError: org.testng.Assert
at com.aspose.email.private.k.a.a(Unknown Source)
at com.aspose.email.private.b.a.a(Unknown Source)
at com.aspose.email.ix.b(Unknown Source)
at com.aspose.email.ix.(Unknown Source)
at com.aspose.email.MapiMessageReader.(Unknown Source)
at com.aspose.email.MapiMessage.fromFile(Unknown Source)
at com.aon.ecm.component.AsposeEvalMsgToPdgWithAttAsposeCode.main(AsposeEvalMsgToPdgWithAttAsposeCode.java:42)
Caused by: java.lang.ClassNotFoundException: org.testng.Assert
at java.net.URLClassLoader.findClass(URLClassLoader.java:419)
at java.lang.ClassLoader.loadClass(ClassLoader.java:643)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:345)
at java.lang.ClassLoader.loadClass(ClassLoader.java:609)
... 7 more

Hi,


I am afraid, I am unable to replicate your said exception with Aspose.Email for Java v1.1.0. Also, I have noticed that the exception is thrown for the “org.testng.Assert” as the exception states “Caused by: java.lang.ClassNotFoundException: org.testng.Assert”.

My test application does not reference above mentioned class therefore I am unable to determine the cause of this exception.

Regards,

Hi,


I have had the same “java.lang.NoClassDefFoundError: org/testng/Assert” error and have narrowed it down to a conflict with the log4j jar (log4j-1.2.15.jar). Please advice as to how this should be resolved because log4j is a commonly used library.


Regards,

Hi ChEmailTest,


Can you please start a new thread and provide us a sample project for our review. We will look into this matter very soon.

[Edited] Please create the thread in Aspose.Email forum if you are using Aspose.Email for Java component.

Thank you.

The issues you have found earlier (filed as 32704) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)

Hi Sandachar,


We have fixed the problem that was discussed in this post. Please try out the latest version of Aspose.Email for Java v1.2.0 (link shared by the automatic notifier) and feed us back with your results.

Thank you.

Hi,

Yes the latest jar is working fine but has not yet resolved my problem.

One thing here, i cannot use the below code because after running this we get two attachment сканирование 2.pdf (expected) and pic19801.gif (Embeded image not required as attachment).

MapiMessage mapiMessage = MapiMessage.fromFile("C\\FW Tajik Air.msg");

for (index = 0; index < mapiMessage.getAttachments().size(); index++)

{

MapiAttachment att = (MapiAttachment) mapiMessage.getAttachments().get(index);

System.out.println("Attachment Name: " + att.getLongFileName());

String strMSGAttachmentName = att.getLongFileName();

att.save(tempFilePath + strMSGAttachmentName);

}

We got this below code to resolve the embeded image coming as attachment and here attachment name is still coming as 2.pdf -

MailMessage message = MailMessage.

load("C\\FW Tajik Air.msg", MessageFormat.getMsg());

MapiMessage mapiMessage=MapiMessage.fromMailMessage(message);

for ( index = 0; index < message.getAttachments().size(); index++)

{

MapiAttachment att = (MapiAttachment) mapiMessage.getAttachments().get(index);

System.out.println("Attachment Name: " + att.getLongFileName());

String strMSGAttachmentName = att.getLongFileName().replace('\\', ' ').replace('/', ' ')

.replace(':', ' ').replace('?', ' ').replace('*', ' ').replace('"', ' ').replace('<', ' ')

.replace('<', ' ').replace('>', ' ').replace('|', ' ');

att.save(tempFilePath + strMSGAttachmentName);

}

Hi,


Thank you for your feedback.

There is a way to identify in-line and regular attachments using the MapiMessage class. But I am afraid, this is not producing desirable results at the moment and I have logged an investigative ticket (Id: 33046) to look into this matter further. As soon as I receive any updates in this regard, I will post here for you. Below is source code for your reference,

Java
MapiMessage mapiMessage = MapiMessage.fromFile(strBaseFolder + “FW-Tajik-Air.msg”);
for (int index = 0; index < mapiMessage.getAttachments().size(); index++)
{
MapiAttachment att = (MapiAttachment) mapiMessage.getAttachments().get(index);
if(att.getProperties().contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_A) || att.getProperties().contains(MapiPropertyTag.PR_ATTACH_CONTENT_ID_W))
{
System.out.println("Inline, Embedded Attachment => Attachment Name: " + att.getLongFileName());
}
else
{
System.out.println("Regular Attachment => Attachment Name: " + att.getLongFileName());
String strMSGAttachmentName = att.getLongFileName();
att.save(strBaseFolder + strMSGAttachmentName);
}
}

For your workaround, I would like to suggest that you first save the message in OutlookMessageFormatUnicode by using the MailMessage class and then load it into an instance of MapiMessage. You can either use the Stream object to hold the intermediate data or you can save to disk. Doing so will ensure the correct file name for your extracted attachment. Below is a sample,

Java
MailMessage message = MailMessage.load(strBaseFolder + “FW-Tajik-Air.msg”, MessageFormat.getMsg());
message.save(“output.msg”, MailMessageSaveType.getOutlookMessageFormatUnicode());
MapiMessage mapiMessage=MapiMessage.fromFile(“output.msg”);
for (int index = 0; index < message.getAttachments().size(); index++)
{
MapiAttachment att = (MapiAttachment) mapiMessage.getAttachments().get(index);
System.out.println(“Attachment Name: " + att.getLongFileName());
String strMSGAttachmentName = att.getLongFileName().replace(’\’, ’ ‘).replace(’/’, ’ ‘)
.replace(’:’, ’ ‘).replace(’?’, ’ ‘).replace(’*’, ’ ‘).replace(’”’, ’ ‘).replace(’<’, ’ ‘)
.replace(’<’, ’ ‘).replace(’>’, ’ ‘).replace(’|’, ’ ');
att.save(strBaseFolder + strMSGAttachmentName);
}

The issues you have found earlier (filed as NETWRKJAVA-33046) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.