在实际运用中,比如你淘宝购物,申请退款,这时你在邮箱中看到退款邮件,或者你注册某个账号,申请验证邮箱通知等等,这些都是邮件发送,这里将介绍下系统捕获异常发送邮件案例。
准备工作:
eclipse4.5 64位
jdk1.7 64位
邮件发送所需jar:
fastjson-1.1.24.jar,javax.mail-1.5.6.jar
类Developer:
枚举类型,发送邮件人姓名和邮箱地址
package mail; /** * @class:Developer *@descript:枚举类型,发送邮件人姓名和邮箱地址 *@date:2016年10月26日 下午8:07:50 *@author sanghaiqin *@version:V1.0 */ public enum Developer { zhoujing("周静","405687038@qq.com"),peiyuxiang("裴玉翔","498736875@qq.com"),yipeng("乙鹏","729325112@qq.com"),liuan("刘安","2211747233@qq.com"),chenyuhao("陈宇豪","631604198@qq.com"),wangdong("王栋","1217295649@qq.com"),sanghaiqin("桑海芹","1522580013@qq.com"); //发件人姓名 private String name; //发件人email private String mail; private Developer() { } private Developer(String name,String mail) { this.name = name; this.mail = mail; } /** * @descript:传递发件人姓名得到该发件人的邮箱 * @param name 发件人姓名 * @return */ public static String getMail(String name) { for (Developer c : Developer.values()) { if (c.getName().equals(name)) { return c.mail; } } return null; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } }
类ExceptionInfo:发件人信息
package mail; /** * @class:ExceptionInfo *@descript:发件人信息 *@date:2016年10月26日 下午8:11:27 *@author sanghaiqin *@version:V1.0 */ public class ExceptionInfo { //发件人姓名 private String developer; //发件人方法 private String method; //发件人url private String url; //发件人捕获异常信息 private Exception e; /** * @param developer 发件人姓名 * @param method 发件人方法 * @param url 发件人url * @param e 发件人捕获异常信息 */ public ExceptionInfo(String developer,String method,String url,Exception e) { super(); this.developer = developer; this.method = method; this.url = url; this.e = e; } public String getDeveloper() { return developer; } public void setDeveloper(String developer) { this.developer = developer; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Exception getE() { return e; } public void setE(Exception e) { this.e = e; } }
类MailSenderInfo:发送邮箱信息
package mail; import java.util.Properties; /** * @class:MailSenderInfo *@descript:发送邮箱信息 *@date:2016年10月26日 下午8:14:22 *@author sanghaiqin *@version:V1.0 */ public class MailSenderInfo { //发送邮件的服务器的IP private String mailServerHost; //发送邮件的服务器的端口默认为25 private String mailServerPort = "25"; // 邮件发送者的地址 private String fromAddress; // 邮件接收者的地址 private String toAddress; // 登陆邮件发送服务器的用户名 private String username; // 登陆邮件发送服务器的密码 private String password; // 是否需要身份验证 private boolean validate = false; // 邮件主题 private String subject; // 邮件的文本内容 private String content; // 邮件附件的文件名 private String[] attachFileNames; public MailSenderInfo() { super(); } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } /** * @descript:获得邮件会话属性 * @return */ public Properties getProperties() { PropertyUtil propertyUtil = new PropertyUtil(); Properties properties =propertyUtil.readProperties(); return properties; } }
类MyAuthenticator:用户验证
package mail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * @class:MyAuthenticator *@descript:用户验证 *@date:2016年10月26日 下午8:57:45 *@author sanghaiqin *@version:V1.0 */ public class MyAuthenticator extends Authenticator { //用户名 String username = null; //密码 String password = null; public MyAuthenticator() { } public MyAuthenticator(String username,String password) { this.username = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password); } }
类PropertyUtil:获得properties文件工具类
package mail; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * @class:PropertyUtil *@descript:获得properties文件工具类 *@date:2016年10月26日 下午8:20:10 *@author sanghaiqin *@version:V1.0 */ public class PropertyUtil { /** * @descript:加载资源文件 * @param resources 资源文件 * @return * @throws FileNotFoundException */ private Properties loadProperties(String resources) { InputStream inputstream = null; Properties properties = new Properties(); // 使用InputStream得到一个资源文件 try { inputstream = new FileInputStream(resources); // 加载配置文件 properties.load(inputstream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(inputstream!=null){ try { inputstream.close(); } catch (IOException e) { e.printStackTrace(); } } } return properties; } /** * @descript:读属性文件 * @return * @throws FileNotFoundException */ public Properties readProperties(){ String resources = PropertyUtil.class.getClassLoader().getResource("prop.properties").getPath(); Properties properties = loadProperties(resources); return properties; } /** * @descript:测试 * @param args */ public static void main(String[] args) { PropertyUtil p=new PropertyUtil(); Properties pro=p.readProperties(); String mailSenderUsername=(String) pro.get("mail.sender.username"); System.out.println("邮件发送者用户名:"+mailSenderUsername);//neo_lifes@163.com String path = PropertyUtil.class.getClassLoader().getResource("prop.properties").getPath(); System.out.println(path);// /G:/Workspaces4.4/test/bin/prop.properties } }