Search in sources :

Example 11 with EmailException

use of org.apache.commons.mail.EmailException in project c4sg-services by Code4SocialGood.

the class AsyncEmailServiceImpl method send.

/**
     * Sends an email message asynchronously.
     *
     * @param from       email address from which the message will be sent.
     * @param recipient  array of strings containing the recipients of the message.
     * @param subject    subject header field.
     * @param text       content of the message.
     */
@Async
public void send(String from, String recipient, String subject, String text) {
    try {
        Properties properties = new Properties();
        properties.load(getClass().getResourceAsStream("/mail.properties"));
        String host = properties.getProperty("mail.smtp.host");
        String port = properties.getProperty("mail.smtp.port");
        String ssl = properties.getProperty("mail.smtp.ssl.enable");
        String username = properties.getProperty("mail.smtp.username");
        String password = properties.getProperty("mail.smtp.password");
        HtmlEmail email = new HtmlEmail();
        email.setHostName(host);
        email.setSmtpPort(Integer.parseInt(port));
        email.setAuthentication(username, password);
        email.setSSLOnConnect(Boolean.parseBoolean(ssl));
        Objects.requireNonNull(from, "Sender's email is incorrect");
        Objects.requireNonNull(recipient, "Recipient's email is incorrect");
        email.setFrom(from);
        email.addTo(recipient);
        email.setSubject(subject);
        email.setHtmlMsg(text);
        email.send();
        System.out.println("Email sent to " + recipient + " successfully");
    } catch (EmailException e) {
        e.printStackTrace();
        System.out.println("Fail: Could not send email to: " + recipient + " !!! ");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException) IOException(java.io.IOException) Properties(java.util.Properties) Async(org.springframework.scheduling.annotation.Async)

Example 12 with EmailException

use of org.apache.commons.mail.EmailException in project Activiti by Activiti.

the class MailActivityBehavior method execute.

@Override
public void execute(ActivityExecution execution) {
    boolean doIgnoreException = Boolean.parseBoolean(getStringFromField(ignoreException, execution));
    String exceptionVariable = getStringFromField(exceptionVariableName, execution);
    Email email = null;
    try {
        String toStr = getStringFromField(to, execution);
        String fromStr = getStringFromField(from, execution);
        String ccStr = getStringFromField(cc, execution);
        String bccStr = getStringFromField(bcc, execution);
        String subjectStr = getStringFromField(subject, execution);
        String textStr = textVar == null ? getStringFromField(text, execution) : getStringFromField(getExpression(execution, textVar), execution);
        String htmlStr = htmlVar == null ? getStringFromField(html, execution) : getStringFromField(getExpression(execution, htmlVar), execution);
        String charSetStr = getStringFromField(charset, execution);
        List<File> files = new LinkedList<File>();
        List<DataSource> dataSources = new LinkedList<DataSource>();
        getFilesFromFields(attachments, execution, files, dataSources);
        email = createEmail(textStr, htmlStr, attachmentsExist(files, dataSources));
        addTo(email, toStr);
        setFrom(email, fromStr, execution.getTenantId());
        addCc(email, ccStr);
        addBcc(email, bccStr);
        setSubject(email, subjectStr);
        setMailServerProperties(email, execution.getTenantId());
        setCharset(email, charSetStr);
        attach(email, files, dataSources);
        email.send();
    } catch (ActivitiException e) {
        handleException(execution, e.getMessage(), e, doIgnoreException, exceptionVariable);
    } catch (EmailException e) {
        handleException(execution, "Could not send e-mail in execution " + execution.getId(), e, doIgnoreException, exceptionVariable);
    }
    leave(execution);
}
Also used : ActivitiException(org.activiti.engine.ActivitiException) Email(org.apache.commons.mail.Email) HtmlEmail(org.apache.commons.mail.HtmlEmail) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) EmailException(org.apache.commons.mail.EmailException) File(java.io.File) LinkedList(java.util.LinkedList) DataSource(javax.activation.DataSource)

Example 13 with EmailException

use of org.apache.commons.mail.EmailException in project Activiti by Activiti.

the class MailActivityBehavior method setFrom.

protected void setFrom(Email email, String from, String tenantId) {
    String fromAddress = null;
    if (from != null) {
        fromAddress = from;
    } else {
        // use default configured from address in process engine config
        if (tenantId != null && tenantId.length() > 0) {
            Map<String, MailServerInfo> mailServers = Context.getProcessEngineConfiguration().getMailServers();
            if (mailServers != null && mailServers.containsKey(tenantId)) {
                MailServerInfo mailServerInfo = mailServers.get(tenantId);
                fromAddress = mailServerInfo.getMailServerDefaultFrom();
            }
        }
        if (fromAddress == null) {
            fromAddress = Context.getProcessEngineConfiguration().getMailServerDefaultFrom();
        }
    }
    try {
        email.setFrom(fromAddress);
    } catch (EmailException e) {
        throw new ActivitiException("Could not set " + from + " as from address in email", e);
    }
}
Also used : MailServerInfo(org.activiti.engine.cfg.MailServerInfo) ActivitiException(org.activiti.engine.ActivitiException) EmailException(org.apache.commons.mail.EmailException)

Example 14 with EmailException

use of org.apache.commons.mail.EmailException 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 15 with EmailException

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

the class JavaMailDemo method sendEmailWithAttachment.

//	@Test
public void sendEmailWithAttachment() {
    try {
        // Create the attachment
        EmailAttachment attachment = new EmailAttachment();
        System.out.println(System.getProperty("user.dir"));
        attachment.setPath("src/main/resources/john.jpg");
        // 通过url添加附件			  attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription("Picture of John");
        attachment.setName("John.jpg");
        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        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("The picture");
        email.setMsg("Here is the picture you wanted");
        // add the attachment
        email.attach(attachment);
        // send the email
        email.send();
        System.out.println("finished!!!");
    } catch (EmailException e) {
        e.printStackTrace();
    }
}
Also used : EmailAttachment(org.apache.commons.mail.EmailAttachment) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) EmailException(org.apache.commons.mail.EmailException)

Aggregations

EmailException (org.apache.commons.mail.EmailException)29 HtmlEmail (org.apache.commons.mail.HtmlEmail)18 Email (org.apache.commons.mail.Email)7 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)7 EmailAttachment (org.apache.commons.mail.EmailAttachment)5 ExecutionException (java.util.concurrent.ExecutionException)4 SimpleEmail (org.apache.commons.mail.SimpleEmail)4 RenderResult (cn.bran.japid.template.RenderResult)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 InternetAddress (javax.mail.internet.InternetAddress)3 ImageHtmlEmail (org.apache.commons.mail.ImageHtmlEmail)3 VelocityContext (org.apache.velocity.VelocityContext)3 MailException (cn.bran.play.exceptions.MailException)2 Status (com.ctrip.platform.dal.daogen.domain.Status)2