1、首先在項目中導(dǎo)入jar包,我用的是mail-1.4.jar
2、一下是我的代碼:
public class SendMailUtil {
private static final Logger logger = Logger.getLogger(SendMailUtil.class);
// 定義發(fā)件人、收件人、主題等
private String to = null;// 收件人郵箱地址
private String from = null;// 發(fā)件人郵箱地址
private String password = null;//發(fā)件人密碼
private String host = null;//郵件服務(wù)器
private String filename = null;
// 用于保存發(fā)送附件的文件路徑名的集合
private Vector<String> fileList = new Vector<String>();
/*
* 可以傳發(fā)件人等參數(shù)的構(gòu)造
*/
public SendMailUtil(String to, String from, String password,
String smtpServer) {
// 初始化發(fā)件人、收件人、主題等
this.to = to;
this.from = from;
this.password = password;
this.host = smtpServer;
}
/*
* 該方法用于收集附件名
*/
public void attachfile(String fname) {
fileList.addElement(fname);
}
/*
* 開始發(fā)送信件的方法
*/
public boolean startSend(String emailTitle, String emailContent) {
if (StringUtils.isBlank(emailContent)) {
logger.error("郵件內(nèi)容不能為空!");
return false;
}
try {
if (StringUtils.isBlank(emailContent)) {
throw new NullPointerException("發(fā)送的內(nèi)容不能為空!");
}
// 創(chuàng)建Properties對象
Properties props = System.getProperties();
// 創(chuàng)建信件服務(wù)器
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.password", "0");
// 得到默認(rèn)的對話對象
Session session = Session.getInstance(props,
new PopupAuthenticator(this.from, this.password));
// 創(chuàng)建一個消息,并初始化該消息的各項元素
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(Message.RecipientType.TO, address);
// getBytes("ISO-8859-1")
// String title=new String(emailTitle.getBytes("ISO-8859-1"),"GBK");
msg.setSubject(emailTitle);
// 后面的BodyPart將加入到此處創(chuàng)建的Multipart中
Multipart mp = new MimeMultipart("subtype");
// 添加HTML正文
BodyPart htmlBody = new MimeBodyPart();
MimeMultipart htmlContent = new MimeMultipart("related");
BodyPart msgContent = new MimeBodyPart();
htmlContent.addBodyPart(msgContent);
msgContent.setContent(emailContent, "text/html;charset=utf-8");
htmlBody.setContent(htmlContent);
mp.addBodyPart(htmlBody);
// 利用枚舉器方便的遍歷集合
Enumeration efile = fileList.elements();
// 檢查序列中是否還有更多的對象
while (efile.hasMoreElements()) {
MimeBodyPart mbp = new MimeBodyPart();
// 選擇出每一個附件名
filename = efile.nextElement().toString();
// 得到數(shù)據(jù)源
FileDataSource fds = new FileDataSource(filename);
// 得到附件本身并至入BodyPart
mbp.setDataHandler(new DataHandler(fds));
// 得到文件名同樣至入BodyPart
mbp.setFileName(fds.getName());
mp.addBodyPart(mbp);
}
// 移走集合中的所有元素
fileList.removeAllElements();
// Multipart加入到信件
msg.setContent(mp);
// 設(shè)置信件頭的發(fā)送日期
msg.setSentDate(new Date());
// 發(fā)送信件
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// 認(rèn)證類
class PopupAuthenticator extends Authenticator {
private String username ;
private String password ;
public PopupAuthenticator(String user, String pass) {
this.username = user;
this.password = pass;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
//測試
public static void main(String[] args) {
SendMailUtil mail = new SendMailUtil("",
"", "smtp.163.com");
mail.attachfile("C:\Users\awen\Desktop\a.txt");
mail.startSend("交接文檔","收到請回復(fù)");
System.out.println("郵件發(fā)送完成...");
}
3、測試成功,大家可以試一下。
更多信息請查看IT技術(shù)專欄