/** * Add an inline element to the MimeMessage, taking the content from a * {@code java.io.File}. * <p>The content type will be determined by the name of the given * content file. Do not use this for temporary files with arbitrary * filenames (possibly ending in ".tmp" or the like)! * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText}; * else, mail readers might not be able to resolve inline references correctly. * @param contentId the content ID to use. Will end up as "Content-ID" header * in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>". * Can be referenced in HTML source via src="cid:myId" expressions. * @param file the File resource to take the content from * @throws MessagingException in case of errors * @see #setText * @see #addInline(String, org.springframework.core.io.Resource) * @see #addInline(String, javax.activation.DataSource) */ public void addInline(String contentId, File file) throws MessagingException { Assert.notNull(file, "File must not be null"); FileDataSource dataSource = new FileDataSource(file); dataSource.setFileTypeMap(getFileTypeMap()); addInline(contentId, dataSource); }
/** * Add an inline element to the MimeMessage, taking the content from a * {@code org.springframework.core.io.Resource}. * <p>The content type will be determined by the name of the given * content file. Do not use this for temporary files with arbitrary * filenames (possibly ending in ".tmp" or the like)! * <p>Note that the InputStream returned by the Resource implementation * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke * {@code getInputStream()} multiple times. * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText}; * else, mail readers might not be able to resolve inline references correctly. * @param contentId the content ID to use. Will end up as "Content-ID" header * in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>". * Can be referenced in HTML source via src="cid:myId" expressions. * @param resource the resource to take the content from * @throws MessagingException in case of errors * @see #setText * @see #addInline(String, java.io.File) * @see #addInline(String, javax.activation.DataSource) */ public void addInline(String contentId, Resource resource) throws MessagingException { Assert.notNull(resource, "Resource must not be null"); String contentType = getFileTypeMap().getContentType(resource.getFilename()); addInline(contentId, resource, contentType); }
addInline(contentId, dataSource);
/** * Add an inline element to the MimeMessage, taking the content from a * {@code java.io.File}. * <p>The content type will be determined by the name of the given * content file. Do not use this for temporary files with arbitrary * filenames (possibly ending in ".tmp" or the like)! * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText}; * else, mail readers might not be able to resolve inline references correctly. * @param contentId the content ID to use. Will end up as "Content-ID" header * in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>". * Can be referenced in HTML source via src="cid:myId" expressions. * @param file the File resource to take the content from * @throws MessagingException in case of errors * @see #setText * @see #addInline(String, org.springframework.core.io.Resource) * @see #addInline(String, javax.activation.DataSource) */ public void addInline(String contentId, File file) throws MessagingException { Assert.notNull(file, "File must not be null"); FileDataSource dataSource = new FileDataSource(file); dataSource.setFileTypeMap(getFileTypeMap()); addInline(contentId, dataSource); }
/** * Add an inline element to the MimeMessage, taking the content from a * {@code org.springframework.core.io.Resource}. * <p>The content type will be determined by the name of the given * content file. Do not use this for temporary files with arbitrary * filenames (possibly ending in ".tmp" or the like)! * <p>Note that the InputStream returned by the Resource implementation * needs to be a <i>fresh one on each call</i>, as JavaMail will invoke * {@code getInputStream()} multiple times. * <p><b>NOTE:</b> Invoke {@code addInline} <i>after</i> {@link #setText}; * else, mail readers might not be able to resolve inline references correctly. * @param contentId the content ID to use. Will end up as "Content-ID" header * in the body part, surrounded by angle brackets: e.g. "myId" -> "<myId>". * Can be referenced in HTML source via src="cid:myId" expressions. * @param resource the resource to take the content from * @throws MessagingException in case of errors * @see #setText * @see #addInline(String, java.io.File) * @see #addInline(String, javax.activation.DataSource) */ public void addInline(String contentId, Resource resource) throws MessagingException { Assert.notNull(resource, "Resource must not be null"); String contentType = getFileTypeMap().getContentType(resource.getFilename()); addInline(contentId, resource, contentType); }
/** * 发送正文中有静态资源(图片)的邮件 * @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); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常!", e); } } }
addInline(contentId, dataSource);
messageHelper.addInline("picture12345", byteArrayResource, "image/png");
@RequestMapping("/inline") public void sendInlineMail() throws Exception { logger.info("发送嵌入静态资源的邮件..."); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom("576697722@qq.com"); helper.setTo("1241564291@qq.com"); 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); }
message.addInline("logo.jpg", new FileSystemResource("imgs/logo-pro.jpg"), "image/jpg"); } catch (IOException e) { e.printStackTrace();
@Override public void attach(MimeMessageHelper mimeMessageHelper) throws MessagingException { mimeMessageHelper.addInline(contentId, file); } }
@Override public void attach(MimeMessageHelper mimeMessageHelper) throws MessagingException { mimeMessageHelper.addInline(contentId, new InputStreamSource() { public InputStream getInputStream() { return new ByteArrayInputStream(content); } }, contentType); } }
JavaMailSender mailSender = new JavaMailSenderImpl(); // Or use Spring IoC to inject a pre-configured JavaMailSenderImpl MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // Add information to the message, such as subject, recipients, etc message.addInline("logo.png", imageSource);
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")) .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; }
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")) .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; }
MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message); helper.setFrom("no-reply@example.com", "xyz"); helper.setTo(email); helper.setText(body,true); helper.setSubject(subject); // use javax.mail.util.ByteArrayDataSource ByteArrayDataSource imgDS = new ByteArrayDataSource( imageByteArr, "image/png"); helper.addInline("cImage", imgDS); mailSender.send(message);
JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost("mail.host.com"); MimeMessage message = sender.createMimeMessage(); // use the true flag to indicate you need a multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("test@host.com"); // use the true flag to indicate the text included is HTML helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true); // let's include the infamous windows Sample file (this time copied to c:/) FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg")); helper.addInline("identifier1234", res); sender.send(message);
@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 "发送成功!"; }
@Override public void sendMail(Member member) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "utf-8"); messageHelper.setSubject("[Inline] 회원 가입 안내"); String htmlContent = "<strong>안녕하세요</strong>, 반갑습니다." + "<img src='cid:signature' />"; messageHelper.setText(htmlContent, true); messageHelper.setFrom("no-reply@madvirus.net", "운영자"); messageHelper.setTo(new InternetAddress(member.getEmail(), "utf-8")); messageHelper.addInline("signature", new FileDataSource("sign.jpg")); mailSender.send(message); } catch (Exception e) { e.printStackTrace(); } }
public void buildKycStartEmail(Optional<MimeMessageHelper> oMessage, String kycUrl) { if (oMessage.isPresent()) { try { Context context = new Context(); context.setVariable("logo", "logo"); context.setVariable("logoWidth", getLogoWidth()); context.setVariable("logoHeight", getLogoHeight()); context.setVariable("kycUrl", kycUrl); context.setVariable("entityName", this.mailServiceConfigHolder.getEntityName()); context.setVariable("year", this.mailServiceConfigHolder.getYear()); String html5Content = this.templateEngine.process("kyc_start_email", context); oMessage.get().setText(html5Content, true); oMessage.get().addInline("logo", this.logoContentData, getLogoContentType()); } catch (MessagingException e) { LOG.error("Error to add inline images to the message."); } } }