邮件异步发送

1.邮件异步发送

	/** 
     * 异步方式发送邮件 
     * @param id 
     * @param other 
     * @param title 
     * @param context 
     */  
    public void sendMailByAsynchronous(final String id, final String other,  
            final String title, final String context) {  
  
        new Thread(new Runnable() {  
            public void run() {  
                try {  
                	mailSendService.sendMail(id,
							null, title, context);  
                } catch (Exception ex) {
                }  
            }  
        }).start();  
    }

2.邮件信息处理

public String sendMail(String ids, String others, String title, String context) {
        String msg = "0";
        
        String host = SysConfig.getInstance().getProperty("mail", "mail.server");
        String from = SysConfig.getInstance().getProperty("mail", "mail.name");
        String pwd = SysConfig.getInstance().getProperty("mail", "mail.password");
        
        Mail ms = new Mail(host, from, pwd, title, context);
        
        if (StringUtil.isEmpty(host) || StringUtil.isEmpty(from) || StringUtil.isEmpty(pwd)) {
            msg = "邮件服务器未配置,请联系管理人员!";
            return msg;
        }
        // 收件人
        List<String> targets = new ArrayList<>();
        if (ids != null && ids.length() < 1 && others!= null && others.length() < 1) {
            msg = "收件人为空!";
            return msg;
        }
        
        if (ids != null && ids.length() > 0) {
            String[] idArray = ids.split(",");
            for (String id : idArray) {
                User user = userService.getUserByid(Integer.parseInt(id));
                if (user != null) {
                    String email = user.getEmail();
                    if (!StringUtil.isEmpty(email) && !targets.contains(email)) {
                        targets.add(email);
                    } else {
                    	log.warn(user.getName() + "未配置邮件,请为该用户配置邮件后重试!");
                    }
                }
            }
        }
        
        if (others!= null && others.length() > 0) {
            String[] otherArray = others.split(";");
            for (String other : otherArray) {
                if (!targets.contains(other)) {
                    targets.add(other);
                }
            }
        }
        for (String target : targets) {
            try {
                ms.addTarget(target);
            } catch (Exception e) {
                msg = "添加邮件接收人失败!";
                this.log.error("添加邮件接收人失败: " + target + "\n" + e.getStackTrace());
                e.printStackTrace();
            }
        }
        try {
        	mailSend.sendmail(ms);
//        	SysConfig.getInstance().addMailSend(ms);
        } catch (Exception e) {
            msg = "发送邮件失败!";
            this.log.error("发送邮件失败: " + "\n" + e.getStackTrace());
            e.printStackTrace();
        }
        return msg;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值