java实现邮件发送的方法很多,这里提供一种:
1.mail实体类
public class Mail {
public String host = "";
public String from="";
public String password = "";
public List<String> to=new ArrayList<String>();//目标邮件地址
public List<File> att=new ArrayList<File>();//附件
public String context;
public String title;
public String fromname;
public Boolean hassend;
/**
*
* @param host 邮件键服务器
* @param from 发送邮件地址
* @param pwd 密码
* @param title 标题
* @param context 内容,html格式
*/
@SuppressWarnings("static-access")
public Mail(String host,String from,String pwd,String title,String context){
this.host=host;
this.from=from;
this.password=pwd;
this.context=context;
this.fromname=context;
this.title=title;
this.hassend = false;
}
/**
* 添加目标地址
* @param s
*/
public void addTarget(String s)throws NullPointerException,ParseException{
this.to.add(s);
}
/**
* 添加附件
* @param s
*/
public boolean addAttach(String s){
File f=new File(s);
return this.addAttach(f);
}
/**
* 添加附件
* @param f
* @throws FileNotFoundException
*/
public boolean addAttach(File f){
if(f.exists()&&f.isFile()){
this.att.add(f);
return true;
}else{
return false;
}
}
/**
* 邮件内容
* @param context
*/
public void setContext(String context) {
this.context = context;
}
/**
* 邮件标题
* @param title
*/
public void setTitle(String title) {
this.title = title;
}
2.具体实现方法
public void sendmail(Mail mail){
if(mail.hassend) return;
Integer status = 1;
try{
Properties props = new Properties();
props.put("mail.smtp.host", mail.host);
props.put("mail.smtp.auth", "true");
Session mailSession = Session.getDefaultInstance(props);
Message newMessage = new MimeMessage(mailSession);
newMessage.setFrom(new InternetAddress(mail.from));
for(String toa:mail.to){
newMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(toa));
}
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
Multipart multipart = new MimeMultipart();
BodyPart html=new MimeBodyPart();
html.setContent(mail.context, "text/html;charset=gbk");
multipart.addBodyPart(html);
for(File s:mail.att){
MimeBodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(s);
messageBodyPart.setDataHandler(new DataHandler(source));
String FileName = MimeUtility.encodeText(s.getName());
messageBodyPart.setFileName(FileName);
multipart.addBodyPart(messageBodyPart);
}
newMessage.setContent(multipart);
newMessage.setSubject(mail.title);
newMessage.setSentDate(new java.util.Date());
//newMessage.setText(this.context);
SMTPTransport t = (SMTPTransport) mailSession.getTransport("smtp");
try{
t.connect(mail.host, mail.from, mail.password);
t.sendMessage(newMessage, newMessage.getAllRecipients());
}catch(Exception e){
e.printStackTrace();
status = 2;
}finally{
t.close();
for(File f : mail.att)
f.deleteOnExit();
}
} catch (MessagingException e) {
e.printStackTrace();
status = 2;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
status = 2;
} finally{
mail.hassend = true;
String maillist = "";
for(String m : mail.to)
maillist += m + ";";
}
}