bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)
http://www.simplejavamail.org
Apache License 2.0
1.22k stars 266 forks source link

outlookMsgToEmail duplicates recipients if same name used for To and Cc #504

Open atmcq opened 5 months ago

atmcq commented 5 months ago

Repeated in 8.8.3. outlookMsgToEmail() duplicates To and Cc addresses, seemingly depending on the "name" used for the recipient addresses matching.

Pass:

To Andrew McQuillen andrew.mcquillen@example.com Cc test@example.com test@example.com

[Recipient{name='Andrew McQuillen', address='andrew.mcquillen@example.com', type=To}, Recipient{name='test@example.com', address='test@example.com', type=Cc}]

Fail:

To Andrew McQuillen andrew.mcquillen@example.com Cc Andrew McQuillen dummy@gmail.com

[Recipient{name='Andrew McQuillen', address='andrew.mcquillen@example.com', type=To}, Recipient{name='Andrew McQuillen', address='dummy@gmail.com', type=To}, Recipient{name='Andrew McQuillen', address='andrew.mcquillen@example.com', type=Cc}, Recipient{name='Andrew McQuillen', address='dummy@gmail.com', type=Cc}]

Code to repeat using attached examples:

    @Test
    void givenMsgFile_avoidDuplicateCc() {
        try { 

            Email t_message = EmailConverter.outlookMsgToEmail(new File("TestingCC.msg"));
            assertTrue(countCcRecipients(t_message.getRecipients()) == 1);

            String t_eml = EmailConverter.emailToEML(t_message);

            Email t_message2 = EmailConverter.emlToEmail(new ByteArrayInputStream(t_eml.getBytes()));
            assertTrue(countCcRecipients(t_message2.getRecipients()) == 1);

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

    @Test
    void givenMsgFileSameName_avoidDuplicateCc() {
        try { 

            Email t_message = EmailConverter.outlookMsgToEmail(new File("TestingCCSameName.msg"));
            assertTrue(countCcRecipients(t_message.getRecipients()) == 1);

            String t_eml = EmailConverter.emailToEML(t_message);

            Email t_message2 = EmailConverter.emlToEmail(new ByteArrayInputStream(t_eml.getBytes()));
            assertTrue(countCcRecipients(t_message2.getRecipients()) == 1);

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

    public static int countCcRecipients(List<Recipient> recipients) {
        int ccCount = 0;
        for (Recipient recipient : recipients) {
            if ("Cc".equals(recipient.getType().toString())) {
                ccCount++;
            }
        }
        return ccCount;
    }

TestingMsg.zip

bbottema commented 5 months ago

Ok, I checked and this is actually a bug in outlook-message-parser. I added an issue there: https://github.com/bbottema/outlook-message-parser/issues/76.