1 /**
 2  * 类文件说明
 3  * 
 4  */
 5 public class SendMail {
 6     
 7     Logger log = Logger.getLogger(SendMail.class);
 8 
 9     /**
10      * 发送邮件
11      * 
12      * @param toUser
13      *            接收邮件者(多人时,邮件地址以,分隔)
14      * @param subject
15      *            邮件标题
16      * @param content
17      *            邮件内容
18      */
19     public void send(String toUser, String subject, String content) {
20         try {
21             InputStream inputStream = this.getClass().getClassLoader()
22                     .getResourceAsStream("mail.properties");
23             Properties p = new Properties();
24             p.load(inputStream);
25             // 这个类主要是设置邮件
26             MailSenderInfo mailInfo = new MailSenderInfo();
27             mailInfo.setMailServerHost(p.getProperty("MailServerHost"));
28             mailInfo.setMailServerPort(p.getProperty("MailServerPort"));
29             mailInfo.setUserName(p.getProperty("UserName"));
30             mailInfo.setPassword(p.getProperty("Password"));
31             mailInfo.setValidate(true);
32             mailInfo.setFromAddress(p.getProperty("FromAddress"));
33             if(null != toUser && !"".equals(toUser)){
34                 String [] tos = toUser.split(",");
35                 if(tos.length>1){
36                     mailInfo.setReceivers(tos);
37                 }else {
38                     mailInfo.setToAddress(toUser);
39                 }
40             }
41             mailInfo.setSubject(subject);
42             mailInfo.setContent(content);
43             // 这个类主要来发送邮件
44             com.hys.zyy.manage.mail.SimpleMailSender sms = new com.hys.zyy.manage.mail.SimpleMailSender();
45             sms.sendHtmlMail(mailInfo);// 发送文体格式
46             log.error(String.format("send mail %s",mailInfo.getToAddress()));
47         } catch (Exception e) {
48             e.printStackTrace();
49         }
50     }
51 
52     public static void main(String[] args) {
53         SendMail mail = new SendMail();
54         mail.send("wangbaochang@mail.haoyisheng.com", "邮件测试", "测试是时尚!!!");
55     }
56 
57 }

 

posted on 2016-07-25 11:26  Sharpest  阅读(493)  评论(0编辑  收藏  举报