用JavaMail发送带附件的邮件

本文根据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. *
* @throws IOException
*/

public void save() throws IOException {
if (outStr == null) {
outStr = new FileOutputStream(fileName);
}
// Get the superclass to do most of the work for us.
store(outStr, "# Written by FileProperties.save() at " + new Date());
}

public void close() {
try {
if (inStr != null) {
inStr.close();
}
if (outStr != null) {
outStr.close();
}
}
catch (IOException e) {
// don't care
}
}
}
///

3.Mailer.java,将javamail发邮件的部分进行封装:
///
package untitled2;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class Mailer {
/** The javamail session object. */
protected Session session;
/** The sender's email address */
protected String from;
/** The subject of the message. */
protected String subject;
/** The recipient ("To:"), as Strings. */
protected ArrayList toList = new ArrayList();
/** The CC list, as Strings. */
protected ArrayList ccList = new ArrayList();
/** The BCC list, as Strings. */
protected ArrayList bccList = new ArrayList();
/** The text of the message. */
protected String body;
/** The SMTP relay host */
protected String mailHost;
/** The accessories list, as Strings.*/
protected ArrayList accessories = new ArrayList();
/** The verbosity setting */
protected boolean verbose;

/** Get from
* @return where the mail from
*/
public String getFrom() {
return from;
}

/** Set from
* @param fm where the mail from
*/
public void setFrom(String fm) {
from = fm;
}

/** Get subject
* @return the mail subject
*/
public String getSubject() {
return subject;
}

/** Set subject
* @param subj the mail subject
*/
public void setSubject(String subj) {
subject = subj;
}

// SETTERS/GETTERS FOR TO: LIST

/** Get tolist, as an array of Strings
* @return the list of toAddress
*/
public ArrayList getToList() {
return toList;
}

/** Set to list to an ArrayList of Strings
* @param to the list of toAddress
*/
public void setToList(ArrayList to) {
toList = to;
}

/** Set to as a string like "tom, mary, robin@host". Loses any
* previously-set values.
* @param s the list of toAddress*/
public void setToList(String s) {
toList = tokenize(s);
}

/** Add one "to" recipient
* @param to the toAddress
*/
public void addTo(String to) {
toList.add(to);
}

// SETTERS/GETTERS FOR CC: LIST  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值