利用JavaMail发送邮件的Java类
利用JavaMail发送邮件需要用到mail.jar和activtion.jar两个包。
该类实现了较完整的邮件发送成果,包罗以HTML名目发送,添加附件和抄送人。下面是详细的代码:
Mail.java:
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; } } /** * 配置发信人 * 查察本栏目public static void main(String[] args){ String smtp = "SMTP处事器"; String from = "发信人"; String to = "收信人"; String copyto = "抄送人"; String subject = "邮件主题"; String content = "邮件内容"; String username="用户名"; String password="暗码"; String filename = "附件路径,如:F:\\条记<a>\\struts2</a>与mvc.txt"; Mail.sendAndCc(smtp, from, to, copyto, subject, content, username, password, filename); }