Search in sources :

Example 1 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project openhab1-addons by openhab.

the class Mail method sendMail.

/**
     * Sends an email with attachment(s) via SMTP
     *
     * @param to the email address of the recipient
     * @param subject the subject of the email
     * @param message the body of the email
     * @param attachmentUrlList a list of URL strings of the contents to send as attachments
     *
     * @return <code>true</code>, if sending the email has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends an email with attachment via SMTP")
public static boolean sendMail(@ParamDoc(name = "to") String to, @ParamDoc(name = "subject") String subject, @ParamDoc(name = "message") String message, @ParamDoc(name = "attachmentUrlList") List<String> attachmentUrlList) {
    boolean success = false;
    if (MailActionService.isProperlyConfigured) {
        Email email = new SimpleEmail();
        if (attachmentUrlList != null && !attachmentUrlList.isEmpty()) {
            email = new MultiPartEmail();
            for (String attachmentUrl : attachmentUrlList) {
                // Create the attachment
                try {
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setURL(new URL(attachmentUrl));
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    String fileName = attachmentUrl.replaceFirst(".*/([^/?]+).*", "$1");
                    attachment.setName(isNotBlank(fileName) ? fileName : "Attachment");
                    ((MultiPartEmail) email).attach(attachment);
                } catch (MalformedURLException e) {
                    logger.error("Invalid attachment url.", e);
                } catch (EmailException e) {
                    logger.error("Error adding attachment to email.", e);
                }
            }
        }
        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setStartTLSEnabled(startTLSEnabled);
        email.setSSLOnConnect(sslOnConnect);
        if (isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }
        try {
            if (isNotBlank(charset)) {
                email.setCharset(charset);
            }
            email.setFrom(from);
            String[] toList = to.split(";");
            for (String toAddress : toList) {
                email.addTo(toAddress);
            }
            if (!isEmpty(subject)) {
                email.setSubject(subject);
            }
            if (!isEmpty(message)) {
                email.setMsg(message);
            }
            email.send();
            logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
            success = true;
        } catch (EmailException e) {
            logger.error("Could not send e-mail to '" + to + "'.", e);
        }
    } else {
        logger.error("Cannot send e-mail because of missing configuration settings. The current settings are: " + "Host: '{}', port '{}', from '{}', startTLSEnabled: {}, sslOnConnect: {}, username: '{}', password '{}'", new Object[] { hostname, String.valueOf(port), from, String.valueOf(startTLSEnabled), String.valueOf(sslOnConnect), username, password });
    }
    return success;
}
Also used : MalformedURLException(java.net.MalformedURLException) Email(org.apache.commons.mail.Email) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) EmailAttachment(org.apache.commons.mail.EmailAttachment) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) SimpleEmail(org.apache.commons.mail.SimpleEmail) URL(java.net.URL) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 2 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project weicoder by wdcode.

the class EmailApache method sendMultiPartEmail.

/**
 * 发送带附件的邮件
 * @param to 发送地址
 * @param subject 邮件标题
 * @param msg 邮件内容
 * @param attach 附件
 */
protected final void sendMultiPartEmail(String[] to, String subject, String msg, String attach) {
    // 实例化邮件操作类
    MultiPartEmail email = new MultiPartEmail();
    // 添加附件
    setAttachment(email, attach);
    // 发送Email
    sendEmail(email, to, subject, msg);
}
Also used : MultiPartEmail(org.apache.commons.mail.MultiPartEmail)

Example 3 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project dwoss by gg-net.

the class DocumentSupporterOperation method mail.

/**
 * This method send document to the e-Mail address that is in the customer set.
 * <p/>
 * @param document This is the Document that will be send.
 * @throws UserInfoException if the sending of the Mail is not successful.
 * @throws RuntimeException  if problems exist in the JasperExporter
 */
@Override
public void mail(Document document, DocumentViewType jtype) throws UserInfoException, RuntimeException {
    UiCustomer customer = customerService.asUiCustomer(document.getDossier().getCustomerId());
    String customerMailAddress = customerService.asCustomerMetaData(document.getDossier().getCustomerId()).getEmail();
    if (customerMailAddress == null) {
        throw new UserInfoException("Kunde hat keine E-Mail Hinterlegt! Senden einer E-Mail ist nicht Möglich!");
    }
    String doctype = (jtype == DocumentViewType.DEFAULT ? document.getType().getName() : jtype.getName());
    try (InputStream is = mandator.getMailTemplateLocation().toURL().openStream();
        InputStreamReader templateReader = new InputStreamReader(is)) {
        String text = new MailDocumentParameter(customer.toTitleNameLine(), doctype).eval(IOUtils.toString(templateReader));
        MultiPartEmail email = mandator.prepareDirectMail();
        email.setCharset("UTF-8");
        email.addTo(customerMailAddress);
        email.setSubject(document.getType().getName() + " | " + document.getDossier().getIdentifier());
        email.setMsg(text + mandator.getDefaultMailSignature());
        email.attach(new ByteArrayDataSource(JasperExportManager.exportReportToPdf(jasper(document, jtype)), "application/pdf"), "Dokument.pdf", "Das ist das Dokument zu Ihrem Aufrag als PDF.");
        for (MandatorMailAttachment mma : mandator.getDefaultMailAttachment()) {
            email.attach(mma.getAttachmentData().toURL(), mma.getAttachmentName(), mma.getAttachmentDescription());
        }
        email.send();
    } catch (EmailException ex) {
        L.error("Error on Mail sending", ex);
        throw new UserInfoException("Das senden der Mail war nicht erfolgreich!\n" + ex.getMessage());
    } catch (IOException | JRException e) {
        throw new RuntimeException(e);
    }
}
Also used : MailDocumentParameter(eu.ggnet.dwoss.mandator.api.value.partial.MailDocumentParameter) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) MandatorMailAttachment(eu.ggnet.dwoss.mandator.api.value.partial.MandatorMailAttachment) EmailException(org.apache.commons.mail.EmailException) UiCustomer(eu.ggnet.dwoss.customer.opi.UiCustomer) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

Example 4 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project dwoss by gg-net.

the class SalesListingProducerOperation method prepareAndSend.

/**
 * Prepare and send filejackets to the specified email address.
 * <p>
 * @param fileJackets files to be send
 */
private void prepareAndSend(List<FileJacket> fileJackets) {
    SubMonitor m = monitorFactory.newSubMonitor("Transfer");
    m.message("sending Mail");
    m.start();
    try {
        ListingMailConfiguration config = listingService.get().listingMailConfiguration();
        MultiPartEmail email = mandator.prepareDirectMail();
        email.setFrom(config.getFromAddress());
        email.addTo(config.getToAddress());
        email.setSubject(config.getSubject());
        email.setMsg(config.toMessage());
        for (FileJacket fj : fileJackets) {
            email.attach(new javax.mail.util.ByteArrayDataSource(fj.getContent(), "application/xls"), fj.getHead() + fj.getSuffix(), "Die Händlerliste für die Marke ");
        }
        email.send();
        m.finish();
    } catch (EmailException e) {
        throw new RuntimeException(e);
    }
}
Also used : ListingMailConfiguration(eu.ggnet.dwoss.mandator.api.value.partial.ListingMailConfiguration) java.util(java.util) eu.ggnet.dwoss.util(eu.ggnet.dwoss.util) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) SubMonitor(eu.ggnet.dwoss.progress.SubMonitor) EmailException(org.apache.commons.mail.EmailException)

Example 5 with MultiPartEmail

use of org.apache.commons.mail.MultiPartEmail in project ninja by ninjaframework.

the class CommonsMailHelperImplTest method testDoSetServerParameterStartTLSEnabled.

@Test
public void testDoSetServerParameterStartTLSEnabled() throws Exception {
    Mail mail = MailImplTestHelper.getMailImplWithDemoContent();
    MultiPartEmail multiPartEmail = commonsmailHelper.createMultiPartEmailWithContent(mail);
    commonsmailHelper.doSetServerParameter(multiPartEmail, "mail.superserver.com", 33, true, true, true, "username", "password", true);
    assertEquals(true, multiPartEmail.isStartTLSEnabled());
    assertEquals(true, multiPartEmail.isStartTLSRequired());
}
Also used : Mail(ninja.postoffice.Mail) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) Test(org.junit.Test)

Aggregations

MultiPartEmail (org.apache.commons.mail.MultiPartEmail)22 EmailException (org.apache.commons.mail.EmailException)11 EmailAttachment (org.apache.commons.mail.EmailAttachment)9 HtmlEmail (org.apache.commons.mail.HtmlEmail)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 InternetAddress (javax.mail.internet.InternetAddress)4 Email (org.apache.commons.mail.Email)4 RenderResult (cn.bran.japid.template.RenderResult)3 IOException (java.io.IOException)3 URL (java.net.URL)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ExecutionException (java.util.concurrent.ExecutionException)3 ByteArrayDataSource (javax.mail.util.ByteArrayDataSource)3 Mail (ninja.postoffice.Mail)3 Test (org.junit.Test)3 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MailException (cn.bran.play.exceptions.MailException)1