2009-02-15 18:02:16
下面是该程序的源代码,请参考注释或其他标识。下载附件请到:http://download.csdn.net/source/1012887
/*
该程序通过JavaMail是实现邮件的发送:主要步骤有:
1、导入mail.jar、htmlparser.jar和 acivation.jar类包。
2、设置邮件的smtp服务器 和 smtp端口(smtp服务器用于发送邮件 ,pop3服务器用于接收邮件)
3、设置邮件的主题、抽送对象、附件 和 邮件正文
4、设置邮件账号
*/
/**
* @author tsball@qq.com
* @publishDate 2009-02-15
*/
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMail {
private MimeMessage mimeMsg; //MIME郵件物件
private Session session; //郵件會話物件
private Properties props; //系統屬性
private String username = ""; //smtp認證用戶名和密碼
private String password = "";
private Multipart mp; //Multipart物件,郵件內容,標題,附件等內容均添加到其中後再生成MimeMessage物件
public SendMail(String hostName, int portNumber, boolean need){
setSmtpProperties(hostName, portNumber, need);
createMimeMessage();
}
public void setSmtpProperties(String hostName, int portNumber, boolean need) {
if(props == null)
props = System.getProperties();
props.put("mail.smtp.host",hostName); //設置SMTP服务器主機
props.put("mail.smtp.port",portNumber); //設置SMTP主機端口
props.put("mail.smtp.auth",String.valueOf(need)); //設置smtp身份認證,是否需要验证
System.out.println("設置SMTP服务器主機 :" + hostName + " 端口:" + portNumber + "身份验证:" + need);
}
public boolean createMimeMessage() {
try {
session = Session.getDefaultInstance(props,null);
System.out.println("準備獲取郵件會話對象!");
} catch(Exception e) {
System.err.println("獲取郵件會話物件時發生錯誤!"+e);
return false;
}
try{
mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
System.out.println("準備創建MIME郵件對象!");
return true;
} catch(Exception e){
System.err.println("創建MIME郵件物件失敗!"+e);
return false;
}
}
public void setNamePass(String username,String password) {
this.username = username;
this.password = password;
}
public boolean setSubject(String mailSubject) {
try{
mimeMsg.setSubject(mailSubject);
System.out.println("設置郵件主題!");
return true;
} catch(Exception e) {
System.err.println("設置郵件主題發生錯誤!" + e);
return false;
}
}
public boolean setBody(String mailBody) {
try{
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+mailBody,"text/html;charset=GB2312");
mp.addBodyPart(bp);
System.out.println("設置郵件正文!");
return true;
} catch(Exception e){
System.out.println("設置郵件正文時發生錯誤!" + e);
return false;
}
}
public boolean addFileAffix(String filename) {
try{
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
System.out.println("增加郵件附件:" + filename);
return true;
} catch(Exception e){
System.err.println("增加郵件附件:" + filename + "發生錯誤!" + e);
return false;
}
}
public boolean setFrom(String from) {
try{
mimeMsg.setFrom(new InternetAddress(from));
System.out.println("設置發信人!");
return true;
} catch(Exception e) {
System.out.println("設置發信人發生錯誤!");
return false;
}
}
public boolean setTo(String to){
if(to == null)
return false;
try{
mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
System.out.println("設置接收對象!");
return true;
} catch(Exception e) {
System.out.println("設置接收對象發生錯誤!");
return false;
}
}
public boolean setCopyTo(String copyto)
{
if(copyto == null)
return false;
try{
mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
System.out.println("設置抽送對象!");
return true;
} catch(Exception e) {
System.out.println("設置抽送對象發生錯誤!");
return false;
}
}
public boolean sendout()
{
try{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在發送郵件....");
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);
System.out.println("發送郵件成功!");
transport.close();
return true;
} catch(Exception e) {
System.err.println("郵件發送失敗!" + e);
return false;
}
}
/**
* Just do it as this
*/
public static void main(String[] args) {
String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=big5>"+
"<div align=center><a href=http://www.google.cn > 邮件内容! </a></div>";
SendMail themail = new SendMail("smtp.qq.com", 587, true);
if(themail.setTo("tsball@126.com") == false) return; //接收邮件
if(themail.setCopyTo("paul@apjcorp.com") == false) return; //抽送邮件
if(themail.setFrom("tsball@qq.com") == false) return; //发送方邮件
if(themail.setSubject("邮件主题") == false) return; //邮件主题
if(themail.setBody(mailbody) == false) return; //邮件正文
if(themail.addFileAffix("d://attachement.txt") == false) return; //邮件附件
themail.setNamePass("userName","password"); //发送方账号
if(themail.sendout() == false) return;
}
}