Java Code Examples for org.springframework.mail.javamail.MimeMessageHelper#addInline()
The following examples show how to use
org.springframework.mail.javamail.MimeMessageHelper#addInline() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmailServiceImpl.java From graviteeio-access-management with Apache License 2.0 | 7 votes |
private String addResourcesInMessage(final MimeMessageHelper mailMessage, final String htmlText) throws Exception { final Document document = Jsoup.parse(htmlText); final List<String> resources = new ArrayList<>(); final Elements imageElements = document.getElementsByTag("img"); resources.addAll(imageElements.stream() .filter(imageElement -> imageElement.hasAttr("src")) .filter(imageElement -> !imageElement.attr("src").startsWith("http")) .map(imageElement -> { final String src = imageElement.attr("src"); imageElement.attr("src", "cid:" + src); return src; }) .collect(Collectors.toList())); final String html = document.html(); mailMessage.setText(html, true); for (final String res : resources) { final FileSystemResource templateResource = new FileSystemResource(new File(templatesPath, res)); mailMessage.addInline(res, templateResource, getContentTypeByFileName(res)); } return html; }
Example 2
Source File: MailServiceImpl.java From spring-boot-demo with MIT License | 7 votes |
/** * 发送正文中有静态资源的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param rscPath 静态资源地址 * @param rscId 静态资源id * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); if (ArrayUtil.isNotEmpty(cc)) { helper.setCc(cc); } FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); }
Example 3
Source File: MailController.java From spring-boot-study with Apache License 2.0 | 7 votes |
/** * 内联附件的邮件测试 * * @return success * @throws MessagingException */ @GetMapping("/inline-attach") public String inlineAttach() throws MessagingException { MimeMessage message = this.javaMailSender.createMimeMessage(); // 第二个参数表示是否开启multipart模式 MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(this.mailProperties.getUsername()); messageHelper.setTo("[email protected]"); messageHelper.setSubject("内联附件的邮件测试"); // 第二个参数表示是否html,设为true messageHelper.setText("<h1>HTML内容..<img src=\"cid:attach\"/></h1>", true); messageHelper.addInline("attach", new ClassPathResource("wx.jpg")); this.javaMailSender.send(message); return "success"; }
Example 4
Source File: MailServiceImpl.java From Java-API-Test-Examples with Apache License 2.0 | 6 votes |
/** * 发送正文中有静态资源(图片)的邮件 * @param to * @param subject * @param content * @param rscPath * @param rscId */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); log.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { log.error("发送嵌入静态资源的邮件时发生异常!", e); } }
Example 5
Source File: EmailServiceImpl.java From gravitee-management-rest-api with Apache License 2.0 | 6 votes |
private String addResourcesInMessage(final MimeMessageHelper mailMessage, final String htmlText) throws Exception { final Document document = Jsoup.parse(htmlText); final List<String> resources = new ArrayList<>(); final Elements imageElements = document.getElementsByTag("img"); resources.addAll(imageElements.stream() .filter(imageElement -> imageElement.hasAttr("src")) .filter(imageElement -> !imageElement.attr("src").startsWith("http")) .map(imageElement -> { final String src = imageElement.attr("src"); imageElement.attr("src", "cid:" + src); return src; }) .collect(Collectors.toList())); final String html = document.html(); mailMessage.setText(html, true); for (final String res : resources) { final FileSystemResource templateResource = new FileSystemResource(new File(templatesPath, res)); mailMessage.addInline(res, templateResource, getContentTypeByFileName(res)); } return html; }
Example 6
Source File: MailService.java From spring-boot-101 with Apache License 2.0 | 6 votes |
/** * 发送嵌入静态资源(一般是图片)的邮件 * @param content 邮件内容,需要包括一个静态资源的id,比如:<img src=\"cid:rscId01\" > * @param rscPath 静态资源路径和文件名 * @param rscId 静态资源id */ public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId){ MimeMessage message = sender.createMimeMessage(); try { MimeMessageHelper helper = setInfoByHelper(to, subject, content, message); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); sender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } }
Example 7
Source File: MailServiceImpl.java From spring-boot-demo with MIT License | 6 votes |
/** * 发送正文中有静态资源的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param rscPath 静态资源地址 * @param rscId 静态资源id * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); if (ArrayUtil.isNotEmpty(cc)) { helper.setCc(cc); } FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); }
Example 8
Source File: MailTests.java From SpringBootUnity with MIT License | 6 votes |
@Test public void sendInlineMail() throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom("[email protected]"); helper.setTo("[email protected]"); helper.setSubject("主题:嵌入静态资源"); helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true); FileSystemResource file = new FileSystemResource(new File("weixin.jpg")); helper.addInline("weixin", file); mailSender.send(mimeMessage); }
Example 9
Source File: MailController.java From springBoot-study with Apache License 2.0 | 6 votes |
@PostMapping("/sendInlineMail") public String sendInlineMail(@RequestBody Mail mail) throws Exception { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(mail.getSender()); helper.setTo(mail.getReceiver()); helper.setSubject(mail.getSubject()); //这里的text 是html helper.setText(mail.getText(), true); FileSystemResource file = new FileSystemResource(new File("1.png")); helper.addInline("文件", file); mailSender.send(mimeMessage); return "发送成功!"; }
Example 10
Source File: SpitterMailServiceImpl.java From Project with Apache License 2.0 | 6 votes |
/** * <p>描述:采用Thymeleaf模板的富文本email</p> * @param to * @param spittle * @throws MessagingException * @author lzc */ public void sendRichEmailWithThymeleaf(String to, Spittle spittle) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); String spitterName = spittle.getSpitter().getFullName(); helper.setFrom("[email protected]"); helper.setTo(to); helper.setSubject("New spittle from " + spitterName); Context context = new Context(); context.setVariable("spitterName", spitterName); context.setVariable("spittleText", spittle.getText()); String emailText = thymeleaf.process("emailTemplate.html", context); helper.setText(emailText, true); ClassPathResource couponImage = new ClassPathResource("/collateral/coupon.jpg"); helper.addInline("spitterLogo", couponImage); mailSender.send(message); }
Example 11
Source File: SpitterMailServiceImpl.java From Project with Apache License 2.0 | 6 votes |
/** * <p>描述:采用Velocity模板的富文本email</p> * @param to * @param spittle * @throws MessagingException */ public void sendRichEmailWithVelocity(String to, Spittle spittle) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); String spitterName = spittle.getSpitter().getFullName(); helper.setFrom("[email protected]"); helper.setTo(to); helper.setSubject("New spittle from " + spitterName); Map<String, Object> model = new HashMap<String, Object>(); model.put("spitterName", spitterName); model.put("spittleText", spittle.getText()); String emailText = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "emailTemplate.vm", "UTF-8", model); helper.setText(emailText, true); ClassPathResource couponImage = new ClassPathResource("/collateral/coupon.jpg"); helper.addInline("spitterLogo", couponImage); mailSender.send(message); }
Example 12
Source File: EmailAction.java From spring-boot-examples with Apache License 2.0 | 6 votes |
@PostMapping(value = "html_with_img") public String sendHtmlWithImg(String msg, String email) { if (StringUtils.isEmpty(msg) || StringUtils.isEmpty(email)) { return "请输入要发送消息和目标邮箱"; } try { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(message, true); messageHelper.setFrom(sendName); messageHelper.setTo(email); messageHelper.setSubject("带静态资源图片的HTML邮件"); String html = "<div><h1><a name=\"hello\"></a><span>Hello</span></h1><blockquote><p><span>this is a html email.</span></p></blockquote><p> </p><p><span>" + msg + "</span></p><img src='cid:myImg' /></div>"; messageHelper.setText(html, true); File file = new File("src/main/resources/wei.jpg"); messageHelper.addInline("myImg", file); mailSender.send(message); return "发送成功"; } catch (MessagingException e) { e.printStackTrace(); return "发送失败:" + e.getMessage(); } }
Example 13
Source File: MailServiceImpl.java From spring-boot-demo with MIT License | 6 votes |
/** * 发送正文中有静态资源的邮件 * * @param to 收件人地址 * @param subject 邮件主题 * @param content 邮件内容 * @param rscPath 静态资源地址 * @param rscId 静态资源id * @param cc 抄送地址 * @throws MessagingException 邮件发送异常 */ @Override public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); if (ArrayUtil.isNotEmpty(cc)) { helper.setCc(cc); } FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); }
Example 14
Source File: MailComposerImpl.java From yes-cart with Apache License 2.0 | 6 votes |
/** * Add inline resource to mail message. * Resource id will be interpreted as file name in following fashion: filename_ext. * * @param helper MimeMessageHelper, that has mail message * @param htmlTemplate html message template * @param mailTemplateChain physical path to resources * @param shopCode shop code * @param locale locale * @param templateName template name * * @throws javax.mail.MessagingException in case if resource can not be inlined */ void inlineResources(final MimeMessageHelper helper, final String htmlTemplate, final List<String> mailTemplateChain, final String shopCode, final String locale, final String templateName) throws MessagingException, IOException { if (StringUtils.isNotBlank(htmlTemplate)) { final List<String> resourcesIds = getResourcesId(htmlTemplate); if (!resourcesIds.isEmpty()) { for (String resourceId : resourcesIds) { final String resourceFilename = transformResourceIdToFileName(resourceId); final byte[] content = mailTemplateResourcesProvider.getResource(mailTemplateChain, shopCode, locale, templateName, resourceFilename); helper.addInline(resourceId, new ByteArrayResource(content) { @Override public String getFilename() { return resourceFilename; } }); } } } }
Example 15
Source File: JavaMailUtils.java From seppb with MIT License | 6 votes |
private static void buildMimeMessage(MailDTO mailDTO, MimeMessage mimeMessage) throws MessagingException { MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(verifyNotNull(mailDTO.getFrom(), "邮件发送人不能为空")); helper.setTo(verifyNotNull(mailDTO.getTo(), "邮件接收人不能为空")); helper.setText(mailDTO.getContent(), mailDTO.isHtml()); helper.setSubject(mailDTO.getSubject()); if (!isEmpty(mailDTO.getTocc()) && isNotBlank(mailDTO.getTocc()[0])) { helper.setCc(mailDTO.getTocc()); } if (mailDTO.isHasImage()) { helper.addInline(mailDTO.getImageId(), mailDTO.ResourceImage()); } if (mailDTO.isHasAttachment()) { FileSystemResource docx = new FileSystemResource(new File(mailDTO.getAttachmentPath())); helper.addAttachment(mailDTO.getAttachmentName(), docx); } }
Example 16
Source File: MailTest.java From SpringBootLearn with Apache License 2.0 | 5 votes |
/** * 发送静态邮箱 * @throws Exception */ @Test public void sendStaticMail() throws Exception { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(username); helper.setTo("[email protected]"); helper.setSubject("测试主题:嵌入静态资源"); helper.setText("<html><body><img src=\"cid:weixin_qrcode\" ></body></html>", true); FileSystemResource file = new FileSystemResource(new File("weixin_qrcode.jpg")); // addInline函数中资源名称jpg需要与正文中cid:weixin_qrcode对应起来 helper.addInline("weixin_qrcode", file); javaMailSender.send(mimeMessage); }
Example 17
Source File: EmailServiceImpl.java From tutorials with MIT License | 5 votes |
private void sendHtmlMessage(String to, String subject, String htmlBody) throws MessagingException { MimeMessage message = emailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setFrom(NOREPLY_ADDRESS); helper.setTo(to); helper.setSubject(subject); helper.setText(htmlBody, true); helper.addInline("attachment.png", resourceFile); emailSender.send(message); }
Example 18
Source File: MailService.java From SpringBoot-Course with MIT License | 5 votes |
/** * 发送html内嵌图片的邮件 * @param to * @param subject * @param content * @param srcPath * @param srcId * @throws MessagingException */ public void sendHtmlInlinePhotoMail(String to, String subject, String content, String srcPath, String srcId) throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource fileSystemResource = new FileSystemResource(new File(srcPath)); helper.addInline(srcId, fileSystemResource); javaMailSender.send(message); }
Example 19
Source File: EmailService.java From jakduk-api with MIT License | 5 votes |
/** * Send HTML mail with inline image */ public void sendMailWithInline(final String recipientName, final String recipientEmail, final Locale locale) throws MessagingException { // Prepare the evaluation context final Context ctx = new Context(locale); ctx.setVariable("name", recipientName); ctx.setVariable("subscriptionDate", new Date()); ctx.setVariable("hobbies", Arrays.asList("Cinema", "Sports", "Music")); // Prepare message using a Spring helper final MimeMessage mimeMessage = mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true /* multipart */, "UTF-8"); message.setSubject("Example HTML email with inline image"); message.setFrom("[email protected]"); message.setTo(recipientEmail); // Create the HTML body using Thymeleaf final String htmlContent = htmlTemplateEngine.process("mail/email-inlineimage", ctx); message.setText(htmlContent, true /* isHtml */); // Add the inline image, referenced from the HTML code as "cid:${imageResourceName}" // final InputStreamSource imageSource = new ByteArrayResource(imageBytes); // message.addInline(imageResourceName, imageSource, imageContentType); message.addInline("sample-image", new ClassPathResource("public/images/logo_type_A_en.png"), "image/png"); // Send mail mailSender.send(mimeMessage); }
Example 20
Source File: MailController.java From spring-boot-study with MIT License | 5 votes |
/** * 发送带静态资源的邮件 * */ @GetMapping("/sendInlineMail") public String sendInlineMail(){ MimeMessage message=null; try{ message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setFrom(sendUser); helper.setTo("[email protected]"); helper.setSubject("主题邮件"); StringBuilder sb=new StringBuilder(); sb.append("<h1>尊敬的客户您好!</h1>") .append("<p>欢迎您访问我的博客 www.fishpro.com.cn</p><p><img src='cid:pic' /></p>"); helper.setText(sb.toString(),true); //获取附件资源 FileSystemResource fileSystemResource =new FileSystemResource(new File("")); //把附件资源加入到发送消息中 helper.addInline("pic",fileSystemResource); }catch (Exception e){ e.printStackTrace(); return "fail"; } mailSender.send(message); return "success"; }