Search in sources :

Example 1 with ImageHtmlEmail

use of org.apache.commons.mail.ImageHtmlEmail in project Java-Tutorial by gpcodervn.

the class SendHtmlEmail method main.

public static void main(String[] args) throws EmailException, MalformedURLException {
    // Tạo đối tượng Email
    ImageHtmlEmail email = new ImageHtmlEmail();
    // Cấu hình thông tin Email Server
    email.setHostName(MailConfig.MAILCATCHER_HOST_NAME);
    email.setSmtpPort(MailConfig.MAILCATCHER_SSL_PORT);
    // Người gửi
    email.setFrom(MailConfig.APP_EMAIL);
    // Người nhận
    email.addTo(MailConfig.RECEIVE_EMAIL);
    // Tiêu đề
    email.setSubject("Testing Subject");
    // Định nghĩa URL cơ sở để xác định đúng vị trí nguồn dữ liệu (img,..)
    // (Trong trường hợp nó có đường dẫn tương đối, ví dụ thẻ img như bên dưới)
    URL url = new URL("https://gpcoder.com");
    email.setDataSourceResolver(new DataSourceUrlResolver(url));
    // Nội dung email
    String htmlContent = "<h1>Welcome to <a href=\"gpcoder.com\">GP Coder</a></h1>" + "<img src=\"wp-content/uploads/2017/10/Facebook_Icon_GP_2-300x180.png\" " + "	width=\"300\" " + "	height=\"180\" " + "	border=\"0\" " + "	alt=\"gpcoder.com\" />";
    email.setHtmlMsg(htmlContent);
    // Nội dung thay thế:
    // Trong trường hợp chương trình đọc email của người nhận ko hỗ trợ HTML
    email.setTextMsg("Your email client does not support HTML messages");
    // send message
    email.send();
    System.out.println("Message sent successfully");
}
Also used : DataSourceUrlResolver(org.apache.commons.mail.resolver.DataSourceUrlResolver) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) URL(java.net.URL)

Example 2 with ImageHtmlEmail

use of org.apache.commons.mail.ImageHtmlEmail 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)

Example 3 with ImageHtmlEmail

use of org.apache.commons.mail.ImageHtmlEmail in project estatio by estatio.

the class EmailServiceThrowingException method send.

// endregion
// region > send
@Programmatic
public boolean send(final List<String> toList, final List<String> ccList, final List<String> bccList, final String subject, final String body, final DataSource... attachments) {
    try {
        final ImageHtmlEmail email = new ImageHtmlEmail();
        email.setAuthenticator(new DefaultAuthenticator(senderEmailAddress, senderEmailPassword));
        email.setHostName(getSenderEmailHostName());
        email.setSmtpPort(senderEmailPort);
        email.setStartTLSEnabled(getSenderEmailTlsEnabled());
        email.setDataSourceResolver(new DataSourceClassPathResolver("/", true));
        // 
        // change from default impl.
        // 
        email.setSocketTimeout(2000);
        email.setSocketConnectionTimeout(2000);
        final Properties properties = email.getMailSession().getProperties();
        // TODO ISIS-987: check whether all these are required and extract as configuration settings
        properties.put("mail.smtps.auth", "true");
        properties.put("mail.debug", "true");
        properties.put("mail.smtps.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.port", "" + senderEmailPort);
        properties.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtps.socketFactory.fallback", "false");
        email.setFrom(senderEmailAddress);
        email.setSubject(subject);
        email.setHtmlMsg(body);
        email.setCharset("windows-1252");
        if (attachments != null && attachments.length > 0) {
            for (DataSource attachment : attachments) {
                email.attach(attachment, attachment.getName(), "");
            }
        }
        if (notEmpty(toList)) {
            email.addTo(toList.toArray(new String[toList.size()]));
        }
        if (notEmpty(ccList)) {
            email.addCc(ccList.toArray(new String[ccList.size()]));
        }
        if (notEmpty(bccList)) {
            email.addBcc(bccList.toArray(new String[bccList.size()]));
        }
        email.send();
    } catch (EmailException ex) {
        // 
        // change from default impl.
        // 
        LOG.error("An error occurred while trying to send an email about user email verification", ex);
        throw new RuntimeException(ex);
    }
    return true;
}
Also used : DataSourceClassPathResolver(org.apache.commons.mail.resolver.DataSourceClassPathResolver) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) Properties(java.util.Properties) DataSource(javax.activation.DataSource) Programmatic(org.apache.isis.applib.annotation.Programmatic)

Example 4 with ImageHtmlEmail

use of org.apache.commons.mail.ImageHtmlEmail in project Java-Tutorial by gpcodervn.

the class SendHtmlEmail method main.

public static void main(String[] args) throws EmailException, MalformedURLException {
    // Tạo đối tượng Email
    ImageHtmlEmail email = new ImageHtmlEmail();
    // Cấu hình thông tin Email Server
    email.setHostName(MailConfig.HOST_NAME);
    email.setSmtpPort(MailConfig.SSL_PORT);
    email.setAuthenticator(new DefaultAuthenticator(MailConfig.APP_EMAIL, MailConfig.APP_PASSWORD));
    email.setSSLOnConnect(true);
    // Người gửi
    email.setFrom(MailConfig.APP_EMAIL);
    // Người nhận
    email.addTo(MailConfig.RECEIVE_EMAIL);
    // Tiêu đề
    email.setSubject("Testing Subject");
    // Định nghĩa URL cơ sở để xác định đúng vị trí nguồn dữ liệu (img,..)
    // (Trong trường hợp nó có đường dẫn tương đối, ví dụ thẻ img như bên dưới)
    URL url = new URL("https://gpcoder.com");
    email.setDataSourceResolver(new DataSourceUrlResolver(url));
    // Nội dung email
    String htmlContent = "<h1>Welcome to <a href=\"gpcoder.com\">GP Coder</a></h1>" + "<img src=\"wp-content/uploads/2017/10/Facebook_Icon_GP_2-300x180.png\" " + "	width=\"300\" " + "	height=\"180\" " + "	border=\"0\" " + "	alt=\"gpcoder.com\" />";
    email.setHtmlMsg(htmlContent);
    // Nội dung thay thế:
    // Trong trường hợp chương trình đọc email của người nhận ko hỗ trợ HTML
    email.setTextMsg("Your email client does not support HTML messages");
    // send message
    email.send();
    System.out.println("Message sent successfully");
}
Also used : DataSourceUrlResolver(org.apache.commons.mail.resolver.DataSourceUrlResolver) ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) URL(java.net.URL)

Example 5 with ImageHtmlEmail

use of org.apache.commons.mail.ImageHtmlEmail in project VaadinUtils by rlsutton1.

the class JasperEmailBuilder method send.

public void send(boolean debug) throws EmailException, URISyntaxException {
    Preconditions.checkNotNull(fromAddress);
    Preconditions.checkNotNull(tos.size() > 0);
    Preconditions.checkNotNull(subject);
    Preconditions.checkNotNull(this.htmlBody != null || this.renderedReportBody != null, "You must specify a body.");
    ImageHtmlEmail email = new ImageHtmlEmail();
    if (this.renderedReportBody != null)
        email.setDataSourceResolver(new JasperDataSourceResolver(renderedReportBody));
    email.setDebug(debug);
    email.setHostName(settings.getSmtpFQDN());
    email.setSmtpPort(settings.getSmtpPort());
    if (settings.isAuthRequired())
        email.setAuthentication(settings.getUsername(), settings.getPassword());
    if (settings.getUseSSL()) {
        email.setSslSmtpPort(settings.getSmtpPort().toString());
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    }
    email.setFrom(fromAddress);
    email.setBounceAddress(settings.getBounceEmailAddress());
    email.setSubject(subject);
    for (String to : this.tos) email.addTo(to);
    for (String cc : this.ccs) email.addCc(cc);
    for (String bcc : this.bccs) email.addBcc(bcc);
    if (StringUtils.isNotEmpty(this.htmlBody))
        email.setHtmlMsg(this.htmlBody);
    else
        email.setHtmlMsg("The body of this email was left blank");
    if (this.renderedReportBody != null)
        email.setHtmlMsg(this.renderedReportBody.getBodyAsHtml());
    email.setTextMsg(this.textBody);
    for (DataSource attachment : this.attachments) email.attach(attachment, attachment.getName(), attachment.getName());
    email.send();
}
Also used : ImageHtmlEmail(org.apache.commons.mail.ImageHtmlEmail) FileDataSource(javax.activation.FileDataSource) DataSource(javax.activation.DataSource)

Aggregations

ImageHtmlEmail (org.apache.commons.mail.ImageHtmlEmail)5 URL (java.net.URL)3 DataSourceUrlResolver (org.apache.commons.mail.resolver.DataSourceUrlResolver)3 DataSource (javax.activation.DataSource)2 DefaultAuthenticator (org.apache.commons.mail.DefaultAuthenticator)2 EmailException (org.apache.commons.mail.EmailException)2 MalformedURLException (java.net.MalformedURLException)1 Properties (java.util.Properties)1 FileDataSource (javax.activation.FileDataSource)1 DataSourceClassPathResolver (org.apache.commons.mail.resolver.DataSourceClassPathResolver)1 Programmatic (org.apache.isis.applib.annotation.Programmatic)1 Test (org.junit.Test)1