Only one CC entry is extracted when there are two CC entries

SampleFiles.zip (649 Bytes)

Aspose Team,
We use the Aspose Email java package to get properties from msg files. Some msg and eml files have two CC entries like the following

From: sender@example.com
To: recipient@example.com
CC: cc1@example.com
Cc: cc2@example.com
Subject: Test Email with Duplicate CC Headers
Date: Fri, 11 Jul 2025 17:15:32 +0530

However, Aspose only gets one of them (the last one). Note that both values show up when the files are opened with Outlook.
Wonder if you can give us some advice about getting all the values.

By the way, our client found two To entries in some of the files. Wonder if Aspose has the same issue in this case.

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

Sample code:

import com.aspose.email.MailMessage;

public class GetCC_34 {
public static void main(String[] args) {
String inputMsg = “my_path/sample_email_1.msg”;
String inputEml = “my_path/eml_file.eml”;
try {
new com.aspose.email.License().setLicense(“my_path/Aspose.Total.Java.lic”);

        try (MailMessage mailMessage = MailMessage.load(inputMsg)) {
            mailMessage.getCC().forEach(cc -> {
                System.out.println("Msg CC: " + cc.getAddress());
            });
        }

        try (MailMessage mailMessage = MailMessage.load(inputEml)) {
            mailMessage.getCC().forEach(cc -> {
                System.out.println("Eml CC: " + cc.getAddress());
            });
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

@xyang

Cause

The issue you’re experiencing with Aspose.Email only extracting one CC entry from emails that contain multiple CC entries is likely due to how the library processes the headers. In some cases, it may only recognize the last occurrence of a header, which can lead to missing earlier entries.

Solution

To ensure that all CC entries are extracted, you can manually parse the headers of the email message. This way, you can retrieve all instances of the CC field, regardless of how many there are.

Code

Here’s a modified version of your code that demonstrates how to extract all CC entries by directly accessing the headers:

import com.aspose.email.MailMessage;
import com.aspose.email.HeaderCollection;

public class GetCCEntries {
    public static void main(String[] args) {
        String inputMsg = "my_path/sample_email_1.msg";
        String inputEml = "my_path/eml_file.eml";
        try {
            new com.aspose.email.License().setLicense("my_path/Aspose.Total.Java.lic");

            // Load MSG file
            MailMessage mailMessage = MailMessage.load(inputMsg);
            printCCEntries(mailMessage);

            // Load EML file
            mailMessage = MailMessage.load(inputEml);
            printCCEntries(mailMessage);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void printCCEntries(MailMessage mailMessage) {
        HeaderCollection headers = mailMessage.getHeaders();
        String ccHeader = headers.get("CC");
        if (ccHeader != null) {
            String[] ccAddresses = ccHeader.split(",");
            for (String cc : ccAddresses) {
                System.out.println("CC: " + cc.trim());
            }
        } else {
            System.out.println("No CC entries found.");
        }
    }
}

This code retrieves the CC header directly from the email’s headers and splits it by commas to get all entries.

Next Steps

  1. Test the modified code with your sample files to ensure it captures all CC entries.
  2. If you encounter similar issues with the “To” field, apply the same approach to extract all entries from that header as well.
  3. Check the Aspose.Email documentation for any updates or changes regarding header handling in future versions.

Please let me know if you need further assistance!

This suggestion does not help. This issue is for when there are 2 (or maybe more) CC / To / BCC fields in the file with 1 or more entries in each line.

Hello,

Aspose.Email follows the standards (RFC 5322) when parsing email files. According to these standards, each recipient header such as To, CC, or BCC should appear only once in the email headers. If multiple recipients are needed, they should be combined into a single header field, with addresses separated by commas.

Correct Format:

CC: cc1@example.com, cc2@example.com
To: to1@example.com, to2@example.com

Incorrect Format:

CC: cc1@example.com
Cc: cc2@example.com
To: to1@example.com
To: to2@example.com