public void SendMail(String correo, String ruta) throws FileNotFoundException, UnsupportedEncodingException { SendgridEmailServiceProxy envio = new SendgridEmailServiceProxy(); envio.setEndpoint(PropertiesUtil.getInstance().recuperaValor("EnvioMail")); String emailFrom = PropertiesUtil.getInstance().recuperaValor("FROM"); String[] emailTo = new String[1]; emailTo[0] = correo; String[] cc = new String[1]; String subject = PropertiesUtil.getInstance().recuperaValor("subject"); String message = PropertiesUtil.getInstance().recuperaValor("body"); boolean isHtml = true; VariablesDTO[] variables = new VariablesDTO[1]; VariablesDTO v = new VariablesDTO(); v.setName("jjj"); v.setValue("dddd"); variables[0] = v; AttachmentDTO[] attachments = new AttachmentDTO[1]; AttachmentDTO att = new AttachmentDTO(); att.setFileName(getName(ruta)); att.setBase64File(getArchivo(ruta)); attachments[0] = att; try { envio.sendSendGridEmail( emailFrom, emailTo, cc, subject, message, variables, attachments, isHtml); } catch (RemoteException e) { logg.error(e); } }
/* * 根据配置文件中key获得模块 */ public static InputStream getTemplateByKey(String key) { try { if (key != null && !"".equals(key)) { String templatePath = PropertiesUtil.getInstance().getProperty("excelExportTemplatePath"); String k_v = PropertiesUtil.getInstance().getProperty(key); if (k_v != null && !"".equals(k_v)) { String realPath = templatePath + "/" + new String(k_v.getBytes(), "UTF-8"); File file = new File(realPath); if (!file.exists()) { log.info(realPath + ":对应文件不存在!"); FileException fe = new FileException(FileExceptionType.FILE_EXCEPTION_ALLOWEDTYPES); throw new GeneralException(fe.getErrorCode(), fe.getMessage(), fe, new Object[] {}) {}; } return new FileInputStream(file); } else { log.info("配置文件中没有对应模块的键值对!"); FileException fe = new FileException(FileExceptionType.FILE_EXCEPTION_PROPKEYVALNOTEXISTS); throw new GeneralException(fe.getErrorCode(), fe.getMessage(), fe, new Object[] {}) {}; } } } catch (Exception e) { e.printStackTrace(); } return null; }
/* * 创建文件名 */ @SuppressWarnings("serial") public static String createFileName(String fileName) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_"); String name = formatter.format(new Date()) + UUID.randomUUID().toString(); if (fileName.lastIndexOf(".") != -1) { // 得到后缀名 String extension = fileName.substring(fileName.lastIndexOf(".")); String allowedTypes = PropertiesUtil.getInstance().getProperty("allowedTypes").trim(); if (allowedTypes == null || "".equals(allowedTypes) || allowedTypes.indexOf(extension.substring(1, extension.length()).toLowerCase()) < 0) { FileException fe = new FileException(FileExceptionType.FILE_EXCEPTION_ALLOWEDTYPES); throw new GeneralException(fe.getErrorCode(), fe.getMessage(), fe, new Object[] {}) {}; } name = name + extension; } return name; }