Attanchment Not Extracted From Email

Hi Aspose Team,

When we are trying to load the email and extract attachments from it, for some email we are not able to
extract the attachment using MailMessage object.
The attachment is a csv file with the below content information.

Content-Type: text/plain; charset=“utf-8”
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=“1936909.csv”

It is not getting extracted either by using MailMessage.getAttachments() or by MailMessage.getLinkedResources(). Both are resulting into empty AttachmentCollection or LinkedResourceCollection respectively.

Can you help us ? We are using aspose email java(17.8)

Hello @hruser,

Can you please share the code sample and source file that has the problem?

Thanks.

Hi @dmitry.samodurov

The code is somthig like this :

AttachmentCollection attachmentCollection = message.getAttachments();
if(attachmentCollection != null) {
for(com.aspose.email.Attachment attachment : attachmentCollection) {
//process
}
}

LinkedResourceCollection linkedResourceCollection = message.getLinkedResources();
if(linkedResourceCollection != null) {
for(LinkedResource linkedResource : linkedResourceCollection) {
// process
}
}
The source eml file is confidential could we work with this given information?

Hello @hruser,

Сould you check AlternateViews.LinkedResources:

MailMessage message = MailMessage.load("message.eml", new EmlLoadOptions());
LinkedResourceCollection linkedResources = message.getLinkedResources(); 
AttachmentCollection attachments = message.getAttachments();
// >>>>
for (AlternateView alternateView : message.getAlternateViews()) {
    System.out.println("View (" + alternateView.getContentType() + ") Linked Resources Count is : " + alternateView.getLinkedResources().size());
}

For example:

The EML file has two multipart/alternative view parts, TXT and HTML.
HTML view part is multipart/related and contains linked attachment.

EML structure:

Content-Type: multipart/alternative; 
	boundary="----MAIN_ALTERNATIVE_BOUNDARY"

------MAIN_ALTERNATIVE_BOUNDARY
    // === TEXT ALTERNATIVE ===
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: quoted-printable
    
    The e-mail below is from an external source. Please do not open attachments=
    ...
    
------MAIN_ALTERNATIVE_BOUNDARY
    // === HTML ALTERNATIVE WITH INLINE ATTACHMENT ===
    Content-Type: multipart/related; 
    	boundary="----HTML_RELATED_BOUNDARY"
    
    ------HTML_RELATED_BOUNDARY
        // === HTML CONTENT ===
        Content-Type: text/html; charset=UTF-8
        Content-Transfer-Encoding: quoted-printable
        
        <html><head>
        ...
        
    ------HTML_RELATED_BOUNDARY
        // === INLINE LINKED ATTACHMENT WITH ID ===
        Content-Type: image/gif; name="image0.gif";
        Content-Disposition: inline; filename="image0.gif"
        Content-Transfer-Encoding: base64
        Content-ID: <DDA2E86A-BC74-46DE-BDF6-F42F526BC2BA-L0-001>
        
        iVBORw0KGgoAAAANSUhEUgAAANIAAAAzCAYAAADigVZlA
        ...
    ------HTML_RELATED_BOUNDARY--
    
------MAIN_ALTERNATIVE_BOUNDARY--

Hi @sergey.vivsiuk,
Tried the vesion of code you suggested
Didn’t receive any linked resources in the alternate views also
View (text/html; charset=“utf-8”) Linked Resources Count is : 0
View (text/plain; charset=“utf-8”) Linked Resources Count is : 0

but when I manually change the content from
Content-Type: text/plain; charset=“utf-8”
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=“1936909.csv”
to
Content-Type: text/plain; charset=“utf-8”
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=“1936909.csv”

I am able to extract the csv file. That is a manual eml file change which I want to avoid.

Hello @hruser,

Your issue has been accepted for investigation. We will contact you as soon as we have a result.

Thanks.

@hruser,

Could you share with us the structure of the EMAIL file without confidential data as I posted in the previous post?

Hello @sergey.vivsiuk

Will this work?

Content-Type: multipart/mixed;
 boundary="--_=_NextPart1_79c4d9b0-2c0d-498d-8e6f-ca8ea53c7501"

This is a multi-part message in MIME format.

----_=_NextPart1_79c4d9b0-2c0d-498d-8e6f-ca8ea53c7501
Content-Type: multipart/alternative; boundary="--_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda"


----_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8">
</head>
<body>
//data
</body>
</html>

----_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="71936909.csv"
//content
----_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda--

----_=_NextPart1_79c4d9b0-2c0d-498d-8e6f-ca8ea53c7501--

Hello @hruser,

Yes, the issue is reproduced with your code sample. We will contact you as soon as we have a result.

Thanks.

Hello @sergey.vivsiuk,

Can you provide an ETA for this?

Thanks.

Hello, We cannot give you exact dates.We will contact you as soon as we have a result.

Hello @hruser,

Email clients are ignoring the attachment due to format mismatch.
To troubleshoot this issue, it is important to ensure that the message is properly formatted using the multipart/alternative structure and Content-Disposition inline.

The multipart/alternative content type is used in email communication to provide alternative versions of the same message, allowing the recipient’s email client or program to choose the most suitable format for display. This is especially useful when the sender wants to accommodate different email clients with varying capabilities or preferences.

The basic idea behind multipart/alternative is to include multiple versions of the same content within a single email message. Each version is encapsulated within a separate body part, and the email client selects the most appropriate version based on its capabilities.

Typically, a multipart/alternative message consists of two or more body parts, each representing an alternative version of the content. For example, a common use case is to include both plain text and HTML versions of the email. The email client then chooses the appropriate version to display based on its capabilities. If the client can render HTML, it will display the HTML version; otherwise, it will fall back to displaying the plain text version.

Here’s an example of how a multipart/alternative message might look:

Content-Type: multipart/alternative; boundary="--_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda"

----_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8">
</head>
<body>
html body
</body>
</html>

----_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

QWxhZGRpbjpvcGVuIHNlc2FtZQ==,QmFzZTY0IGVuY29kaW5n

----_=_NextPart0_1dd69b19-ab5d-4884-b185-cd705afe8bda--

The Content-Disposition header with the value “inline” is used in MIME email messages to specify that the content should be displayed within the body of the message itself, rather than being treated as an attachment. This header is typically used when embedding images or other media files within an HTML email.

When “inline” is specified as the content disposition, the email client is expected to display the content directly within the email message. This allows the recipient to view the content seamlessly as part of the message, without having to open or download any attachments separately.

Hi @sergey.vivsiuk,
We get “inline” images as part of LinkedResourceCollection but we are not getting the csv as part of that.
Can we expect a solution for this?

@hruser
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): EMAILJAVA-35189

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.