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

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

Introduction

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

Prototype

EmailAttachment

Source Link

Usage

From source file:com.smi.travel.controller.mail.SendMail.java

public static void main(String args[]) throws Exception {
    EmailAttachment attachment = new EmailAttachment();
    HtmlEmail email = new HtmlEmail();
    try {//from w w w . j  av  a  2  s .  com
        //            attachment.setPath("C:\\Users\\chonnasith\\Desktop\\test.txt");
        //            attachment.setDescription("file attachment");
        //            attachment.setName("test.txt");
        //            email.attach(attachment);
        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setAuthentication(username, password);
        email.setSSLOnConnect(true);
        //            email.setStartTLSEnabled(true);
        //            email.setStartTLSRequired(true);
        email.setFrom(username);
        email.setSubject(subject);
        email.addTo(addto);
        email.setHtmlMsg(message);
        email.send();
        System.out.println("Email Send");
        System.out.println("Port : " + email.getSmtpPort());
    } catch (EmailException ex) {
        System.out.println("Email Exception");
        System.out.println("Port : " + email.getSmtpPort());
        ex.printStackTrace();
    }

    //        InetAddress host = InetAddress.getByName("mail.foobar.com");
    //        System.out.println("host.isReachable(1000) = " + host.isReachable(1000));

    //        int port = 587;
    //        String host = "smtp.gmail.com";
    //        String user = "username@gmail.com";
    //        String pwd = "email password";
    //
    //        try {
    //            Properties props = new Properties();
    //            // required for gmail 
    //            props.put("mail.smtp.starttls.enable","true");
    //            props.put("mail.smtp.auth", "true");
    //            // or use getDefaultInstance instance if desired...
    //            Session session = Session.getInstance(props, null);
    //            Transport transport = session.getTransport("smtp");
    //            transport.connect(host, port, user, pwd);
    //            transport.close();
    //            System.out.println("success");
    //         } 
    //         catch(AuthenticationFailedException e) {
    //               System.out.println("AuthenticationFailedException - for authentication failures");
    //               e.printStackTrace();
    //         }
    //         catch(MessagingException e) {
    //               System.out.println("for other failures");
    //               e.printStackTrace();
    //         }

    //        Properties prop=new Properties();
    //        prop.put("mail.smtp.auth", "true");
    //        prop.put("mail.smtp.host", "smtp.gmail.com");
    //        prop.put("mail.smtp.port", "587");
    //        prop.put("mail.smtp.starttls.enable", "true");
    //
    //        Session session = Session.getDefaultInstance(prop,
    //        new javax.mail.Authenticator() {
    //            protected PasswordAuthentication getPasswordAuthentication() {
    //                return new PasswordAuthentication("finance@wendytour", "wendytr");
    //          }
    //        });
    //        
    //        try {
    //            String body="Dear Renish Khunt Welcome";
    //            String htmlBody = "<strong>This is an HTML Message</strong>";
    //            String textBody = "This is a Text Message.";
    //            Message message = new MimeMessage(session);
    //            message.setFrom(new InternetAddress(username));
    //            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(addto));
    //            message.setSubject("Testing Subject");
    //            MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    //            mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    //            mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    //            mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    //            mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    //            mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    //            CommandMap.setDefaultCommandMap(mc);
    //            message.setText(htmlBody);
    //            message.setContent(textBody, "text/html");
    //            Transport.send(message);
    //
    //            System.out.println("Done");
    //
    //        } catch (MessagingException e) {
    //            e.printStackTrace();
    //        }

    //        final String fromEmail = "finance@wendytour"; //requires valid gmail id
    //        final String password = "wendytr"; // correct password for gmail id
    //        final String toEmail = "wee.chonnasith@gmail.com"; // can be any email id 
    //         
    //        System.out.println("TLSEmail Start");
    //        Properties props = new Properties();
    //        props.put("mail.smtp.host", ""); //SMTP Host
    //        props.put("mail.smtp.port", "587"); //TLS Port
    //        props.put("mail.smtp.auth", "true"); //enable authentication
    //        props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
    //         
    //                //create Authenticator object to pass in Session.getInstance argument
    //        Authenticator auth = new Authenticator() {
    //            //override the getPasswordAuthentication method
    //            protected PasswordAuthentication getPasswordAuthentication() {
    //                return new PasswordAuthentication(fromEmail, password);
    //            }
    //        };
    //        Session session = Session.getInstance(props, auth);
    //         
    //        EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", "TLSEmail Testing Body");
    //         

}

From source file:com.github.somi92.seecsk.util.email.EmailSender.java

public static void sendEmail(EmailContainer ec) throws RuntimeException {

    try {/* ww  w.  ja  v a 2  s . c  om*/

        String user = Config.vratiInstancu().vratiVrednost(Constants.OrgInfoConfigKeys.ORGANISATION_EMAIL);
        String password = Config.vratiInstancu()
                .vratiVrednost(Constants.OrgInfoConfigKeys.ORGANISATION_EMAIL_PASSWORD);

        EmailAttachment ea = new EmailAttachment();
        ea.setPath(ec.getAttachmentPath());
        ea.setDisposition(EmailAttachment.ATTACHMENT);
        ea.setDescription("Primer ispravno popunjene uplatnice za ?lanarinu");
        ea.setName("uplatnica.pdf");

        MultiPartEmail mpe = new MultiPartEmail();
        mpe.setDebug(true);
        mpe.setAuthenticator(new DefaultAuthenticator(user, password));
        mpe.setHostName(
                Config.vratiInstancu().vratiVrednost(Constants.EmailServerConfigKeys.EMAIL_SERVER_HOST));
        mpe.setSSLOnConnect(true);
        mpe.setStartTLSEnabled(true);
        mpe.setSslSmtpPort(
                Config.vratiInstancu().vratiVrednost(Constants.EmailServerConfigKeys.EMAIL_SERVER_PORT));
        mpe.setSubject(ec.getSubject());
        mpe.setFrom(ec.getFromEmail(),
                Config.vratiInstancu().vratiVrednost(Constants.OrgInfoConfigKeys.ORGANISATION_NAME));
        mpe.setMsg(ec.getMessage());

        mpe.addTo(ec.getToEmail());
        mpe.attach(ea);

        mpe.send();

    } catch (EmailException ex) {
        ex.printStackTrace();
        throw new RuntimeException("Sistem nije uspeo da poalje email. Pokuajte ponovo.");
    }
}

From source file:br.com.colmeiatecnologia.EmailMarketing.model.email.EnviaEmailAnexoModel.java

public void anexar(String caminho, String descricao, String nome) throws EmailException {
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(caminho);/*from w w w.  ja va2  s .  c  o  m*/
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription(descricao);
    attachment.setName(nome);

    MultiPartEmail.class.cast(email).attach(attachment);
}

From source file:com.esofthead.mycollab.module.mail.FileEmailAttachmentSource.java

@Override
public EmailAttachment getAttachmentObj() {
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(file.getPath());//from   w  w  w. j  ava2  s .com
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName(file.getName());
    return attachment;
}

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

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

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

@Override
public EmailAttachment getAttachmentObj() {
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(file.getPath());//w ww.j  a  va  2s  . c o m
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setName((name == null) ? file.getName() : name);
    return attachment;
}

From source file:com.vicky.common.utils.sendemail.SendEmailImpl.java

@Override
public void send(Mail mail) throws Exception {
    try {//from w  w w  . j av  a2  s . c o m
        HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setHostName(mail.getHost());
        htmlEmail.setCharset(Mail.ENCODEING);
        htmlEmail.addTo(mail.getReceiverEmailAddress());
        htmlEmail.setFrom(mail.getSenderEmailAddress(), mail.getName());
        htmlEmail.setAuthentication(mail.getSenderUsername(), mail.getSenderPassword());
        htmlEmail.setSubject(mail.getSubject());
        htmlEmail.setMsg(mail.getMessage());
        for (MailAttach mailFile : mail.getMailAttachs()) {
            EmailAttachment attachment = new EmailAttachment();//  
            attachment.setPath(mailFile.getFilePath());//?    
            //attachment.setURL(new URL("http://www.baidu.com/moumou"));//?  
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setName(MimeUtility.encodeText(mailFile.getFileName()));//??  
            htmlEmail.attach(attachment);//,?
        }
        htmlEmail.send();
    } catch (Exception exception) {
        exception.printStackTrace();
        this.logger.error("toString" + exception.toString());
        this.logger.error("getMessage" + exception.getMessage());
        this.logger.error("exception", exception);
        throw new StatusMsgException(
                "??,??,?,?V!");
    }
}

From source file:br.com.spolti.tsmreport.functions.SendEmail.java

public void senderWithAttachment(String mailSender, String Subject, String msg, String dests, String smtpHost,
        String outputFile) throws UnknownHostException {

    FileOperations readFile = new FileOperations();
    MultiPartEmail email = new MultiPartEmail();
    EmailAttachment attachment = new EmailAttachment();

    String dest_temp = dests;// www .  java  2 s  . c o  m
    String[] dest = dest_temp.split(",");

    try {

        email.setHostName(smtpHost);
        for (int i = 0; i < dest.length; i++) {
            email.addTo(dest[i]);
        }

        attachment.setPath(outputFile);
        attachment.setDisposition(EmailAttachment.ATTACHMENT);
        attachment.setDescription(outputFile);
        attachment.setName("tsmreport.txt");
        email.setFrom(mailSender);
        email.setSubject(Subject);
        Logger.getLogger(br.com.spolti.tsmreport.functions.SendEmail.class)
                .info(readFile.format2html(outputFile));
        msg = msg + " \n" + readFile.format2html(outputFile);
        msg = msg
                + "Developed by Spolti (filippespolti@gmail.com), if you wanna contribute please do not hesitate. Visit https://github.com/spolti/tsmreport.";
        email.setMsg(msg);
        email.getMailSession().getProperties().setProperty("mail.smtp.localhost", "localhost");
        email.attach(attachment);
        email.send();

        Logger.getLogger(br.com.spolti.tsmreport.functions.SendEmail.class).info("----> Email sended.");

    } catch (Exception e) {
        Logger.getLogger(br.com.spolti.tsmreport.functions.SendEmail.class).error("----> Email not sended.");
        Logger.getLogger(br.com.spolti.tsmreport.functions.SendEmail.class).error(e);
    }
}

From source file:ec.edu.espe.distribuidas.web.ReportFacturaBean.java

public void testMultiPartEmail() throws UnsupportedEncodingException, EmailException, MalformedURLException {

    EmailAttachment att2 = new EmailAttachment();
    att2.setPath(//from w  w  w  . j a  v  a 2 s.  com
            "C:\\Users\\Andres Vr\\Documents\\Git\\ProyectoMaldito7.0\\ProyectMaltido2\\ProyectoMaldito2\\ProyectoMaldito2-web\\src\\main\\webapp\\pdf\\factura.pdf");
    att2.setDisposition(EmailAttachment.ATTACHMENT);
    att2.setDescription("Envio Factura Mantenimiento SpotLinght&Wires");

    MultiPartEmail email = new MultiPartEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(587);
    email.setSSLOnConnect(true);
    email.setAuthentication("spotwires@gmail.com", "084383260a");

    email.addTo("avrz237@gmail.com");
    email.setFrom("spotwires@gmail.com");
    email.setSubject("Factura");
    email.setMsg("Adjunto Factura");

    email.attach(att2);

    email.send();
    //            SimpleEmail mail = new SimpleEmail();
    // 
    //        //Configuracion necesaria para GMAIL
    //        mail.setHostName("smtp.gmail.com");
    //        mail.setTLS(true);
    //        mail.setSmtpPort(587);
    //        mail.setSSL(true);
    //        //En esta seccion colocar cuenta de usuario de Gmail y contrasea
    //        mail.setAuthentication("spotwires@gmail.com", "084383260a");
    // 
    //        //Cuenta de Email Destino
    //        mail.addTo("avrz237@gmail.com");
    //        //Cuenta de Email Origen, la misma con la que nos autenticamos
    //        mail.setFrom("spotwires@gmail.com");
    //        //Titulo del Email
    //        mail.setSubject("Email enviado usando Apache Commons Email");
    //        //Contenido del Email
    //        mail.setMsg("Mail enviado usando una cuenta de correo de GMAIL");
    //        mail.send();
}

From source file:com.emo.ananas.reporters.EmailReporter.java

public void report(final String reportName, final File report) {
    logger.info("send email report {} for {}", reportName, email);

    Preconditions.checkNotNull(reportName, "report name is required for email reporting");
    Preconditions.checkNotNull(report, "report file is required for email reporting");

    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(report.getAbsolutePath());
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription(reportName);

    final String extension;

    int i = report.getName().lastIndexOf('.');
    if (i > 0) {
        extension = report.getName().substring(i);
    } else {/*www.j ava  2 s . c o  m*/
        extension = "";
    }

    attachment.setName(reportName + extension);

    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(sender.smtp);

    try {
        for (final String aTo : this.email.to) {
            email.addTo(aTo);
        }

        email.setFrom(this.email.from);
        email.setSubject(this.email.subject);
        email.setMsg("Report for " + reportName);

        email.attach(attachment);

        email.send();
    } catch (EmailException e) {
        throw new RuntimeException("failed to send email for report " + reportName + " with " + this.email, e);
    }
}