jakartaee / mail-api

Jakarta Mail Specification project
https://jakartaee.github.io/mail-api
Other
242 stars 100 forks source link

jakarta.mail.internet.AddressException when entire email address is single quoted #663

Closed davecrighton closed 1 year ago

davecrighton commented 1 year ago

We use jakartamail 2.0 (currently) for email access from one of our projects and we recently had an issue where a user was experiencing the following exception:

MbEmailInputNode.readData, 'jakarta.mail.internet.AddressException: Domain contains illegal character in string ``'dummyAddress@removed.com''' at jakarta.mail.internet.InternetAddress.checkAddress(InternetAddress.java:1410) 
at jakarta.mail.internet.InternetAddress.parse(InternetAddress.java:1191) 
at jakarta.mail.internet.InternetAddress.parseHeader(InternetAddress.java:753) 
at jakarta.mail.internet.MimeMessage.getAddressHeader(MimeMessage.java:732) 
at jakarta.mail.internet.MimeMessage.getRecipients(MimeMessage.java:558) at <etc>

Here the problem is that the recipient in the "TO" field has been single quoted and this results in the last single quote in the string being treated as part of the domain. I can reproduce this in the following test trivially:

package testEmailQuotes;

import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.util.Properties;

import org.junit.Test;

import jakarta.mail.Address;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMessage.RecipientType;

public class TestEmailQuotes {

    @Test
    public void testQuotedMimeMessage() throws UnsupportedEncodingException, MessagingException {
        Properties p = new Properties();
        // p.put("mail.mime.address.strict", "false"); // Remove comment  to make test pass
        Session s = Session.getDefaultInstance(p);
        MimeMessage m = new MimeMessage(s);
        String address = "'test@quotedAddress.com'";
        m.addRecipient(RecipientType.TO, new  InternetAddress(address, address));
        m.getRecipients(RecipientType.TO);
    }

}

As noted in the test setting the mail.mime.address.strict session property to false resolves the issue however I am wondering if this might be a common mis-configuration and as such should be dealt with by the library. If so I'm happy to look into putting together a PR contribution but wanted clarity as to whether this is working as designed or not first.

lukasj commented 1 year ago

I believe current behaviour is correct and should not be changed - RFC 822 treats single quote as a normal character, one should use double-quote (char 34) instead or mail.mime.address.strict=false to allow deviations from the RFC