Search in sources :

Example 1 with DataSourceUrlResolver

use of org.apache.commons.mail.resolver.DataSourceUrlResolver in project SpringStepByStep by JavaProgrammerLB.

the class JavaMailDemo method test.

@Test
public void test() {
    try {
        // load your HTML email template
        String htmlEmailTemplate = "....hello how are you <img src=\"http://www.apache.org/images/feather.gif\"> ....";
        // define you base URL to resolve relative resource locations
        URL url = new URL("http://www.apache.org");
        // create the email message
        ImageHtmlEmail email = new ImageHtmlEmail();
        email.setDataSourceResolver(new DataSourceUrlResolver(url));
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setSSLOnConnect(true);
        email.setFrom("username", "LIUBEI");
        email.addTo("admin@yitianyigexiangfa.com", "ADMIN");
        email.setAuthentication("username", "password");
        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();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (EmailException e) {
        e.printStackTrace();
    }
}
Also used : DataSourceUrlResolver(org.apache.commons.mail.resolver.DataSourceUrlResolver) MalformedURLException(java.net.MalformedURLException) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) EmailException(org.apache.commons.mail.EmailException) URL(java.net.URL) Test(org.junit.Test)

Aggregations

MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 EmailException (org.apache.commons.mail.EmailException)1 ImageHtmlEmail (org.apache.commons.mail.ImageHtmlEmail)1 DataSourceUrlResolver (org.apache.commons.mail.resolver.DataSourceUrlResolver)1 Test (org.junit.Test)1