本文根据Ian F. Darwin的《Java Cookbook》整理而成,原书用整章的文字介绍如何发邮件,可能头绪会比较乱,本文则将其浓缩成一篇文章,力求使完全不懂JavaMail的人,都可以根据文中指示稍作修改,拿来就可以用。如果对其中原理还有不清楚,你可以参考原书。
一、首先要用到三个java文件:
1.MailConstants.java,properties文件的助记符:
///////////////////////////////////////////////////////////////////////
package untitled2;
/** Simply a list of names for the Mail System to use.
* If you "implement" this interface, you don't have to prefix
* all the names with MailProps in your code.
*/
public interface MailConstants {
public static final String PROPS_FILE_NAME = "MailClient.properties";
public static final String SEND_PROTO = "Mail.send.protocol";
public static final String SEND_USER = "Mail.send.user";
public static final String SEND_PASS = "Mail.send.password";
public static final String SEND_ROOT = "Mail.send.root";
public static final String SEND_HOST = "Mail.send.host";
public static final String SEND_DEBUG = "Mail.send.debug";
public static final String RECV_PROTO = "Mail.receive.protocol";
public static final String RECV_PORT = "Mail.receive.port";
public static final String RECV_USER = "Mail.receive.user";
public static final String RECV_PASS = "Mail.receive.password";
public static final String RECV_ROOT = "Mail.receive.root";
public static final String RECV_HOST = "Mail.receive.host";
public static final String RECV_DEBUG = "Mail.receive.debug";
}
///////////////////////////////////////////////////////////////////////
2.FileProperties.java,从文件中读取properties:
///////////////////////////////////////////////////////////////////////
package untitled2;
import java.io.*;