/** * Embeds a file in the HTML. This implementation delegates to * {@link #embed(File, String)}. * * @param file The <code>File</code> object to embed * @return A String with the Content-ID of the file. * @throws EmailException when the supplied <code>File</code> cannot be * used; also see {@link javax.mail.internet.MimeBodyPart} for definitions * * @see #embed(File, String) * @since 1.1 */ public String embed(final File file) throws EmailException { final String cid = EmailUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase(Locale.ENGLISH); return embed(file, cid); }
/** * Attempts to parse the specified <code>String</code> as a URL that will * then be embedded in the message. * * @param urlString String representation of the URL. * @param name The name that will be set in the filename header field. * @return A String with the Content-ID of the URL. * @throws EmailException when URL supplied is invalid or if {@code name} is null * or empty; also see {@link javax.mail.internet.MimeBodyPart} for definitions * * @see #embed(URL, String) * @since 1.1 */ public String embed(final String urlString, final String name) throws EmailException { try { return embed(new URL(urlString), name); } catch (final MalformedURLException e) { throw new EmailException("Invalid URL", e); } }
return embed(dataSource, name, cid);
return embed(new FileDataSource(file), file.getName(), cid);
return embed(new URLDataSource(url), name);
protected void addEmbeddables(HtmlEmail email) throws EmailException { for(Entry<String,DataSource> entry : toEmbed.entrySet()){ String key = entry.getKey(); String cid = email.embed(entry.getValue(),key); with(key, "cid:" + cid); } }
/** * Embeds a file in the HTML. This implementation delegates to * {@link #embed(File, String)}. * * @param file The <code>File</code> object to embed * @return A String with the Content-ID of the file. * @throws EmailException when the supplied <code>File</code> cannot be * used; also see {@link javax.mail.internet.MimeBodyPart} for definitions * * @see #embed(File, String) * @since 1.1 */ public String embed(final File file) throws EmailException { final String cid = EmailUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase(Locale.ENGLISH); return embed(file, cid); }
/** * Attempts to parse the specified <code>String</code> as a URL that will * then be embedded in the message. * * @param urlString String representation of the URL. * @param name The name that will be set in the filename header field. * @return A String with the Content-ID of the URL. * @throws EmailException when URL supplied is invalid or if {@code name} is null * or empty; also see {@link javax.mail.internet.MimeBodyPart} for definitions * * @see #embed(URL, String) * @since 1.1 */ public String embed(final String urlString, final String name) throws EmailException { try { return embed(new URL(urlString), name); } catch (final MalformedURLException e) { throw new EmailException("Invalid URL", e); } }
HtmlEmail email = new ImageHtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // embed the image and get the content id URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg(htmlEmailTemplate, new File("").toURI().toURL(), false);
return embed(dataSource, name, cid);
return embed(new FileDataSource(file), file.getName(), cid);
return embed(new URLDataSource(url), name);
if (inlineEmbeds != null) { for (Map.Entry<String, InlineImage> entry : inlineEmbeds.entrySet()) { htmlEmail.embed(entry.getValue().getDataSource(), entry.getKey(), entry.getValue().getCid());
public static void main(String[] args) throws EmailException, MalformedURLException { // 创建 Email Message HtmlEmail email = new HtmlEmail(); email.setHostName("mail.myserver.com"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("Test email with inline image"); // 嵌入图片 URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); String cid = email.embed(url, "Apache logo"); // 发送 HTML 内容 email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // 设置替代消息 email.setTextMsg("Your email client does not support HTML messages"); // 发送 email.send(); } }
private void assertCorrectContentType(final String picture, final String contentType) throws Exception { final HtmlEmail htmlEmail = createDefaultHtmlEmail(); final String cid = htmlEmail.embed(new File("./src/test/resources/images/" + picture), "Apache Logo"); final String htmlMsg = "<html><img src=\"cid:" + cid + "\"><html>"; htmlEmail.setHtmlMsg(htmlMsg); htmlEmail.buildMimeMessage(); final MimeMessage mm = htmlEmail.getMimeMessage(); mm.saveChanges(); final MimeMessageParser mmp = new MimeMessageParser(mm); mmp.parse(); final List<?> attachments = mmp.getAttachmentList(); assertEquals("Attachment size", 1, attachments.size()); final DataSource ds = (DataSource) attachments.get(0); assertEquals("Content type", contentType, ds.getContentType()); }
ValueMap props = ResourceUtil.getValueMap(resource); HtmlEmail email = new HtmlEmail(); String[] mailTo = props.get("mailto", new String[0]); email.setFrom((String)props.get("from")); for (String toAddr : mailTo) { email.addTo(toAddr); } //========Email Attachments=============== for (Map.Entry<String, RequestParameter[]> param : slingRequest.getRequestParameterMap().entrySet()) { RequestParameter rpm = param.getValue()[0]; if(!rpm.isFormField()) { EmailAttachment attachment = new EmailAttachment(); attachment.setPath(rpm.getFileName()); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Any Description"); attachment.setName("Any name you can set"); email.embed(new ByteArrayDataSource(rpm.get(), rpm.getContentType()), rpm.getFileName()); } } //========Email Attachment END=========== String emailTextToSend = "<p>Name: " + slingRequest.getParameter("name") + "</p>"; emailTextToSend += "<p>Message: " + slingRequest.getParameter("message") + "</p>"; email.setHtmlMsg(emailTextToSend); email.setSubject((String)props.get("subject")); MessageGatewayService messageGatewayService = sling.getService(MessageGatewayService.class); MessageGateway messageGateway = messageGatewayService.getGateway(HtmlEmail.class); messageGateway.send(email);
} else { FileDataSource dataSource = new FileDataSource(new File(attachment.getFilePath())); htmlEmail.embed(dataSource, attachment.getName(), attachment.getContentId());