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

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

Introduction

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

Prototype

public String getName() 

Source Link

Document

Get the name.

Usage

From source file:com.irurueta.server.commons.email.ApacheMailTextEmailMessageWithAttachments.java

/**
 * Builds email content to be sent using an email sender.
 * @param content instance where content must be set.
 * @throws com.irurueta.server.commons.email.EmailException if setting mail 
 * content fails.//from  w w  w. java 2 s.c  o m
 */
@Override
protected void buildContent(MultiPartEmail content) throws com.irurueta.server.commons.email.EmailException {
    try {
        if (getText() != null) {
            content.setMsg(getText());
        }

        //add attachments
        List<EmailAttachment> attachments = getAttachments();
        org.apache.commons.mail.EmailAttachment apacheAttachment;
        if (attachments != null) {
            for (EmailAttachment attachment : attachments) {
                //only add attachments with files
                if (attachment.getAttachment() == null) {
                    continue;
                }

                apacheAttachment = new org.apache.commons.mail.EmailAttachment();
                apacheAttachment.setPath(attachment.getAttachment().getAbsolutePath());
                apacheAttachment.setDisposition(org.apache.commons.mail.EmailAttachment.ATTACHMENT);
                if (attachment.getName() != null) {
                    apacheAttachment.setName(attachment.getName());
                }

                content.attach(apacheAttachment);
            }
        }
    } catch (EmailException e) {
        throw new com.irurueta.server.commons.email.EmailException(e);
    }
}

From source file:com.itcs.commons.email.impl.RunnableSendHTMLEmail.java

private void addAttachments(MultiPartEmail email, List<EmailAttachment> attachments) throws EmailException {

    if (attachments != null && attachments.size() > 0) {
        String maxStringValue = getSession().getProperty(MAX_ATTACHMENTS_SIZE_PROP_NAME);
        //            System.out.println("maxStringValue= " + maxStringValue);
        long maxAttachmentSize = DISABLE_MAX_ATTACHMENTS_SIZE;
        try {// ww w. jav a2s. co  m
            maxAttachmentSize = Long.parseLong(maxStringValue);
        } catch (NumberFormatException e) {
            Logger.getLogger(RunnableSendHTMLEmail.class.getName()).log(Level.WARNING,
                    "DISABLE_MAX_ATTACHMENTS_SIZE MailSession does not have property "
                            + MAX_ATTACHMENTS_SIZE_PROP_NAME);
        }

        long size = 0;
        for (EmailAttachment attach : attachments) {
            if (maxAttachmentSize != EmailClient.DISABLE_MAX_ATTACHMENTS_SIZE) {
                size += attach.getSize();
                if (size > maxAttachmentSize) {
                    throw new EmailException(
                            "Adjuntos exceden el tamao maximo permitido (" + maxAttachmentSize + "),"
                                    + " pruebe enviando menos archivos adjuntos, o de menor tamao.");
                }
            }
            if (attach.getData() != null) {
                try {
                    email.attach(new ByteArrayDataSource(attach.getData(), attach.getMimeType()),
                            attach.getName(), attach.getMimeType());
                } catch (IOException e) {
                    throw new EmailException("IOException Attachment has errors," + e.getMessage());
                } catch (EmailException e) {
                    throw new EmailException("EmailException Attachment has errors," + e.getMessage());
                }
            } else {
                email.attach(attach);
            }
        }

    }

}

From source file:com.ms.commons.message.impl.sender.AbstractEmailSender.java

/**
 * Email//from   www.ja v  a 2 s  . c  o m
 * 
 * @param mail
 * @return
 * @throws Exception
 */
protected Email getEmail(MsunMail mail) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("send mail use smth : " + hostName);
    }
    // set env for logger
    mail.getEnvironment().setHostName(hostName);
    mail.getEnvironment().setUser(user);
    mail.getEnvironment().setPassword(password);

    Email email = null;

    if (!StringUtils.isEmpty(mail.getHtmlMessage())) {
        email = makeHtmlEmail(mail, mail.getCharset());
    } else {
        if ((mail.getAttachment() == null || mail.getAttachment().length == 0)
                && mail.getAttachments().isEmpty()) {
            email = makeSimpleEmail(mail, mail.getCharset());
        } else {
            email = makeSimpleEmailWithAttachment(mail, mail.getCharset());
        }
    }

    if (auth) {
        // email.setAuthenticator(new MyAuthenticator(user, password));
        email.setAuthentication(user, password);
    }
    email.setHostName(hostName);

    if (mail.getTo() == null) {
        mail.setTo(defaultTo.split(";"));
    }

    if (StringUtils.isEmpty(mail.getFrom())) {
        mail.setFrom(defaultFrom);
    }

    email.setFrom(mail.getFrom(), mail.getFromName());

    List<String> unqualifiedReceiver = mail.getUnqualifiedReceiver();
    String[] mailTo = mail.getTo();
    String[] mailCc = mail.getCc();
    String[] mailBcc = mail.getBcc();
    if (unqualifiedReceiver != null && unqualifiedReceiver.size() > 0) {
        if (mailTo != null && mailTo.length > 0) {
            mailTo = filterReceiver(mailTo, unqualifiedReceiver);
        }
        if (mailCc != null && mailCc.length > 0) {
            mailCc = filterReceiver(mailCc, unqualifiedReceiver);
        }
        if (mailBcc != null && mailBcc.length > 0) {
            mailBcc = filterReceiver(mailBcc, unqualifiedReceiver);
        }
    }

    if (mailTo == null && mailCc == null && mailBcc == null) {
        throw new MessageSerivceException("?????");
    }

    int count = 0;

    if (mailTo != null) {
        count += mailTo.length;
        for (String to : mailTo) {
            email.addTo(to);
        }
    }

    if (mailCc != null) {
        count += mailCc.length;
        for (String cc : mailCc) {
            email.addCc(cc);
        }
    }
    if (mailBcc != null) {
        count += mailBcc.length;
        for (String bcc : mailBcc) {
            email.addBcc(bcc);
        }
    }

    if (count < 1) {
        throw new MessageSerivceException("?????");
    }

    if (!StringUtils.isEmpty(mail.getReplyTo())) {
        email.addReplyTo(mail.getReplyTo());
    }

    if (mail.getHeaders() != null) {
        for (Iterator<String> iter = mail.getHeaders().keySet().iterator(); iter.hasNext();) {
            String key = (String) iter.next();
            email.addHeader(key, (String) mail.getHeaders().get(key));
        }
    }

    email.setSubject(mail.getSubject());

    // 
    if (email instanceof MultiPartEmail) {
        MultiPartEmail multiEmail = (MultiPartEmail) email;

        if (mail.getAttachments().isEmpty()) {
            File[] fs = mail.getAttachment();
            if (fs != null && fs.length > 0) {
                for (int i = 0; i < fs.length; i++) {
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setPath(fs[i].getAbsolutePath());
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    attachment.setName(MimeUtility.encodeText(fs[i].getName()));// ???
                    multiEmail.attach(attachment);
                }
            }
        } else {
            for (MsunMailAttachment attachment : mail.getAttachments()) {
                DataSource ds = new ByteArrayDataSource(attachment.getData(), null);
                multiEmail.attach(ds, MimeUtility.encodeText(attachment.getName()), null);
            }
        }
    }
    return email;
}

From source file:org.agnitas.util.ImportUtils.java

public static boolean sendEmailWithAttachments(String from, String fromName, String to, String subject,
        String message, EmailAttachment[] attachments) {
    boolean result = true;
    try {//from  www  . j a v a 2s  .  c  om
        // Create the email message
        MultiPartEmail email = new MultiPartEmail();
        email.setCharset("UTF-8");
        email.setHostName(AgnUtils.getDefaultValue("system.mail.host"));
        email.addTo(to);

        if (fromName == null || fromName.equals(""))
            email.setFrom(from);
        else
            email.setFrom(from, fromName);

        email.setSubject(subject);
        email.setMsg(message);

        //bounces and reply forwarded to support@agnitas.de
        String replyName = AgnUtils.getDefaultValue("import.report.replyTo.name");
        if (replyName == null || replyName.equals(""))
            email.addReplyTo(AgnUtils.getDefaultValue("import.report.replyTo.address"));
        else
            email.addReplyTo(AgnUtils.getDefaultValue("import.report.replyTo.address"), replyName);

        email.setBounceAddress(AgnUtils.getDefaultValue("import.report.bounce"));

        // Create and attach attachments
        for (EmailAttachment attachment : attachments) {
            ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment.getData(),
                    attachment.getType());
            email.attach(dataSource, attachment.getName(), attachment.getDescription());
        }

        // send the email
        email.send();
    } catch (Exception e) {
        AgnUtils.logger().error("sendEmailAttachment: " + e.getMessage());
        result = false;
    }
    return result;
}

From source file:org.fao.geonet.util.MailUtil.java

/**
 * Send an html mail with atachments// w w  w  .  j a  va 2s. c  om
 *
 * @param toAddress
 * @param from
 * @param subject
 * @param htmlMessage
 * @param attachment
 * @throws EmailException
 */
public static Boolean sendHtmlMailWithAttachment(List<String> toAddress, String from, String subject,
        String htmlMessage, List<EmailAttachment> attachment, SettingManager settings) {
    // Create data information to compose the mail
    HtmlEmail email = new HtmlEmail();
    String username = settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_USERNAME);
    String password = settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_PASSWORD);
    Boolean ssl = settings.getValueAsBool(Settings.SYSTEM_FEEDBACK_MAILSERVER_SSL, false);
    Boolean tls = settings.getValueAsBool(Settings.SYSTEM_FEEDBACK_MAILSERVER_TLS, false);

    String hostName = settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_HOST);
    Integer smtpPort = Integer.valueOf(settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_PORT));
    Boolean ignoreSslCertificateErrors = settings
            .getValueAsBool(Settings.SYSTEM_FEEDBACK_MAILSERVER_IGNORE_SSL_CERTIFICATE_ERRORS, false);

    configureBasics(hostName, smtpPort, from, username, password, email, ssl, tls, ignoreSslCertificateErrors);

    for (EmailAttachment attach : attachment) {
        try {
            email.attach(attach);
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error attaching attachment " + attach.getName(), e);
        }
    }

    email.setSubject(subject);
    try {
        email.setHtmlMsg(htmlMessage);
    } catch (EmailException e1) {
        Log.error(LOG_MODULE_NAME, "Error setting email HTML message", e1);
        return false;
    }

    // send to all mails extracted from settings
    for (String add : toAddress) {
        try {
            email.addBcc(add);
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error setting email BCC address " + add, e);
            return false;
        }
    }

    return send(email);
}