需要两个包:javamail.jar和activation.jar.
SendAuthMail .java
-------------------------------------------------------------------------------------------------
package com.hanweb.jcms;
/**
* 发送带SMTP身份认证的电子邮件
* 整理 by longware
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendAuthMail {
private MimeMessage mimeMsg; //MIME邮件对象
private Session session; //邮件会话对象
private Properties props; //系统属性
private boolean needAuth = false; //smtp是否需要认证
private String username = ""; //smtp认证用户名和密码
private String password = "";
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
public SendAuthMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName) {
if (props == null) {
props = System.getProperties(); //获得系统属性对象
}
props.put("mail.smtp.host", hostName); //设置SMTP主机
}
public boolean createMimeMessage() {
try {
session = Session.getDefaultInstance(props, null); //获得邮件会话对象
} catch (Exception e) {
System.err.println("[SYS] 获取邮件会话对象时发生错误!" + e);
return false;
}
try {
mimeMsg = new MimeMessage(session); //创建MIME邮件对象
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("[SYS] 创建MIME邮件对象失败!" + e);
return false;
}
}
public void setNeedAuth(boolean need) {
if (props == null) {
props = System.getProperties();
}
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
public boolean setSubject(String mailSubject) {
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("[SYS] 设置邮件主题发生错误!");
return false;
}
}
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<html><meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ mailBody + "</html>", "text/html;charset=gb2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("[SYS] 设置邮件正文时发生错误!" + e);
return false;
}
}
public boolean addAttachFile(String filename) {
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("[SYS] 增加邮件附件:'" + filename + "' 发生错误!" + e);
return false;
}
}
public boolean setFrom(String from) {
try {
mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
return true;
} catch (Exception e) {
return false;
}
}
public boolean setTo(String to) {
if (to == null) {
return false;
}
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
public boolean setCC(String cc) {
if (cc == null) {
return false;
}
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(cc));
return true;
} catch (Exception e) {
return false;
}
}
public boolean sendOut() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
//transport.send(mimeMsg);
transport.close();
return true;
} catch (Exception e) {
System.err.println("[SYS] 邮件发送失败!" + e);
return false;
}
}
}
usage:
-------------------------------------------------------------------------------------------------
String strSMTP = "smtp.csdn.net";
String strSubject = "Test mail from javamail.";
String strBody = "<b>This is just a test.<b>";
String strFrom = "longware@csdn.net";
String strTo = "blog@csdn.net";
String strCC= "users@163.com";
String strUser = "yourName";
String strPassword = "yourPassword";
String strAttach = "c:\boot.ini";
SendAuthMail sm = new SendAuthMail(strSMTP);
sm.setNeedAuth(true);
sm.setNamePass(strUser,strPassword);
sm.setFrom(strFrom);
sm.setTo(strTo);
sm.setCC(strCC);
sm.setSubject(strSubject);
sm.setBody(strBody);
sm.addAttachFile(strAttach);
sm.sendOut();
方法很多,抛砖引玉了...