/** * 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 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()); }
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(); } }
email.setSmtpPort(senderEmailPort); email.setStartTLSEnabled(senderEmailTlsEnabled); email.setDataSourceResolver(new DataSourceClassPathResolver("/", true));
htmlEmail.setDataSourceResolver(new DataSourceClassPathResolver());