Skip to content →

Category: Webmail

WebMail is a system that allows users to submit Email requests from a web form. It then will process user inputs and communicate with corresponding SMTP servers to deliver mails.The administrator of the system can con figure the port number on which the WebMail server will be monitoring, as well as the hosting directory for the WebServer sub-module. Done in 2010.

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));
    }
}
Leave a Comment

MIME Encoding

Quoted printable characters are encoded in the format =XX', where XX’ stands for the hexadecimal value of the character.

The encoder looks as follows:

import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * It encodes data into quoted printable format.
 *
* @author Daoyuan Li
 */
public class QuotedPrintableEncoder {
    /**
     * Encodes data into quoted printable format.
     * @param s Data to be encoded.
     * @return The encoded data.
     */
    public String encode(String s) {
        byte[] b = null;
        try {
            b = s.getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
return "";
        }
        String code = "";
        int wc = 0;
        for (int i = 0; i < b.length; i++) {
            byte c = b[i];
            if(c == 13){
                code = code.concat("\n");
                wc = 0;
                continue;
            } else if(c == 10) {
                //do nothing
                continue;
            }
            code = code.concat("=" + Integer.toHexString(c &255).toUpperCase());
            wc += 3;
            if (wc >= 75) {
                code = code.concat("=\n");
                wc = 0;
            }
        }
        return code;
    }

    /**
     * Encodes data into quoted printable format, without soft line breaks.
     * @param s Data to be encoded.
     * @return The encoded data.
     */
    public String encodeWithoutLineBreak(String s) {
        byte[] b = null;
        try{
            b = s.getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
        String code = "";
        for (int i = 0; i < b.length; i++) {
            byte c = b[i];
            if(c == 13){
                code = code.concat("\n");
                continue;
            } else if (c == 10){
//do nothing
                continue;
            }
            code = code.concat("=" + Integer.toHexString(c & 255).toUpperCase());
        }
        return code;
    }
}
Leave a Comment

URL Decoding

The charset of the form page is ISO-8859-15'. HTTP will encode the form message in URLEncoding, that is, space is replaced with +’; non-ASCII characters are encoded inthe format %XX', where XX’ stands for the hexadecimal value of the character.

The URL decoder in this system looks as follows:

importjava.io.UnsupportedEncodingException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * UrlDecoder decodes URL encoded information into quoted printable characters.
 * 
 * @author Daoyuan Li
 */
public class UrlDecoder {

    /**
     * Decode from URL encoding to quoted printable.
     * @param url URL encoded message to be decoded.
     * @return Decoded quoted printable string.
     * @throwsException If anything wrong happens, an Exception is thrown.
     */
    public String decode(String url) throws Exception {
        String decoded = "";
        Exception ex = new Exception();
        Vector buf = new Vector();
        for (int i = 0; i < url.length(); i++) {
            if (url.charAt(i) == '%') {
                if (i + 2 >= url.length()) {
                    throw ex;
                }

                int d = -1;
try {
                    d = Integer.parseInt(url.substring(i + 1, i + 3), 16);
                } catch (NumberFormatException e) {
                    throw ex;
                }
                if (d > 255 || d < 0) {
                    throw ex;
                }
                buf.add(new Byte((byte)d));
                i += 2;
            } else if (url.charAt(i) == '+') {
                buf.add(new Byte((byte)' '));
} else {
                buf.add(new Byte((byte) url.charAt(i)));
            }
        }
        try {
            byte[] dcd = new byte[buf.size()];
            for (int j = 0; j < dcd.length; j++) {
                dcd[j] = buf.elementAt(j);
            }
            decoded = new String(dcd, "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            Logger.getLogger(FormValidator.class.getName()).log(Level.SEVERE, null,e);
            throw ex;
        }
        return decoded;
    }
}
Leave a Comment

Webmail Overview

WebMail is a system that allows users to submit Email requests from a web form. It then will process user inputs and communicate with corresponding SMTP servers to deliver mails.The administrator of the system can configure the port number on which the WebMail server will be monitoring, as well as the hosting directory for the WebServer sub-module.

The system is mainly divided into three modules: the web server module, the mail deliver module and the scheduler module.

The web server module listens on a certain port, waiting for users’ requests from web browsers. It responds with corresponding pages like ‘new email’ page or status page.

The mail deliver module communicates with a SMTP server and deliver users’ Emails to these servers, and returns with a status code showing what the deliver result should be.

The scheduler determines when to deliver an Email since the user may schedule a future Email instead of sending it right away. It also generates the status page for the web server.

Leave a Comment