Java Code Examples for org.apache.commons.mail.MultiPartEmail#attach()

The following examples show how to use org.apache.commons.mail.MultiPartEmail#attach() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendEmail.java    From sAINT with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void sendEmailAttachment(String email_to, String assunto, String msg, String file_logs) {
    File fileLogs = new File(file_logs);
    EmailAttachment attachmentLogs = new EmailAttachment();
    attachmentLogs.setPath(fileLogs.getPath());
    attachmentLogs.setDisposition(EmailAttachment.ATTACHMENT);
    attachmentLogs.setDescription("Logs");
    attachmentLogs.setName(fileLogs.getName());

    try {
        MultiPartEmail email = new MultiPartEmail();
        email.setDebug(debug);
        email.setHostName(smtp);
        email.addTo(email_to);
        email.setFrom(email_from);
        email.setAuthentication(email_from, email_password);
        email.setSubject(assunto);
        email.setMsg(msg);
        email.setSSL(true);
        email.attach(attachmentLogs);
        email.send();
    } catch (EmailException e) {
        System.out.println(e.getMessage());
    }
}
 
Example 2
Source File: InvoiceFacade.java    From jpa-invoicer with The Unlicense 6 votes vote down vote up
public void sendInvoice(final Invoice invoice) throws EmailException, IOException {
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        writeAsPdf(invoice, out);
        ByteArrayDataSource dataSource =
                new ByteArrayDataSource(out.toByteArray(), "application/pdf");
        String fileName = "invoice_" + invoice.getInvoiceNumber() + ".pdf";

        MultiPartEmail email = new MultiPartEmail();
        email.setAuthentication(smtpUsername, smtpPassword);
        email.setHostName(smtpHostname);
        email.setSmtpPort(smtpPort);
        email.setFrom(smtpFrom);
        email.addTo(invoice.getInvoicer().getEmail());
        email.setSubject(smtpSubject);
        email.setMsg(smtpMessage);
        email.attach(dataSource, fileName, "Invoice");
        email.send();
    }
}
 
Example 3
Source File: MailActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException {
  if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) {
    return;
  }
  MultiPartEmail mpEmail = (MultiPartEmail) email;
  for (File file : files) {
    mpEmail.attach(file);
  }
  for (DataSource ds : dataSources) {
    if (ds != null) {
      mpEmail.attach(ds, ds.getName(), null);
    }
  }
}
 
Example 4
Source File: MailActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException {
  if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) {
    return;
  }
  MultiPartEmail mpEmail = (MultiPartEmail) email;
  for (File file : files) {
    mpEmail.attach(file);
  }
  for (DataSource ds : dataSources) {
    if (ds != null) {
      mpEmail.attach(ds, ds.getName(), null);
    }
  }
}
 
Example 5
Source File: SendEmail.java    From sAINT with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void sendEmailAttachment(String email_to, String assunto, String msg, String file, String file_logs) {
    File fileScreenshot = new File(file);
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath(fileScreenshot.getPath());
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Attachment");
    attachment.setName(fileScreenshot.getName());

    File fileLogs = new File(file_logs);
    EmailAttachment attachmentLogs = new EmailAttachment();
    attachmentLogs.setPath(fileLogs.getPath());
    attachmentLogs.setDisposition(EmailAttachment.ATTACHMENT);
    attachmentLogs.setDescription("Logs");
    attachmentLogs.setName(fileLogs.getName());

    try {
        MultiPartEmail email = new MultiPartEmail();
        email.setDebug(debug);
        email.setHostName(smtp);
        email.addTo(email_to);
        email.setFrom(email_from);
        email.setAuthentication(email_from, email_password);
        email.setSubject(assunto);
        email.setMsg(msg);
        email.setSSL(true);
        email.attach(attachment);
        email.attach(attachmentLogs);
        email.send();
    } catch (EmailException e) {
        System.out.println(e.getMessage());
    }
}
 
Example 6
Source File: MailActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException {
    if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) {
        return;
    }
    MultiPartEmail mpEmail = (MultiPartEmail) email;
    for (File file : files) {
        mpEmail.attach(file);
    }
    for (DataSource ds : dataSources) {
        if (ds != null) {
            mpEmail.attach(ds, ds.getName(), null);
        }
    }
}
 
Example 7
Source File: MailActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException {
    if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) {
        return;
    }
    MultiPartEmail mpEmail = (MultiPartEmail) email;
    for (File file : files) {
        mpEmail.attach(file);
    }
    for (DataSource ds : dataSources) {
        if (ds != null) {
            mpEmail.attach(ds, ds.getName(), null);
        }
    }
}
 
Example 8
Source File: MailActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected void attach(Email email, List<File> files, List<DataSource> dataSources) throws EmailException {
    if (!(email instanceof MultiPartEmail && attachmentsExist(files, dataSources))) {
        return;
    }
    MultiPartEmail mpEmail = (MultiPartEmail) email;
    for (File file : files) {
        mpEmail.attach(file);
    }
    for (DataSource ds : dataSources) {
        if (ds != null) {
            mpEmail.attach(ds, ds.getName(), null);
        }
    }
}
 
Example 9
Source File: MailServer.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public void send (String to, String cc, String bcc, String subject, 
   String message, EmailAttachment attachment)
   throws EmailException
{
   MultiPartEmail email = new MultiPartEmail ();
   // Server configuration
   
   email.setMsg (message);
   if (attachment != null) email.attach (attachment);
   
   send (email, to, cc, bcc, subject);
   
}
 
Example 10
Source File: ReportSender.java    From graylog-plugin-aggregates with GNU General Public License v3.0 4 votes vote down vote up
/**
   * Sends an email with a PDF attachment.
   * @throws TransportConfigurationException 
   * @throws EmailException 
   * @throws MessagingException 
   */
  public void sendEmail(String receipient, byte[] pdf) throws TransportConfigurationException, EmailException, MessagingException {
  	if(!configuration.isEnabled()) {
          throw new TransportConfigurationException("Email transport is not enabled in server configuration file!");
      }

      final MultiPartEmail email = new MultiPartEmail();
      email.setCharset(EmailConstants.UTF_8);
      

      if (Strings.isNullOrEmpty(configuration.getHostname())) {
          throw new TransportConfigurationException("No hostname configured for email transport while trying to send alert email!");
      } else {
          email.setHostName(configuration.getHostname());
      }
      email.setSmtpPort(configuration.getPort());
      if (configuration.isUseSsl()) {
          email.setSslSmtpPort(Integer.toString(configuration.getPort()));
      }

      if(configuration.isUseAuth()) {
          email.setAuthenticator(new DefaultAuthenticator(
                  Strings.nullToEmpty(configuration.getUsername()),
                  Strings.nullToEmpty(configuration.getPassword())
          ));
      }

      email.setSSLOnConnect(configuration.isUseSsl());
      email.setStartTLSEnabled(configuration.isUseTls());
      if (pluginConfig != null && !Strings.isNullOrEmpty(pluginConfig.getString("sender"))) {
          email.setFrom(pluginConfig.getString("sender"));
      } else {
          email.setFrom(configuration.getFromEmail());
      }
      
      
      email.setSubject("Graylog Aggregates Report");

      Calendar c = Calendar.getInstance();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

      email.attach(new ByteArrayDataSource(pdf, "application/pdf"),
      	      "aggregates_report_" + df.format(c.getTime()) +".pdf", "Graylog Aggregates Report",
      	       EmailAttachment.ATTACHMENT);
              
      
      email.setMsg("Please find the report attached.");

      email.addTo(receipient);
              	
     	LOG.debug("sending report to " + email.getToAddresses().toString());
      	
     	email.send();
      
  }