data:image/gif;base64
? protected boolean sendHtmlEmail(String fromAddress, String fromName, String toAddress, String toName, String subject, String textContent, String htmlContent, String serverName, String serverPort, String serverPath, String docRoot) { boolean result = true; HtmlEmail email = new HtmlEmail(); try { // Setup email parameters email.setHostName(SMTP_HOST); email.setSmtpPort(SMTP_PORT); email.setCharset("UTF-8"); email.addTo(toAddress, toName); email.setFrom(fromAddress, fromName); email.setSubject(subject); // Add text body if (!"".equals(textContent)) { email.setTextMsg(textContent); } // Add HTML body with inline images if (!"".equals(htmlContent)) { // System.out.println("htmlContent src: " + htmlContent); Matcher matcher = ManagerBase.imageSrcPattern.matcher(htmlContent); //URL imageURL = null; String imageCID = null; while (matcher.find()) { //System.out.println("sendHtmlEmail:"); //System.out.println("imageURL done: " + imageURL.toString()); //ClassLoader classLoader = getClass().getClassLoader(); //File file = new File(classLoader.getResource("../../" + matcher.group(2)).getFile()); // File image = new File(docRoot + matcher.group(2)); // imageCID = email.embed(image/*, matcher.group( 2 )*/); // System.out.println("imageCID done:" + imageCID); // htmlContent = htmlContent.replaceFirst(matcher.group(2), "cid:" + imageCID); } //System.out.println("htmlContent done: " + htmlContent); email.setHtmlMsg(htmlContent); } // Build && send email //email.buildMimeMessage(); result = result && filterDebugMail(toAddress, errors); if (result == true) email.send(); } catch (Exception e) { // ...... , // ...... :) } return result; }
while (matcher.find()) { // , src cid:[ ] String cidInFile = matcher.group(2); // String imageName = cidInFile.substring(cidInFile.indexOf(":")+1); // String imageFullResPath = "/images/print/" + imageName + ".gif"; }
while (matcher.find()) { // , src cid:[ ] String cidInFile = matcher.group(2); // String imageName = cidInFile.substring(cidInFile.indexOf(":")+1); // String imageFullResPath = "/images/print/" + imageName + ".gif"; // ClassLoader classLoader = getClass().getClassLoader(); InputStream is = classLoader.getResourceAsStream(imageFullResPath); byte[] imageData = IOUtils.toByteArray(is); // embed y DataSource DataSource ds = new ByteArrayDataSource(imageData, "image/gif"); String cid = email.embed(ds, imageName); }
protected boolean sendHtmlEmail(String fromAddress, String fromName, String toAddress, String toName, String subject, String textContent, String htmlContent, String serverName, String serverPort, String serverPath, String docRoot) { boolean result = true; ImageHtmlEmail email = new ImageHtmlEmail(); try { // Setup email parameters email.setHostName(SMTP_HOST); email.setSmtpPort(SMTP_PORT); email.setCharset("UTF-8"); email.addTo(toAddress, toName); email.setFrom(fromAddress, fromName); email.setSubject(subject); // Add text body if (!"".equals(textContent)) { email.setTextMsg(textContent); } // Add HTML body with inline images if (!"".equals(htmlContent)) { URL resurl = getClass().getResource("/"); URI resURI = resurl.toURI(); File resFolder = new File( resURI ); resFolder = resFolder.getParentFile(); DataSourceResolver resolver = new DataSourceFileResolver(resFolder); email.setDataSourceResolver(resolver); Matcher matcher = ManagerBase.imageSrcPattern.matcher(htmlContent); while (matcher.find()) { // , src cid:[ ] String cidInFile = matcher.group(2); // String imageName = cidInFile.substring(cidInFile.indexOf(":")+1); // String imageFullResPath = "/images/print/" + imageName + ".gif"; // ClassLoader classLoader = getClass().getClassLoader(); InputStream is = classLoader.getResourceAsStream(imageFullResPath); byte[] imageData = IOUtils.toByteArray(is); // embed y DataSource DataSource ds = new ByteArrayDataSource(imageData, "image/gif"); String cid = email.embed(ds, imageName); } // email ready, enjoy your transfer email.setHtmlMsg(htmlContent); } result = result && filterDebugMail(toAddress, errors); if (result == true) email.send(); } catch (Exception e) { // ......... } return result; }
Source: https://habr.com/ru/post/313346/
All Articles