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

The following examples show how to use org.apache.commons.mail.MultiPartEmail#addTo() . 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: 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 3
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 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: 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: 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();
      
  }