Skip to content →

Non-ASCII characters in Email subject

In this system, Email subjects are encoded in Q' scheme, a scheme similar to quoted printable’. The format is “=?charset?Q?encoded subject?=”, in our case, the charset is `ISO-8859-15′.

For example, the Email object in this system is as follows:


importjava.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Email object.
 *
 * @author Daoyuan Li
 */
public class Mail {

    /**
     * Sender's Email address
     */
    private String from;

    /**
     * Recipient's Email address
     */
    private String to;

    /**
     * Email subject
     */
    private String subject;

    /**
     * Email message body
     */
    private Stringmessage;

    /**
     * Constructor of Mail
     * @param from Sender's Email address.
     * @param to Recipient's Email address.
     * @param subject Email subject.
     * @param message Email message.
     */
    public Mail(String from, String to, String subject, String message) {
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.message = message;
    }

    /**
     * Get sender'sEmail address.
     * @return Sender's Email address.
     */
    public String getFrom() {
        return this.from;
    }

    /**
     * Get Recipient's Email address.
     * @return Recipient's Email address.
     */
    public String getTo() {
        return this.to;
    }

    /**
     * Get Email subject.
     * @return Email subject.
     */
    public String getSubject() {
        return this.subject;
}

    /**
     * Get Email message.
     * @return Email message.
     */
    public String getMessage() {
        return this.message;
    }

    /**
     * Set sender's Email address.
     * @param from Sender's Email address.
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * Set Recipient's Email address.
     * @param to Recipient's Email address.
     */
public void setTo(String to) {
        this.to = to;
    }

    /**
     * Set Email subject.
     * @param subject Email subject.
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * Set Email message.
     * @param message Email message.
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * Encode Email into MIMEformat.
     * @return The MIME encoded Email.
     */
    public String encode() {
        String FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";
        //String FORMAT = "yyyy-MM-dd HH:mm:ss Z";
        Calendar cal = null;
        cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT);
        //System.out.println(sdf.format(cal.getTime()));
        String s = "From: <" + this.getFrom() + ">\n"
                + "To:<" + this.getTo() + ">\n"
                //+ "Subject: " + this.getSubject() + "\n"
                + "Subject: =?ISO-8859-15?Q?"
                + new QuotedPrintableEncoder().encodeWithoutLineBreak(this.subject) + "?=\n"
                + "Date: " + sdf.format(cal.getTime()) + "\n"
                + "Message-ID: " + cal.getTimeInMillis() + "@ik2213.lab\n"
                + "MIME-Version: 1.0\n"
                + "Content-Type:text/plain; charset=ISO-8859-15\n"
                + "Content-Transfer-Encoding: quoted-printable\n";
        return s.concat(new QuotedPrintableEncoder().encode(this.message));
    }
}

Published in Webmail

Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.