spring邮件发送的第三种实现方式

主要代码:

private class SendMailThread implements Runnable{
		private Message msg;
		private Integer[] userIds;
		private AttachDto ad;
		private String[] newNames;
		private String realPath;
		private UserEmail ue;
		
	    public SendMailThread(UserEmail ue,Message msg, Integer[] userIds, AttachDto ad,String[] newNames,String realPath) {
			super();
			this.msg = msg;
			this.userIds = userIds;
			this.ad = ad;
			this.newNames=newNames;
			this.realPath = realPath;
			this.ue = ue;
		}

		@Override
	    public void run() {
			sendMail(ue,msg,userIds,ad,newNames,realPath);
	    }
	}

private void sendMail(UserEmail ue,Message msg, Integer[] userIds, AttachDto ad,String[] newNames,String realPath) {
		try {
			System.out.println("邮件开始发送");
			JavaMailSenderImpl jms = (JavaMailSenderImpl)mailSender;
			jms.setHost(ue.getHost());
			jms.setPassword(ue.getPassword());
			jms.setProtocol(ue.getProtocol());
			jms.setUsername(ue.getUsername());
			
			MimeMessage email =  mailSender.createMimeMessage();
			MimeMessageHelper helper = new MimeMessageHelper(email,true,"utf-8");
			helper.setFrom(ue.getUser().getEmail());
			helper.setSubject(msg.getTitle());
			List<String> mailTos = userDao.listAllSendEmail(userIds);
            for(String mail:mailTos){
            	helper.addTo(mail);
            }
			String uploadPath = ad.getUploadPath();
			if(ad.isHasAttach()){
				File[] atts = ad.getAtts();
				String[] filenames = ad.getAttsFilename();
				for(int i=0;i<atts.length;i++){
					String fn = filenames[i];
					helper.addAttachment(MimeUtility.encodeText(fn), new FileSystemResource(uploadPath+"/"+newNames[i]));
				}
			}
			String content = msg.getContent();
			Map<String,String> imgMaps = new HashMap<String,String>();
			List<String> imgs = listContentImgUrl(content);
			int i=0;
			for(String img:imgs){
				imgMaps.put("x"+i, img);
				content = content.replace(img, "cid:"+("x"+i));
				i++;
			}
			helper.setText(content,true);
			Set<String> keys = imgMaps.keySet();
			for(String key:keys){
				String url = imgMaps.get(key);
				if(url.startsWith("http://")){
					helper.addInline(key,new URLDataSource(new URL(url)));
				}else{
                    String path = realPath.replace("upload", "")+url;
                    helper.addInline(key, new FileSystemResource(path));
				}
			}
			jms.send(email);
			System.out.println("邮件发送成功");
		} catch (MailException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}

public void add(Message msg, Integer[] userIds,AttachDto ad,int isEmail) throws IOException {
        msg.setUser(SystemContext.getLoginUser());
        msg.setCreateDate(new Date());
        this.messageDao.add(msg);
        UserMessage um = null;
        List<User> users = this.userDao.listByIds(userIds);
        for(User u:users){
        	um = new UserMessage();
        	um.setIsRead(0);
        	um.setMessage(msg);
        	um.setUser(u);
        	messageDao.addObj(um);
        }
       String[] newNames = DocumentUtil.addAttach(ad, attachmentDao, msg, null);
       if(isEmail>0){
    	   UserEmail ue = this.userDao.loadUserEmail(SystemContext.getLoginUser().getId());
    	   taskExecutor.execute(new SendMailThread(ue,msg, userIds, ad,newNames,SystemContext.getRealPath()));
       }
	}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值