org.apache.commons.mail.resolver.DataSourceUrlResolver Java Examples

The following examples show how to use org.apache.commons.mail.resolver.DataSourceUrlResolver. 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: EmailLiveTest.java    From commons-email with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example #2
Source File: EmailLiveTest.java    From commons-email with Apache License 2.0 6 votes vote down vote up
/**
 * 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("https://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());
    }
}
 
Example #3
Source File: EmailManager.java    From Notebook with Apache License 2.0 5 votes vote down vote up
private ImageHtmlEmail createImageEmail(EmailFrom emailFrom, String _url, String html) throws Exception {
       ImageHtmlEmail email = new ImageHtmlEmail();
       email.setCharset("UTF-8");
	URL url = new URL(_url);
	email.setDataSourceResolver(new DataSourceUrlResolver(url));
	email.setHostName(emailFrom.getHostName());
    email.setSmtpPort(emailFrom.getSmtpPort());
    email.setAuthenticator(new DefaultAuthenticator(emailFrom.getUser(), emailFrom.getPwd()));
    email.setSSLOnConnect(true);
       email.setFrom(emailFrom.getFromAddr(), emailFrom.getFromNick());
       email.setSubject(emailFrom.getSubject());
       email.setTextMsg(emailFrom.getText());
       email.setHtmlMsg(html);
       return email;
}
 
Example #4
Source File: ImageHtmlEmailTest.java    From commons-email with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendHtml() throws Exception {

    Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST);

    // Create the email message
    getMailServer();

    final String strSubject = "Test HTML Send default";

    email = new MockImageHtmlEmailConcrete();
    email.setDataSourceResolver(new DataSourceUrlResolver(TEST_IMAGE_DIR.toURI().toURL(), TEST_IS_LENIENT));
    email.setHostName(strTestMailServer);
    email.setSmtpPort(getMailServerPort());
    email.setFrom(strTestMailFrom);
    email.addTo(strTestMailTo);
    email.setSubject(strSubject);

    final String html = loadUrlContent(TEST_HTML_URL);

    // set the html message
    email.setHtmlMsg(html);

    // set the alternative message
    email.setTextMsg("Your email client does not support HTML messages");

    // send the email
    email.send();

    fakeMailServer.stop();

    assertEquals(1, fakeMailServer.getMessages().size());
    final MimeMessage mimeMessage = fakeMailServer.getMessages().get(0).getMimeMessage();

    final MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();
    assertTrue(mimeMessageParser.getHtmlContent().contains("\"cid:"));
    assertTrue(mimeMessageParser.getAttachmentList().size() == 3);
}
 
Example #5
Source File: ImageHtmlEmailTest.java    From commons-email with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendHtmlUrl() throws Exception {
    Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST);

    getMailServer();

    final String strSubject = "Test HTML Send default with URL";

    // Create the email message
    email = new MockImageHtmlEmailConcrete();
    email.setHostName(strTestMailServer);
    email.setSmtpPort(getMailServerPort());
    email.setFrom(strTestMailFrom);
    email.addTo(strTestMailTo);
    email.setSubject(strSubject);
    email.setDataSourceResolver(new DataSourceUrlResolver(TEST_IMAGE_DIR.toURI().toURL(), TEST_IS_LENIENT));

    // set the html message
    email.setHtmlMsg(
            "<html><body><img src=\"http://www.apache.org/images/feather.gif\"/></body></html>"
    );

    // send the email
    email.send();

    fakeMailServer.stop();
    // validate txt message
    validateSend(fakeMailServer, strSubject, email.getHtmlMsg(),
            email.getFromAddress(), email.getToAddresses(),
            email.getCcAddresses(), email.getBccAddresses(), true);
}
 
Example #6
Source File: ImageHtmlEmailTest.java    From commons-email with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendHTMLAbsoluteLocalFile() throws Exception {
    Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST);

    // Create the email message
    getMailServer();

    final String strSubject = "Test HTML Send default with absolute local path";

    // Create the email message
    email = new MockImageHtmlEmailConcrete();
    email.setHostName(strTestMailServer);
    email.setSmtpPort(getMailServerPort());
    email.setFrom(strTestMailFrom);
    email.addTo(strTestMailTo);
    email.setSubject(strSubject);
    email.setDataSourceResolver(new DataSourceUrlResolver(TEST_IMAGE_DIR.toURI().toURL(), TEST_IS_LENIENT));

    final File file = File.createTempFile("emailtest", ".tst");
    FileUtils.writeStringToFile(file,
            "just some silly data that we won't be able to display anyway");

    // set the html message
    email.setHtmlMsg("<html><body><img src=\"" + file.getAbsolutePath()
            + "\"/></body></html>"
    );

    // send the email
    email.send();

    fakeMailServer.stop();
    // validate txt message
    validateSend(fakeMailServer, strSubject, email.getHtmlMsg(),
            email.getFromAddress(), email.getToAddresses(),
            email.getCcAddresses(), email.getBccAddresses(), true);
}
 
Example #7
Source File: ImageHtmlEmailTest.java    From commons-email with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmail127() throws Exception {
    Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST);

    getMailServer();

    final String strSubject = "Test HTML Send default with URL";

    // Create the email message
    email = new MockImageHtmlEmailConcrete();
    email.setHostName(strTestMailServer);
    email.setSmtpPort(getMailServerPort());
    email.setFrom(strTestMailFrom);
    email.addTo(strTestMailTo);
    email.setSubject(strSubject);
    email.setDataSourceResolver(new DataSourceUrlResolver(TEST_IMAGE_DIR.toURI().toURL(), TEST_IS_LENIENT));

    // set the html message
    email.setHtmlMsg("<html><body><img title=\"$\" src=\"http://www.apache.org/images/feather.gif\"/></body></html>");

    // send the email
    email.send();

    fakeMailServer.stop();
    // validate txt message
    validateSend(fakeMailServer, strSubject, email.getHtmlMsg(),
                 email.getFromAddress(), email.getToAddresses(),
                 email.getCcAddresses(), email.getBccAddresses(), true);
}
 
Example #8
Source File: ImageHtmlEmailTest.java    From commons-email with Apache License 2.0 4 votes vote down vote up
@Test
public void testSendHTMLAutoResolveFile() throws Exception {
    Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST);

    // Create the email message
    getMailServer();

    final String strSubject = "Test HTML Send default";

    email = new MockImageHtmlEmailConcrete();
    final DataSourceResolver dataSourceResolvers[] = new DataSourceResolver[2];
    dataSourceResolvers[0] = new DataSourceUrlResolver(new URL("http://foo"), true);
    dataSourceResolvers[1] = new DataSourceClassPathResolver("/", true);

    email.setDataSourceResolver(new DataSourceCompositeResolver(dataSourceResolvers));
    email.setHostName(strTestMailServer);
    email.setSmtpPort(getMailServerPort());
    email.setFrom(strTestMailFrom);
    email.addTo(strTestMailTo);
    email.setSubject(strSubject);

    final String html = loadUrlContent(TEST2_HTML_URL);

    // set the html message
    email.setHtmlMsg(html);

    // set the alternative message
    email.setTextMsg("Your email client does not support HTML messages");

    // send the email
    email.send();

    fakeMailServer.stop();

    assertEquals(1, fakeMailServer.getMessages().size());
    final MimeMessage mimeMessage = fakeMailServer.getMessages().get(0).getMimeMessage();
    MimeMessageUtils.writeMimeMessage(mimeMessage, new File("./target/test-emails/testSendHTMLAutoFile.eml"));

    final MimeMessageParser mimeMessageParser = new MimeMessageParser(mimeMessage).parse();
    assertTrue(mimeMessageParser.getHtmlContent().contains("\"cid:"));
    assertTrue(mimeMessageParser.getAttachmentList().size() == 1);
}