用JavaMail发送带附件的邮件
2008-02-23 10:07:08来源:互联网 阅读 ()
本文根据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.*;
import java.util.*;
/**
* The <CODE>FileProperties</CODE> class extends <CODE>Properties</CODE>,
* "a persistent set of properties [that] can be saved to a stream
* or loaded from a stream". This subclass attends to all the mundane
* details of opening the Stream(s) for actually saving and loading
* the Properties.
*
* <P>This subclass preserves the useful feature that
* a property list can contain another property list as its
* "defaults"; this second property list is searched if
* the property key is not found in the original property list.
*
* @author Ian F. Darwin, ian@darwinsys.com
* @version $Id: FileProperties.java,v 1.5 2001/04/28 13:22:37 ian Exp $
*/
public class FileProperties
extends Properties {
protected String fileName = null;
/**
* Construct a FileProperties given a fileName.
* @param loadsaveFileName the progerties file name
* @throws IOException
*/
public FileProperties(String loadsaveFileName) throws IOException {
super();
fileName = loadsaveFileName;
load();
}
/** Construct a FileProperties given a fileName and
* a list of default properties.
* @param loadsaveFileName the properties file name
* @param defProp the default properties
* @throws IOException
*/
public FileProperties(String loadsaveFileName, Properties defProp) throws
IOException {
super(defProp);
fileName = loadsaveFileName;
load();
}
/** The InputStream for loading */
protected InputStream inStr = null;
/** The OutputStream for loading */
protected OutputStream outStr = null;
/** Load the properties from the saved filename.
* If that fails, try again, tacking on the .properties extension
* @throws IOException
*/
public void load() throws IOException {
try {
if (inStr == null) {
inStr = new FileInputStream(fileName);
}
}
catch (FileNotFoundException fnf) {
if (!fileName.endsWith(".properties")) {
inStr = new FileInputStream(fileName ".properties");
// If we succeeded, remember it:
fileName = ".properties";
}
else {
// It did end with .properties and failed, re-throw exception.
throw fnf;
}
}
// now message the superclass code to load the file.
load(inStr);
}
/** Save the properties for later loading. *
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash