示例中需要用到两个包:mail.jar和activation.jar,其下载地址为:
http://java.sun.com/products/javamail/downloads/index.html
http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html
下载后解压,将mail.jar和activation.jar放到project里的lib文件夹中,为其配置环境变量,或在myEclipse里的"Java Build Path"中将其添加到"Libraries"。
project里有三个类:入口类(TestJavaMail)、邮件信息类(MailBean)和发送邮件类(SendMail)
MailBean.java
package
test3;
import java.util.Vector;
public class MailBean ... {
private String to; // 收件人
private String from; // 发件人
private String host; // SMTP主机
private String username; // 发件人的用户名
private String password; // 发件人的密码
private String subject; // 邮件主题
private String content; // 邮件正文
Vector<String> file; // 多个附件
private String filename; // 附件的文件名
public String getTo() ...{
return to;
}
public void setTo(String to) ...{
this.to = to;
}
public String getFrom() ...{
return from;
}
public void setFrom(String from) ...{
this.from = from;
}
public String getHost() ...{
return host;
}
public void setHost(String host) ...{
this.host = host;
import java.util.Vector;
public class MailBean ... {
private String to; // 收件人
private String from; // 发件人
private String host; // SMTP主机
private String username; // 发件人的用户名
private String password; // 发件人的密码
private String subject; // 邮件主题
private String content; // 邮件正文
Vector<String> file; // 多个附件
private String filename; // 附件的文件名
public String getTo() ...{
return to;
}
public void setTo(String to) ...{
this.to = to;
}
public String getFrom() ...{
return from;
}
public void setFrom(String from) ...{
this.from = from;
}
public String getHost() ...{
return host;
}
public void setHost(String host) ...{
this.host = host;