本例基于javamail的jar包,下载地址: http://www.oracle.com/technetwork/java/index-138643.html
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* Title: 使用javamail发送邮件 Description: 演示如何使用javamail包发送电子邮件。这个实例可群发,可发送多附件
* Filename: SendMailUtil.java
*/
public class SendMailUtil {
String to = ""; // 收件人
String from = ""; // 发件人
String host = ""; // smtp主机
String username = ""; // 用户名
String password = ""; // 密码
String filename = ""; // 附件文件名
String subject = ""; // 邮件主题
String content = ""; // 邮件正文
Vector<Object> file = new Vector<Object>(); // 附件文件集合
/**
* <br>
* 方法说明:默认构造器 <br>
* 输入参数: <br>
* 返回类型:
*/
public SendMailUtil() {
}
/**
* <br>
* 方法说明:构造器,提供直接的参数传入 <br>
* 输入参数: <br>
* 返回类型:
*/
public SendMailUtil(String to, String from, String smtpServer,
String username, String password, String subject, String content) {
this.to = to;
this.from = from;
this.host = smtpServer;
this.username = username;
this.password = password;
this.subject = subject;
this.content = content;
}
/**
* <br>
* 方法说明:把主题转换为中文 <br>
* 输入参数:String strText <br>
* 返回类型:
*/
public String transferChinese(String strText) {
try {
strText = MimeUtility.encodeText(new String(strText.getBytes()),
"utf-8", "B");
} catch (Exception e) {
e.printStackTrace();
}
return strText;
}
/**
* <br>
* 方法说明:往附件组合中添加附件 <br>
* 输入参数: <br>
* 返回类型:
*/
public void addAttachFile(String fname) {
file.addElement(fname);
}
/**
* <br>
* 方法说明:多人发送,分解邮件发送地址 <br>
* 输入参数: <br>
* 返回类型:
*/
public InternetAddress[] ManyAddress(String to) {
// 将要发送的邮件地址根据";"号放在一个数组中
String[] toAddress = to.split(",");
// 要发送的邮件地址数量
int toCount = toAddress.length;
// 格式化要发送的邮件地址
InternetAddress[] address = new InternetAddress[toCount];
try {
for (int i = 0; i < toCount; i++) {
address[i] = new InternetAddress(toAddress[i]);
}
} catch (AddressException e) {
e.printStackTrace();
}
return address;
}
/**
* <br>
* 方法说明:发送邮件 <br>
* 输入参数: <br>
* 返回类型:boolean 成功为true,反之为false
*/
public boolean sendMail() {
// 构造mail session,两个参数分别是内容和图片
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
// 用户登录stmp服务器时候验证用户名和密码。
return new PasswordAuthentication(username, password);
}
});
try {
// 构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
// 单人发送
// InternetAddress[] address = { new InternetAddress(to) };
// 多人发送
InternetAddress[] address = ManyAddress(to);
msg.setRecipients(Message.RecipientType.TO, address);
subject = transferChinese(subject);
msg.setSubject(subject);
// 构造Multipart
Multipart mp = new MimeMultipart();
// 向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setText(content);
// 向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent);
// 向Multipart添加附件
Enumeration<Object> efile = file.elements();
while (efile.hasMoreElements()) {
MimeBodyPart mbpFile = new MimeBodyPart();
filename = efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(transferChinese(fds.getName()));
System.out.println(mbpFile.getFileName());
// 向MimeMessage添加(Multipart代表附件)
mp.addBodyPart(mbpFile);
}
file.removeAllElements();
// 向Multipart添加MimeMessage
msg.setContent(mp);
msg.setSentDate(new Date());
// 发送邮件
Transport.send(msg);
} catch (MessagingException mex) {
System.out.println("邮件发送异常,请检查邮件配置信息是否正确!");
mex.printStackTrace();
return false;
}
System.out.println("邮件发送成功!");
return true;
}
/**
* <br>
* 方法说明:主方法,用于测试 <br>
* 输入参数: <br>
* 返回类型:
*/
public static void main(String[] args) {
SendMailUtil sendmail = new SendMailUtil();
sendmail.setHost("smtp.qq.com"); // 邮件协议
sendmail.setUsername("test"); // 发件人
sendmail.setPassword("zpj2991575"); // 发件人密码
sendmail.setFrom(""); // 发件人地址
sendmail.setTo(""); // 收件人地址,多人地址用","分开
sendmail.setSubject("你好,这是测试邮件!"); // 邮件主题
sendmail.setContent("你好这是一个带多附件的测试邮件!"); // 发送附件
sendmail.addAttachFile("C:\\Users\\Administrator\\Desktop\\JSF新手入门最好的教程.doc"); // 发送附件01
sendmail.addAttachFile("C:\\Users\\Administrator\\Desktop\\简单JSF实例参考.doc"); // 发送附件02
sendmail.sendMail();
}
/**
* <br>
* 方法说明:设置邮件服务器地址 <br>
* 输入参数:String host 邮件服务器地址名称 <br>
* 返回类型:
*/
public void setHost(String host) {
this.host = host;
}
/**
* <br>
* 方法说明:设置登录服务器校验密码 <br>
* 输入参数: <br>
* 返回类型:
*/
public void setPassword(String password) {
this.password = password;
}
/**
* <br>
* 方法说明:设置登录服务器校验用户 <br>
* 输入参数: <br>
* 返回类型:
*/
public void setUsername(String username) {
this.username = username;
}
/**
* <br>
* 方法说明:设置邮件发送目的邮箱 <br>
* 输入参数: <br>
* 返回类型:
*/
public void setTo(String to) {
this.to = to;
}
/**
* <br>
* 方法说明:设置邮件发送源邮箱 <br>
* 输入参数: <br>
* 返回类型:
*/
public void setFrom(String from) {
this.from = from;
}
/**
* <br>
* 方法说明:设置邮件主题 <br>
* 输入参数: <br>
* 返回类型:
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* <br>
* 方法说明:设置邮件内容 <br>
* 输入参数: <br>
* 返回类型:
*/
public void setContent(String content) {
this.content = content;
}
}
如果使用myeclipse,发生异常请删除MyEclipse中javaee.jar包中的 mail和 activation,然后导入mail.jar和actvation.jar包即可.