Cannot get MS Teams Emoji, Sticker and GIFs (or Giphy) as Attachment

Samples.zip (98.4 KB)

Aspose Team,

We use the Aspose Email java package to extract attachments from msg files. Some msg files contain MS Teams Emoji, Sticker and GIFs (or Giphy). Their links are extracted as part of the email body(see below), but the images are not treated as attachment and thus not downloaded.

Emoji:

Sticker:

Giphy:

Wonder if you can treat them as ReferenceAttachment with URI set with the link so that we can easiely find them and download them with the URI.

Following is the sample code and attached are three sample files. The operating system is Ubuntu 20.04. Java version is 21. Aspose Email java packages is 24.7.

Sample code:

import com.aspose.email.Attachment; 
import com.aspose.email.LinkedResource; 
import com.aspose.email.MailMessage; 
import com.aspose.email.MsgLoadOptions; 

public class ExtractEmojiStickerGiphy_32 { 

    public static void main(String[] args) { 
        try { 
            String filepath = "/home/ubuntu/winshare/00_Sample_Files/Msg_with_Emoji_Sticker_Giphy/6_with_att_pic.msg"; 

            String outputDir = "/home/ubuntu/winshare/00_Sample_Files/Msg_with_Emoji_Sticker_Giphy/outdir/"; 

            // Open message 
            MailMessage mailMessage = MailMessage.load(filepath, new MsgLoadOptions()); 

            // Extract test 
            String emailBody; 
            if (mailMessage.isBodyHtml()) { 
                emailBody = mailMessage.getHtmlBodyText(true); 
            } else { 
                emailBody = mailMessage.getBody(); 
            } 
            if (emailBody != null) { 
                System.out.println(emailBody); 
            } else { 
                System.out.println("Failed to get email body"); 
            } 

            // Save attachments 
            for (int i = 0; i < mailMessage.getAttachments().size(); i++) { 
                Attachment attachment = mailMessage.getAttachments().get_Item(i); 
                String outputFile = outputDir + "attachment_" + i; 
                attachment.save(outputFile); 
            } 

            // Save embedded objects 
            for (int i = 0; i < mailMessage.getLinkedResources().size(); i++) { 
                LinkedResource attachment = mailMessage.getLinkedResources().get_Item(i); 
                String outputFile = outputDir + "embedded_" + i; 
                attachment.save(outputFile); 
            } 
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
}

Hello @xyang,

Welcome to our support forum!

These items are extracted as part of the email body rather than being treated as attachments, which makes it difficult to download them.
You might need to implement a custom solution to parse the email body for these links and handle their download separately, as they are not treated as standard attachments.

@margarita.samodurova Wonder if you can treat them as standard attachments so that we do not need to parse the email body ourselves.

@xyang ,

Unfortunately, we are unable to treat external links to resources in the email body as attachments.
However, this can be implemented using Java by parsing the email body to extract and handle img links.

Here’s an example of how you might achieve this:

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

static void extractAndSaveLinks(String htmlContent, String outputDir) throws IOException {
    Document doc = Jsoup.parse(htmlContent);
    Elements links = doc.select("img[src], a[href]"); // Select image and link elements

    for (Element link : links) {
        String url = link.absUrl("src");
        if (url.isEmpty()) {
            url = link.absUrl("href");
        }
        if (!url.isEmpty()) {
            saveUrlToFile(url, outputDir + getFileNameFromUrl(url));
        }
    }
}

static void saveUrlToFile(String urlString, String filePath) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = connection.getInputStream().read(buffer)) != -1) {
            fos.write(buffer, 0, bytesRead);
        }
    }
}

static String getFileNameFromUrl(String url) {
    Pattern pattern = Pattern.compile(".*/(.*)");
    Matcher matcher = pattern.matcher(url);
    return matcher.find() ? matcher.group(1) : "default.png";
}

@margarita.samodurova Thank you very much. We will try the suggested method.

@xyang ,

Please spare a minute to share your feedback.
Thank you.

@margarita.samodurova I got a chance to test the suggested code. It works. Thank you very much. Sorry for the late reply.

@xyang ,

We are glad to hear that the code worked for you! If you have any further questions or need assistance, feel free to reach out!

@margarita.samodurova ,

In addition to downloading Emoji, Sticker and Giphy images, we are interested in retrieving the related information like who did the reaction and when did it happen. It is mentioned that MapiReactionsBlob and ReactionsSummary contain those information in this article: Reactions in Outlook: Public usability update September 2023 - Microsoft Community Hub. Wonder if you can help us to get those information.

Hello @xyang,

We have opened the following new investigation ticket in our internal issue tracking system:

Issue ID(s): EMAILNET-41430

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

@margarita.samodurova can you let me know what the issue ID EMAILNET-41430 covers for your investigation? Also, will a corresponding ticket be opened on the java side?

This ticket covered getting reaction information from a msg.

There is no need for this, if there are changes, they will also be applied to the Java product.