Example usage for org.apache.commons.mail EmailAttachment setURL

List of usage examples for org.apache.commons.mail EmailAttachment setURL

Introduction

In this page you can find the example usage for org.apache.commons.mail EmailAttachment setURL.

Prototype

public void setURL(final URL aUrl) 

Source Link

Document

Set the URL.

Usage

From source file:com.mycollab.module.mail.UrlAttachmentSource.java

@Override
public EmailAttachment getAttachmentObj() {
    EmailAttachment attachment = new EmailAttachment();
    attachment.setURL(url);
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(name);//from w ww .  j a v a2  s  . c  o  m
    return attachment;
}

From source file:binky.reportrunner.engine.utils.impl.EmailHandlerImpl.java

public void sendEmail(String destinationEmail, String fromEmail, String smtpServer, String fileUrl,
        String jobName, String groupName) throws EmailException, IOException {
    logger.debug("sending email to: " + destinationEmail + " on host " + smtpServer);
    FileSystemHandler fs = new FileSystemHandlerImpl();
    MultiPartEmail email = new MultiPartEmail();
    email.setDebug(debug);// w ww .  j  a v  a2s  . c o m
    email.setHostName(smtpServer);
    for (String toEmail : destinationEmail.split(",")) {
        email.addTo(toEmail.trim());
    }
    email.setFrom(fromEmail);
    email.setSubject(subject + jobName + " [" + groupName + "]");
    email.setMsg(message);
    List<String> tempFiles = new LinkedList<String>();

    EmailAttachment attachment = new EmailAttachment();

    String temp = copyToTemp(fileUrl);
    tempFiles.add(temp);
    attachment.setURL(fs.getURL(temp));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Report Runner Report");

    // get the file name of the report from the complete path
    String name = fs.getFileName(fileUrl);
    attachment.setName(name);
    logger.debug("File: " + fileUrl);
    logger.debug("Name: " + name);

    email.attach(attachment);

    email.send();

    fs.deleteFile(temp);

}

From source file:binky.reportrunner.engine.utils.impl.EmailHandlerImpl.java

public void sendEmail(String destinationEmail, String fromEmail, String smtpServer, List<String> fileUrls,
        String jobName, String groupName) throws EmailException, IOException {
    // logger.debug("sending email to: " + to + " on host " + server);
    FileSystemHandler fs = new FileSystemHandlerImpl();
    MultiPartEmail email = new MultiPartEmail();
    email.setDebug(debug);//  w w w . j  ava  2 s.  co m
    email.setHostName(smtpServer);
    for (String toEmail : destinationEmail.split(",")) {
        email.addTo(toEmail.trim());
    }
    email.setFrom(fromEmail);
    email.setSubject(subject + jobName + " [" + groupName + "]");
    email.setMsg("Please find attached outputs for the above job.");
    List<String> tempFiles = new LinkedList<String>();
    for (String url : fileUrls) {

        EmailAttachment attachment = new EmailAttachment();

        String temp = copyToTemp(url);
        tempFiles.add(temp);
        attachment.setURL(fs.getURL(temp));
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription(message);

        // get the file name of the report from the complete path
        String name = fs.getFileName(url);
        attachment.setName(name);
        logger.debug("File: " + url);
        logger.debug("Name: " + name);

        email.attach(attachment);
    }

    email.send();
    for (String url : tempFiles) {
        fs.deleteFile(url);
    }
}

From source file:org.apache.niolex.common.mail.MultiPartMail.java

public static void main(String[] args) throws IOException, EmailException {
    // Create the attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Apache logo");
    attachment.setName("Apache_logo.gif");

    // Create the email message
    MultiPartEmail email = new MultiPartEmail();
    email.setDebug(true);//from  www.  jav  a 2s . com
    // Set email host
    email.setHostName("10.7.2.18");
    //        email.setAuthentication("nice_to_meet", "asf_logo_me");
    //        email.setSSL(true);
    // Set email from
    email.setFrom("lei.gao@renren-inc.com", "Commons Email");
    email.setBounceAddress("lei.gao@renren-inc.com");
    // Set email content
    email.addTo("jiyun.xie@renren-inc.com", "Jiyun Xie");
    email.addTo("lei.gao@renren-inc.com", "Lei Gao");
    email.setSubject("Foll Alert The Git test");
    email.setMsg("Here is Apache's logo, please enjoy it!");

    // add the attachment
    email.attach(attachment);

    // send the email
    email.send();
}

From source file:org.chenillekit.mail.services.TestMailService.java

@Test
public void test_multipartemail_sending() throws EmailException, MessagingException {
    MultiPartEmail email = new MultiPartEmail();
    email.setSubject("Test Mail 2");
    email.addTo("homburgs@gmail.com");
    email.setFrom("homburgs@gmail.com");
    email.setMsg("This is a dummy message text!");
    email.addPart("This is a dummy message part 1!", "text/plain");

    MimeMultipart mmp = new MimeMultipart();
    MimeBodyPart mbp = new MimeBodyPart();

    mbp.setText("This is a dummy MimeBodyPart 1!");

    mmp.addBodyPart(mbp);//from  w  ww . j  av a 2  s .com
    email.addPart(mmp);

    EmailAttachment attachment = new EmailAttachment();
    attachment.setDescription("dummy.txt");
    attachment.setURL(new ClasspathResource("dummy.txt").toURL());
    email.attach(attachment);

    MailService mailService = registry.getService(MailService.class);
    boolean sended = mailService.sendEmail(email);

    assertTrue(sended, "sended");
}

From source file:org.openhab.action.mail.internal.Mail.java

/**
 * Sends an email with attachment(s) via SMTP
 * /*from  w w  w  . ja va  2s.co  m*/
 * @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")
static public 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(StringUtils.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.setTLS(tls);

        if (StringUtils.isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }

        try {
            if (StringUtils.isNotBlank(charset)) {
                email.setCharset(charset);
            }
            email.setFrom(from);
            String[] toList = to.split(";");
            for (String toAddress : toList) {
                email.addTo(toAddress);
            }
            if (!StringUtils.isEmpty(subject))
                email.setSubject(subject);
            if (!StringUtils.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 '{}', useTLS: {}, username: '{}', password '{}'",
                new Object[] { hostname, String.valueOf(port), from, String.valueOf(tls), username, password });
    }

    return success;
}

From source file:org.openhab.binding.mail.internal.MailBuilder.java

/**
 * Build the Mail/*w w  w  .j a  va2 s.c o  m*/
 *
 * @return instance of Email
 * @throws EmailException if something goes wrong
 */
public Email build() throws EmailException {
    Email mail;

    if (attachmentURLs.isEmpty() && attachmentFiles.isEmpty() && html.isEmpty()) {
        // text mail without attachments
        mail = new SimpleEmail();
        if (!text.isEmpty()) {
            mail.setMsg(text);
        }
    } else if (html.isEmpty()) {
        // text mail with attachments
        MultiPartEmail multipartMail = new MultiPartEmail();
        if (!text.isEmpty()) {
            multipartMail.setMsg(text);
        }
        for (File file : attachmentFiles) {
            multipartMail.attach(file);
        }
        for (URL url : attachmentURLs) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(url);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            multipartMail.attach(attachment);
        }
        mail = multipartMail;
    } else {
        // html email
        HtmlEmail htmlMail = new HtmlEmail();
        if (!text.isEmpty()) {
            // alternate text supplied
            htmlMail.setTextMsg(text);
            htmlMail.setHtmlMsg(html);
        } else {
            htmlMail.setMsg(html);
        }
        for (File file : attachmentFiles) {
            htmlMail.attach(new FileDataSource(file), "", "");
        }
        for (URL url : attachmentURLs) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(url);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            htmlMail.attach(attachment);
        }
        mail = htmlMail;
    }

    mail.setTo(recipients);
    mail.setSubject(subject);

    if (!sender.isEmpty()) {
        mail.setFrom(sender);
    }

    return mail;
}

From source file:org.openhab.io.net.actions.Mail.java

/**
 * Sends an email with attachment via SMTP
 * /*from  w w w  .j  a va  2  s .c om*/
 * @param to the email address of the recipient
 * @param subject the subject of the email
 * @param message the body of the email
 * @param attachmentUrl a URL string of the content to send as an attachment
 * 
 * @return <code>true</code>, if sending the email has been successful and 
 * <code>false</code> in all other cases.
 */
static public boolean sendMail(String to, String subject, String message, String attachmentUrl) {
    boolean success = false;
    if (initialized) {
        Email email = new SimpleEmail();
        if (attachmentUrl != null) {
            // Create the attachment
            try {
                email = new MultiPartEmail();
                EmailAttachment attachment = new EmailAttachment();
                attachment.setURL(new URL(attachmentUrl));
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                attachment.setName("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.setTLS(tls);

        if (StringUtils.isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }

        try {
            email.setFrom(from);
            email.addTo(to);
            if (!StringUtils.isEmpty(subject))
                email.setSubject(subject);
            if (!StringUtils.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 '{}', useTLS: {}, username: '{}', password '{}'",
                new String[] { hostname, String.valueOf(port), from, String.valueOf(tls), username, password });
    }

    return success;
}

From source file:org.paxml.bean.EmailTag.java

private EmailAttachment makeAttachment(String att) {
    EmailAttachment r = new EmailAttachment();
    r.setDisposition(EmailAttachment.ATTACHMENT);
    URL url = null;//w ww . ja v a2  s .co m
    try {
        url = new URL(att);
        if (StringUtils.startsWithIgnoreCase(url.getProtocol(), "file")) {
            r.setPath(url.getPath());
        } else {
            r.setURL(url);
        }
    } catch (MalformedURLException e) {
        r.setPath(att);
    }
    r.setName(FilenameUtils.getName(att));
    return r;
}

From source file:org.structr.web.function.SendHtmlMailFunction.java

@Override
public Object apply(final ActionContext ctx, final GraphObject entity, final Object[] sources)
        throws FrameworkException {

    if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 6, 8)) {

        final String from = sources[0].toString();
        final String fromName = sources[1].toString();
        final String to = sources[2].toString();
        final String toName = sources[3].toString();
        final String subject = sources[4].toString();
        final String htmlContent = sources[5].toString();
        String textContent = "";

        if (sources.length >= 7) {
            textContent = sources[6].toString();
        }//from  w  w  w. j  a  v  a  2  s  . c  o m

        List<FileBase> fileNodes = null;
        List<EmailAttachment> attachments = new ArrayList<>();

        try {

            if (sources.length == 8 && sources[7] instanceof List && ((List) sources[7]).size() > 0
                    && ((List) sources[7]).get(0) instanceof FileBase) {

                fileNodes = (List<FileBase>) sources[7];

                for (FileBase fileNode : fileNodes) {

                    final EmailAttachment attachment = new EmailAttachment();
                    attachment.setURL(fileNode.getFileOnDisk().toURI().toURL());
                    attachment.setName(fileNode.getProperty(FileBase.name));
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);

                    attachments.add(attachment);
                }
            }

            return MailHelper.sendHtmlMail(from, fromName, to, toName, null, null, from, subject, htmlContent,
                    textContent, attachments.isEmpty() ? null
                            : attachments.toArray(new EmailAttachment[attachments.size()]));

        } catch (EmailException | MalformedURLException ex) {

            logException(entity, ex, sources);

        }

    } else {

        logParameterError(entity, sources, ctx.isJavaScriptContext());

    }

    return "";
}