@Override public ImageHtmlEmail get() { return factory.newEmail(new ImageHtmlEmail()); }
/** * Does the work of actually building the MimeMessage. * * @see org.apache.commons.mail.HtmlEmail#buildMimeMessage() * @throws EmailException building the MimeMessage failed */ @Override public void buildMimeMessage() throws EmailException { try { // embed all the matching image and script resources within the email String temp = replacePattern(super.html, IMG_PATTERN); temp = replacePattern(temp, SCRIPT_PATTERN); setHtmlMsg(temp); super.buildMimeMessage(); } catch (final IOException e) { throw new EmailException("Building the MimeMessage failed", e); } }
final ImageHtmlEmail email = new ImageHtmlEmail(); email.setAuthenticator(new DefaultAuthenticator(senderEmailUsername, senderEmailPassword)); } else { email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword)); email.setHostName(senderEmailHostName); email.setSmtpPort(senderEmailPort); email.setStartTLSEnabled(senderEmailTlsEnabled); email.setDataSourceResolver(new DataSourceClassPathResolver("/", true)); email.setSocketTimeout(socketTimeout); email.setSocketConnectionTimeout(socketConnectionTimeout); final Properties properties = email.getMailSession().getProperties(); email.setFrom(senderEmailAddress); email.setSubject(subject); email.setHtmlMsg(body); email.attach(attachment, attachment.getName(), ""); email.addTo(toListElseOverride); email.addCc(ccListElseOverride); email.addBcc(bccListElseOverride);
public static void main(String[] args) throws EmailException, MalformedURLException { // 加载邮件模板 String htmlEmailTemplate = ".... <img src=\"http://www.apache.org/images/feather.gif\"> ...."; // 定义URL资源 URL url = new URL("http://www.apache.org"); // 创建 Email Message ImageHtmlEmail email = new ImageHtmlEmail(); email.setDataSourceResolver(new DataSourceUrlResolver(url)); 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"); // 设置 html 内容 email.setHtmlMsg(htmlEmailTemplate); // 设置替代消息 email.setTextMsg("Your email client does not support HTML messages"); // 发送 email.send(); } }
ImageHtmlEmail htmlEmail = new ImageHtmlEmail(); htmlEmail.setFrom(finalMailFromAddress, finalMailFromName); htmlEmail.setFrom(finalMailFromAddress); htmlEmail.setBounceAddress(finalMailBounceAddress); htmlEmail.addReplyTo(finalMailReplyToAddress); htmlEmail.setSubject(finalMailSubject); htmlEmail.setDataSourceResolver(new DataSourceClassPathResolver()); htmlEmail.setHtmlMsg(htmlBody); htmlEmail.setTextMsg(mailText); htmlEmail.addTo(mailTo); htmlEmail.setHostName(smtpServer); htmlEmail.setSmtpPort(smtpPort); if ( (smtpSecurityUsername != null) && (smtpSecurityPassword != null) ) htmlEmail.setAuthentication(smtpSecurityUsername, smtpSecurityPassword); htmlEmail.setSSLOnConnect(smtpSecuritySsl); htmlEmail.send();
@Test public void testSendEmptyHTML() throws Exception { Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST); // Create the email message final ImageHtmlEmail email = new ImageHtmlEmail(); // set the html message try { email.setHtmlMsg(null); fail("Should fail here!"); } catch (final EmailException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid message supplied")); } }
/** * Test sending a image HTML mail bases on a local HTML page and local image. * * @throws Exception the test failed */ @Test public void testImageHtmlEmailLocal() throws Exception { // use a simple HTML page with one image final File htmlFile = new File("./src/test/resources/html/www.apache.org.html"); final String htmlMsg1 = FileUtils.readFileToString(htmlFile, "ISO-8859-1"); final ImageHtmlEmail email = (ImageHtmlEmail) create(ImageHtmlEmail.class); email.setDataSourceResolver(new DataSourceUrlResolver(htmlFile.getParentFile().toURI().toURL(), false)); email.setSubject("[testImageHtmlEmail] 1.Test: simple html content"); email.setHtmlMsg(htmlMsg1); EmailUtils.writeMimeMessage( new File("./target/test-emails/testImageHtmlEmailLocal.eml"), send(email).getMimeMessage()); }
import javax.mail.util.ByteArrayDataSource; import org.apache.commons.mail.ImageHtmlEmail; ... ImageHtmlEmail email = new ImageHtmlEmail(); byte[] qrImageBytes = createQRCode(); // get your image byte array ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png"); String contentId = email.embed(qrImageDataSource, "QR Image");
dataSource = getDataSourceResolver().resolve(resourceLocation); cid = embed(dataSource, name); cidCache.put(name, cid);
/** * Test sending a image HTML mail based on a real world website. We * would expect to see the ApacheCon logo at the bottom of the email. * Please note that not all major email clients can display the email * properly. * * @throws Exception the test failed */ @Test public void testImageHtmlEmailRemote() throws Exception { if(EmailConfiguration.MAIL_FORCE_SEND) { final URL url = new URL("http://commons.apache.org/email/"); // URL url = new URL("http://www.dzone.com/links/index.html"); final String htmlMsg = getFromUrl(url); final ImageHtmlEmail email = (ImageHtmlEmail) create(ImageHtmlEmail.class); email.setDataSourceResolver(new DataSourceUrlResolver(url, true)); email.setSubject("[testImageHtmlEmail] 2.Test: complex html content"); email.setHtmlMsg(htmlMsg); EmailUtils.writeMimeMessage( new File("./target/test-emails/testImageHtmlEmailRemote.eml"), send(email).getMimeMessage()); } }
@Test public void testSendEmptyHTML2() throws Exception { Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST); // Create the email message final ImageHtmlEmail email = new ImageHtmlEmail(); // set the html message try { email.setHtmlMsg(""); fail("Should fail here!"); } catch (final EmailException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid message supplied")); } }
dataSource = getDataSourceResolver().resolve(resourceLocation); cid = embed(dataSource, name); cidCache.put(name, cid);
import org.apache.commons.mail.HtmlEmail; ... // load your HTML email template String htmlEmailTemplate = .... // define you base URL to resolve relative resource locations URL url = new URL("http://www.apache.org"); // create the email message HtmlEmail email = new ImageHtmlEmail(); email.setDataSourceResolver(new DataSourceResolverImpl(url)); 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"); // set the html message email.setHtmlMsg(htmlEmailTemplate); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send();
/** * Does the work of actually building the MimeMessage. * * @see org.apache.commons.mail.HtmlEmail#buildMimeMessage() * @throws EmailException building the MimeMessage failed */ @Override public void buildMimeMessage() throws EmailException { try { // embed all the matching image and script resources within the email String temp = replacePattern(super.html, IMG_PATTERN); temp = replacePattern(temp, SCRIPT_PATTERN); setHtmlMsg(temp); super.buildMimeMessage(); } catch (final IOException e) { throw new EmailException("Building the MimeMessage failed", e); } }
@JsonIgnore public ImageHtmlEmail getNewImageHtmlEmail(final Map<String, Object> params) throws EmailException { ImageHtmlEmail email = new ImageHtmlEmail(); generic_params(email, params); return email; }
@Override public ImageHtmlEmail get() { return factory.newEmail(new ImageHtmlEmail()); }
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);
HtmlEmail email = new ImageHtmlEmail();