oblac / jodd

Jodd! Lightweight. Java. Zero dependencies. Use what you like.
https://jodd.org
BSD 2-Clause "Simplified" License
4.06k stars 724 forks source link

jodd-email should add a method to set email sender's nickname (personal name) #158

Closed yizhl closed 10 years ago

yizhl commented 10 years ago

jodd-email should add a method to set email sender's nickname (personal name), not set it to null.

From jodd-email's source, in class jodd.mail.EmailUtil,it's look like as bellow:

/**
     * Converts string to <code>InternetAddress</code> while taking care of encoding.
     * The email can be given in following form:
     * <ul>
     *     <li>"email" - the whole string is an email</li>
     *     <li>"personal {@literal <email>}" - first part of the string is personal, and
     *          the other part is email, surrounded with &lt; and &gt;</li>
     * </ul>
     */
    public static InternetAddress string2Address(String address) throws AddressException {
        address = address.trim();
        String charset = JoddCore.encoding;

        if (StringUtil.endsWithChar(address, '>') == false) {
            try {
                return new InternetAddress(address, null, charset);
                                // look, jodd set nickname as null  
            } catch (UnsupportedEncodingException ueex) {
                throw new AddressException(ueex.toString());
            }
        }

        int ndx = address.lastIndexOf('<');
        if (ndx == -1) {
            throw new AddressException("Invalid address: " + address);
        }

        try {
            return new InternetAddress(
                    address.substring(ndx + 1, address.length() - 1),
                    address.substring(0, ndx - 1).trim(), charset);

                      //or set it to first part of the string, I don't think it's a good idea
                    //we should add a method so that we can define by ourself 
// such as our emila is admin@jodd.org,if I want to show personal name as "JODD TECH " ,
// we have to overwrite the source. 

        } catch (UnsupportedEncodingException ueex) {
            throw new AddressException(ueex.toString());
        }
    }
igr commented 10 years ago

Sure! Just let me see if I have understood it right - you want a new method where you can set personal name and email address separately? Something like (notice a space in personal name):

.to("JODD TECH ", "admin@jodd.org");

instead of existing:

.to("JODD TECH <admin@jodd.org>");

so you can eg have the extra space in personal name, like in this example?

yizhl commented 10 years ago
        /**
     * @see #setTo(String...)
     */

    public Email to(String to) {
        setTo(to);
        return this;
    }

    /**
     * @see #setTo(String...)
     */
    public Email to(String... tos) {
        setTo(tos);
        return this;
    }

From the source (jodd.mail.Email.to(....)), we can not clear know that we can set persnal name like you said :

.to("JODD TECH <admin@jodd.org>");

From the doc (http://jodd.org/doc/email.html) and source all not describe email address format for pernal name( must have the extra space).

Add a method make email address separately,then you are clear,even have no doc:

.to("JODD TECH ", "admin@jodd.org");
igr commented 10 years ago

Got it. The only reason for having thins in one string is because you can set multiple to addresses at once.

But you are right, this is not the first time someone is facing this problem :) I will make a change.

yizhl commented 10 years ago

Thanks very much!