com.szmslab.quickjavamail.utils.MailAddress.java Source code

Java tutorial

Introduction

Here is the source code for com.szmslab.quickjavamail.utils.MailAddress.java

Source

/*
 * Copyright (c) 2014 szmslab
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
package com.szmslab.quickjavamail.utils;

import java.io.UnsupportedEncodingException;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.lang3.StringUtils;

/**
 * ????
 *
 * @author szmslab
 */
public class MailAddress {

    /**
     * 
     */
    private String address;

    /**
     * ??
     */
    private String personal;

    /**
     * ??
     *
     * @param address
     *            
     */
    public MailAddress(String address) {
        this.address = address;
    }

    /**
     * ??
     *
     * @param address
     *            
     * @param personal
     *            ??
     */
    public MailAddress(String address, String personal) {
        this.address = address;
        this.personal = personal;
    }

    /**
     * InternetAddress?????
     *
     * @return InternetAddress?
     * @throws AddressException
     * @throws UnsupportedEncodingException
     */
    public InternetAddress toInternetAddress() throws AddressException, UnsupportedEncodingException {
        return toInternetAddress(null);
    }

    /**
     * InternetAddress?????
     *
     * @param charset
     *            
     * @return InternetAddress?
     * @throws AddressException
     * @throws UnsupportedEncodingException
     */
    public InternetAddress toInternetAddress(String charset) throws AddressException, UnsupportedEncodingException {
        if (StringUtils.isBlank(personal)) {
            return new InternetAddress(address);
        } else {
            return new InternetAddress(address, personal, charset);
        }
    }

    /**
     * ????
     *
     * @return 
     */
    public String getAddress() {
        return address;
    }

    /**
     * ??????
     *
     * @return ??
     */
    public String getPersonal() {
        return personal;
    }

    /* (? Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isBlank(personal)) {
            sb.append(address);
        } else {
            sb.append(personal).append("<").append(address).append(">");
        }
        return sb.toString();
    }

}