使ç¨JavaMailåéé®ä»¶éè¦ç¨å°mail.jaråactivtion.jar两个å
ã
该类å®ç°äºè¾å®æ´çé®ä»¶åéåè½ï¼å
æ¬ä»¥HTMLæ ¼å¼åéï¼æ·»å é件åæé人ãä¸é¢æ¯å
·ä½ç代ç ï¼
package cn.cgw.util.mail;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Mail {
private MimeMessage mimeMsg; //MIMEé®ä»¶å¯¹è±¡
private Session session; //é®ä»¶ä¼è¯å¯¹è±¡
private Properties props; //ç³»ç»å±æ§
private boolean needAuth = false; //smtpæ¯å¦éè¦è®¤è¯
//smtp认è¯ç¨æ·ååå¯ç
private String username;
private String password;
private Multipart mp; //Multipart对象,é®ä»¶å
容,æ é¢,é件çå
容åæ·»å å°å
¶ä¸ååçæMimeMessage对象
/**
* Constructor
* @param smtp é®ä»¶åéæå¡å¨
*/
public Mail(String smtp){
setSmtpHost(smtp);
createMimeMessage();
}
/**
* 设置é®ä»¶åéæå¡å¨
* @param hostName String
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系ç»å±æ§ï¼mail.smtp.host = "+hostName);
if(props == null)
props = System.getProperties(); //è·å¾ç³»ç»å±æ§å¯¹è±¡
props.put("mail.smtp.host",hostName); //设置SMTP主æº
}
/**
* å建MIMEé®ä»¶å¯¹è±¡
* @return
*/
public boolean createMimeMessage()
{
try {
System.out.println("åå¤è·åé®ä»¶ä¼è¯å¯¹è±¡ï¼");
session = Session.getDefaultInstance(props,null); //è·å¾é®ä»¶ä¼è¯å¯¹è±¡
}
catch(Exception e){
System.err.println("è·åé®ä»¶ä¼è¯å¯¹è±¡æ¶åçé误ï¼"+e);
return false;
}
System.out.println("åå¤å建MIMEé®ä»¶å¯¹è±¡ï¼");
try {
mimeMsg = new MimeMessage(session); //å建MIMEé®ä»¶å¯¹è±¡
mp = new MimeMultipart();
return true;
} catch(Exception e){
System.err.println("å建MIMEé®ä»¶å¯¹è±¡å¤±è´¥ï¼"+e);
return false;
}
}
/**
* 设置SMTPæ¯å¦éè¦éªè¯
* @param need
*/
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认è¯ï¼mail.smtp.auth = "+need);
if(props == null) props = System.getProperties();
if(need){
props.put("mail.smtp.auth","true");
}else{
props.put("mail.smtp.auth","false");
}
}
/**
* 设置ç¨æ·ååå¯ç
* @param name
* @param pass
*/
public void setNamePass(String name,String pass) {
username = name;
password = pass;
}
/**
* 设置é®ä»¶ä¸»é¢
* @param mailSubject
* @return
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置é®ä»¶ä¸»é¢ï¼");
try{
mimeMsg.setSubject(mailSubject);
return true;
}
catch(Exception e) {
System.err.println("设置é®ä»¶ä¸»é¢åçé误ï¼");
return false;
}
}
/**
* 设置é®ä»¶æ£æ
* @param mailBody String
*/
public boolean setBody(String mailBody) {
try{
BodyPart bp = new MimeBodyPart();
bp.setContent(""+mailBody,"text/html;charset=GBK");
mp.addBodyPart(bp);
return true;
} catch(Exception e){
System.err.println("设置é®ä»¶æ£ææ¶åçé误ï¼"+e);
return false;
}
}
/**
* æ·»å é件
* @param filename String
*/
public boolean addFileAffix(String filename) {
System.out.println("å¢å é®ä»¶é件ï¼"+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("å¢å é®ä»¶é件ï¼"+filename+"åçé误ï¼"+e);
return false;
}
}
/**
* 设置å信人
* @param from String
*/
public boolean setFrom(String from) {
System.out.println("设置å信人ï¼");
try{
mimeMsg.setFrom(new InternetAddress(from)); //设置å信人
return true;
} catch(Exception e) {
return false;
}
}
/**
* 设置æ¶ä¿¡äºº
* @param to String
*/
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;
}
}
/**
* 设置æé人
* @param copyto String
*/
public boolean setCopyTo(String copyto)
{
if(copyto == null)return false;
try{
mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
return true;
}
catch(Exception e)
{ 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.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC));
//transport.send(mimeMsg);
System.out.println("åéé®ä»¶æåï¼");
transport.close();
return true;
} catch(Exception e) {
System.err.println("é®ä»¶åé失败ï¼"+e);
return false;
}
}
/**
* è°ç¨sendOutæ¹æ³å®æé®ä»¶åé
* @param smtp
* @param from
* @param to
* @param subject
* @param content
* @param username
* @param password
* @return boolean
*/
public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //éè¦éªè¯
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
/**
* è°ç¨sendOutæ¹æ³å®æé®ä»¶åé,带æé
* @param smtp
* @param from
* @param to
* @param copyto
* @param subject
* @param content
* @param username
* @param password
* @return boolean
*/
public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //éè¦éªè¯
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setCopyTo(copyto)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
/**
* è°ç¨sendOutæ¹æ³å®æé®ä»¶åé,带é件
* @param smtp
* @param from
* @param to
* @param subject
* @param content
* @param username
* @param password
* @param filename é件路å¾
* @return
*/
public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password,String filename) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //éè¦éªè¯
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.addFileAffix(filename)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
/**
* è°ç¨sendOutæ¹æ³å®æé®ä»¶åé,带é件åæé
* @param smtp
* @param from
* @param to
* @param copyto
* @param subject
* @param content
* @param username
* @param password
* @param filename
* @return
*/
public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password,String filename) {
Mail theMail = new Mail(smtp);
theMail.setNeedAuth(true); //éè¦éªè¯
if(!theMail.setSubject(subject)) return false;
if(!theMail.setBody(content)) return false;
if(!theMail.addFileAffix(filename)) return false;
if(!theMail.setTo(to)) return false;
if(!theMail.setCopyTo(copyto)) return false;
if(!theMail.setFrom(from)) return false;
theMail.setNamePass(username,password);
if(!theMail.sendOut()) return false;
return true;
}
}