org.apache.commons.mail.MultiPartEmail Java Examples

The following examples show how to use org.apache.commons.mail.MultiPartEmail. 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: MailUtil.java    From xiaoV with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public static void sendMail(String content) {
	MailSenderInfo mailInfo = getMailSenderInfo(content);
	MultiPartEmail email = new MultiPartEmail();
	try {
		email.setTLS(mailInfo.isSslenable());
		email.setHostName(mailInfo.getMailServerHost());
		email.setAuthentication(mailInfo.getUserName(),
				mailInfo.getPassword());// 用户名和密码
		email.setFrom(mailInfo.getFromAddress()); // 发送方
		email.addTo(mailInfo.getToAddress());// 接收方
		email.setSubject(mailInfo.getSubject()); // 标题
		email.setCharset("UTF-8");
		email.setMsg(mailInfo.getContent()); // 内容
		email.send();

	} catch (EmailException e) {
		e.printStackTrace();
	}
}
 
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: 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 #4
Source File: SendEmail.java    From SpyGen with Apache License 2.0 5 votes vote down vote up
public SendEmail(String subject, String msg) {
    try {
        email = new MultiPartEmail();
        email.setDebug(DEBUG);
        email.setHostName(SMTP);
        email.setSSL(SSL);
        email.addTo(Settings.receiverMail);
        email.setFrom(Settings.senderMail);
        email.setAuthentication(Settings.senderMail, Settings.senderPassword);
        email.setSubject(subject);
        email.setMsg(msg);
    } catch(EmailException ee) {
        ee.printStackTrace();
    }
}
 
Example #5
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 #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 MultiPartEmail createMultiPartEmail(String text) {
    MultiPartEmail email = new MultiPartEmail();
    try {
        email.setMsg(text);
        return email;
    } catch (EmailException e) {
        throw new ActivitiException("Could not create text-only email", e);
    }
}
 
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: MailActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected MultiPartEmail createMultiPartEmail(String text) {
    MultiPartEmail email = new MultiPartEmail();
    try {
        email.setMsg(text);
        return email;
    } catch (EmailException e) {
        throw new FlowableException("Could not create text-only email", e);
    }
}
 
Example #10
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 #11
Source File: MailActivityBehavior.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
protected MultiPartEmail createMultiPartEmail(String text) {
    MultiPartEmail email = new MultiPartEmail();
    try {
        email.setMsg(text);
        return email;
    } catch (EmailException e) {
        throw new FlowableException("Could not create text-only email", e);
    }
}
 
Example #12
Source File: InsecureSmtpSsl.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSSLOnConnect(false); //OK

    Email email2 = new SimpleEmail();
    email2.setHostName("smtp2.googlemail.com");        
    email2.setSSLOnConnect(true); //BAD
    //email2.setSmtpPort(465);
    //email2.setAuthenticator(new DefaultAuthenticator("username", "password"));
    //email2.setFrom("[email protected]");
    //email2.setSubject("TestMail");
    //email2.setMsg("This is a test mail ... :-)");
    //email2.addTo("[email protected]");
    //email2.send();

    MultiPartEmail emailMulti = new MultiPartEmail();
    emailMulti.setHostName("mail.myserver.com");
    emailMulti.setSSLOnConnect(true); //BAD

    HtmlEmail htmlEmail = new HtmlEmail();
    htmlEmail.setHostName("mail.myserver.com");
    htmlEmail.setSSLOnConnect(true); //BAD

    ImageHtmlEmail imageEmail = new ImageHtmlEmail();
    imageEmail.setHostName("mail.myserver.com");
    imageEmail.setSSLOnConnect(true);
    imageEmail.setSSLCheckServerIdentity(true); //OK
    
    ImageHtmlEmail imageEmail2 = new ImageHtmlEmail();
    imageEmail2.setHostName("mail2.myserver.com");
    imageEmail2.setSSLCheckServerIdentity(true); //OK - reversed order
    imageEmail2.setSSLOnConnect(true);

    ImageHtmlEmail imageEmail3 = new ImageHtmlEmail();
    imageEmail3.setHostName("mail3.myserver.com");
    imageEmail3.setSSLOnConnect(true); //BAD
}
 
Example #13
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 #14
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 #15
Source File: MailActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected MultiPartEmail createMultiPartEmail(String text) {
  MultiPartEmail email = new MultiPartEmail();
  try {
    email.setMsg(text);
    return email;
  } catch (EmailException e) {
    throw new ActivitiException("Could not create text-only email", e);
  }
}
 
Example #16
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 #17
Source File: MailActivityBehavior.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
protected MultiPartEmail createMultiPartEmail(String text) {
  MultiPartEmail email = new MultiPartEmail();
  try {
    email.setMsg(text);
    return email;
  } catch (EmailException e) {
    throw new ActivitiException("Could not create text-only email", e);
  }
}
 
Example #18
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();
      
  }
 
Example #19
Source File: PEmailBackend.java    From jphp with Apache License 2.0 4 votes vote down vote up
@Signature
public void __construct() {
    helper = new MultiPartEmail();
}