Java Code Examples for org.apache.commons.mail.Email

The following examples show how to use org.apache.commons.mail.Email. These examples are extracted from open source projects. 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 Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void setFrom(Email email, String from, String tenantId) {
  String fromAddress = null;

  if (from != null) {
    fromAddress = from;
  } else { // use default configured from address in process engine config
    if (tenantId != null && tenantId.length() > 0) {
      Map<String, MailServerInfo> mailServers = Context.getProcessEngineConfiguration().getMailServers();
      if (mailServers != null && mailServers.containsKey(tenantId)) {
        MailServerInfo mailServerInfo = mailServers.get(tenantId);
        fromAddress = mailServerInfo.getMailServerDefaultFrom();
      }
    }

    if (fromAddress == null) {
      fromAddress = Context.getProcessEngineConfiguration().getMailServerDefaultFrom();
    }
  }

  try {
    email.setFrom(fromAddress);
  } catch (EmailException e) {
    throw new ActivitiException("Could not set " + from + " as from address in email", e);
  }
}
 
Example #2
Source Project: shepher   Author: XiaoMi   File: GeneralMailSender.java    License: Apache License 2.0 6 votes vote down vote up
protected void send(String mailAddress, String title, String content) {
    if (StringUtils.isBlank(mailAddress)) {
        return;
    }

    try {
        Email email = new HtmlEmail();
        email.setHostName(hostname);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setSmtpPort(port);
        email.setFrom(from, fromname);
        email.setSubject(title);
        email.setMsg(content);
        email.addTo(mailAddress.split(mailAddressEndSeparator));
        email.send();
    } catch (Exception e) {
        logger.error("Send Mail Error", e);
    }
}
 
Example #3
Source Project: bobcat   Author: Cognifide   File: EmailSender.java    License: Apache License 2.0 6 votes vote down vote up
public void sendEmail(final EmailData emailData) {
  try {
    Email email = new SimpleEmail();
    email.setHostName(smtpServer);
    email.setSmtpPort(smtpPort);
    email.setAuthenticator(new DefaultAuthenticator(username, password));
    email.setSSLOnConnect(secure);
    email.setFrom(emailData.getAddressFrom());
    email.setSubject(emailData.getSubject());
    email.setMsg(emailData.getMessageContent());
    email.addTo(emailData.getAddressTo());
    email.send();
  } catch (org.apache.commons.mail.EmailException e) {
    throw new EmailException(e);
  }
}
 
Example #4
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void addTo(CommandContext commandContext, Email email, String to, String tenantId) {
    if (to == null) {
        // To has to be set, otherwise it can fallback to the forced To and then it won't be noticed early on
        throw new FlowableException("No recipient could be found for sending email");
    }
    String newTo = getForceTo(commandContext, tenantId);
    if (newTo == null) {
        newTo = to;
    }
    String[] tos = splitAndTrim(newTo);
    if (tos != null) {
        for (String t : tos) {
            try {
                email.addTo(t);
            } catch (EmailException e) {
                throw new FlowableException("Could not add " + t + " as recipient", e);
            }
        }
    } else {
        throw new FlowableException("No recipient could be found for sending email");
    }
}
 
Example #5
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void setFrom(CommandContext commandContext, Email email, String from, String tenantId) {
    String fromAddress = null;

    if (from != null) {
        fromAddress = from;
    } else { // use default configured from address in process engine config
        if (tenantId != null && tenantId.length() > 0) {
            Map<String, MailServerInfo> mailServers = CommandContextUtil.getCmmnEngineConfiguration(commandContext).getMailServers();
            if (mailServers != null && mailServers.containsKey(tenantId)) {
                MailServerInfo mailServerInfo = mailServers.get(tenantId);
                fromAddress = mailServerInfo.getMailServerDefaultFrom();
            }
        }

        if (fromAddress == null) {
            fromAddress = CommandContextUtil.getCmmnEngineConfiguration(commandContext).getMailServerDefaultFrom();
        }
    }

    try {
        email.setFrom(fromAddress);
    } catch (EmailException e) {
        throw new FlowableException("Could not set " + from + " as from address in email", e);
    }
}
 
Example #6
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void addCc(CommandContext commandContext, Email email, String cc, String tenantId) {
    if (cc == null) {
        return;
    }

    String newCc = getForceTo(commandContext, tenantId);
    if (newCc == null) {
        newCc = cc;
    }
    String[] ccs = splitAndTrim(newCc);
    if (ccs != null) {
        for (String c : ccs) {
            try {
                email.addCc(c);
            } catch (EmailException e) {
                throw new FlowableException("Could not add " + c + " as cc recipient", e);
            }
        }
    }
}
 
Example #7
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void addBcc(CommandContext commandContext, Email email, String bcc, String tenantId) {
    if (bcc == null) {
        return;
    }
    String newBcc = getForceTo(commandContext, tenantId);
    if (newBcc == null) {
        newBcc = bcc;
    }
    String[] bccs = splitAndTrim(newBcc);
    if (bccs != null) {
        for (String b : bccs) {
            try {
                email.addBcc(b);
            } catch (EmailException e) {
                throw new FlowableException("Could not add " + b + " as bcc recipient", e);
            }
        }
    }
}
 
Example #8
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void addTo(Email email, String to, String tenantId) {
    if (to == null) {
        // To has to be set, otherwise it can fallback to the forced To and then it won't be noticed early on
        throw new FlowableException("No recipient could be found for sending email");
    }
    String newTo = getForceTo(tenantId);
    if (newTo == null) {
        newTo = to;
    }
    String[] tos = splitAndTrim(newTo);
    if (tos != null) {
        for (String t : tos) {
            try {
                email.addTo(t);
            } catch (EmailException e) {
                throw new FlowableException("Could not add " + t + " as recipient", e);
            }
        }
    } else {
        throw new FlowableException("No recipient could be found for sending email");
    }
}
 
Example #9
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void setFrom(Email email, String from, String tenantId) {
    String fromAddress = null;

    if (from != null) {
        fromAddress = from;
    } else { // use default configured from address in process engine config
        if (tenantId != null && tenantId.length() > 0) {
            Map<String, MailServerInfo> mailServers = CommandContextUtil.getProcessEngineConfiguration().getMailServers();
            if (mailServers != null && mailServers.containsKey(tenantId)) {
                MailServerInfo mailServerInfo = mailServers.get(tenantId);
                fromAddress = mailServerInfo.getMailServerDefaultFrom();
            }
        }

        if (fromAddress == null) {
            fromAddress = CommandContextUtil.getProcessEngineConfiguration().getMailServerDefaultFrom();
        }
    }

    try {
        email.setFrom(fromAddress);
    } catch (EmailException e) {
        throw new FlowableException("Could not set " + from + " as from address in email", e);
    }
}
 
Example #10
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void addCc(Email email, String cc, String tenantId) {
    if (cc == null) {
        return;
    }

    String newCc = getForceTo(tenantId);
    if (newCc == null) {
        newCc = cc;
    }
    String[] ccs = splitAndTrim(newCc);
    if (ccs != null) {
        for (String c : ccs) {
            try {
                email.addCc(c);
            } catch (EmailException e) {
                throw new FlowableException("Could not add " + c + " as cc recipient", e);
            }
        }
    }
}
 
Example #11
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void addBcc(Email email, String bcc, String tenantId) {
    if (bcc == null) {
        return;
    }
    String newBcc = getForceTo(tenantId);
    if (newBcc == null) {
        newBcc = bcc;
    }
    String[] bccs = splitAndTrim(newBcc);
    if (bccs != null) {
        for (String b : bccs) {
            try {
                email.addBcc(b);
            } catch (EmailException e) {
                throw new FlowableException("Could not add " + b + " as bcc recipient", e);
            }
        }
    }
}
 
Example #12
Source Project: flowable-engine   Author: flowable   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void setFrom(Email email, String from, String tenantId) {
    String fromAddress = null;

    if (from != null) {
        fromAddress = from;
    } else { // use default configured from address in process engine config
        if (tenantId != null && tenantId.length() > 0) {
            Map<String, MailServerInfo> mailServers = Context.getProcessEngineConfiguration().getMailServers();
            if (mailServers != null && mailServers.containsKey(tenantId)) {
                MailServerInfo mailServerInfo = mailServers.get(tenantId);
                fromAddress = mailServerInfo.getMailServerDefaultFrom();
            }
        }

        if (fromAddress == null) {
            fromAddress = Context.getProcessEngineConfiguration().getMailServerDefaultFrom();
        }
    }

    try {
        email.setFrom(fromAddress);
    } catch (EmailException e) {
        throw new ActivitiException("Could not set " + from + " as from address in email", e);
    }
}
 
Example #13
Source Project: mamute   Author: caelum   File: NewQuestionMailer.java    License: Apache License 2.0 6 votes vote down vote up
public void send(List<User> subscribed, Question question) {
	linker.linkTo(QuestionController.class).showQuestion(question, question.getSluggedTitle());
	String questionLink = linker.get();
	TemplateMail template = templates.template("new_question_notification")
			.with("question", question)
			.with("bundle", bundle)
			.with("questionLink", questionLink)
			.with("logoUrl", emailLogo);
	for (User user : subscribed) {
		boolean notSameAuthor = !user.equals(question.getAuthor());
		if (notSameAuthor) {
			Email email = template.to(user.getName(), user.getEmail());
			mailer.asyncSend(email);
		}
	}
}
 
Example #14
Source Project: mamute   Author: caelum   File: ForgotPasswordController.java    License: Apache License 2.0 6 votes vote down vote up
@Post
public void requestEmailWithToken(String email) {
	User user = users.loadByEmail(email);

	if (user == null) {
		validator.add(messageFactory.build("error", "forgot_password.invalid_email"));
		validator.onErrorRedirectTo(this).forgotPasswordForm();
		return;
	}

	Email forgotPasswordEmail = emailWithTokenFor(user);
	try {
		mailer.send(forgotPasswordEmail);
		result.include("mamuteMessages", Arrays.asList(
					messageFactory.build("confirmation", "forgot_password.sent_mail", user.getEmail()),
					messageFactory.build("confirmation", "forgot_password.sent_mail.warn")
				));
		result.redirectTo(this).forgotPasswordForm();
	} catch (EmailException e) {
		validator.add(messageFactory.build("error", "forgot_password.send_mail.error"));
		validator.onErrorRedirectTo(this).forgotPasswordForm();
	}	
}
 
Example #15
Source Project: restcommander   Author: eBay   File: Mail.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *
 */
public static Email buildMessage(Email email) throws EmailException {

    String from = Play.configuration.getProperty("mail.smtp.from");
    if (email.getFromAddress() == null && !StringUtils.isEmpty(from)) {
        email.setFrom(from);
    } else if (email.getFromAddress() == null) {
        throw new MailException("Please define a 'from' email address", new NullPointerException());
    }
    if ((email.getToAddresses() == null || email.getToAddresses().size() == 0) &&
        (email.getCcAddresses() == null || email.getCcAddresses().size() == 0)  &&
        (email.getBccAddresses() == null || email.getBccAddresses().size() == 0)) 
    {
        throw new MailException("Please define a recipient email address", new NullPointerException());
    }
    if (email.getSubject() == null) {
        throw new MailException("Please define a subject", new NullPointerException());
    }
    if (email.getReplyToAddresses() == null || email.getReplyToAddresses().size() == 0) {
        email.addReplyTo(email.getFromAddress().getAddress());
    }

    return email;
}
 
Example #16
Source Project: camunda-bpm-platform   Author: camunda   File: MailActivityBehavior.java    License: Apache License 2.0 6 votes vote down vote up
protected void setMailServerProperties(Email email) {
  ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();

  String host = processEngineConfiguration.getMailServerHost();
  ensureNotNull("Could not send email: no SMTP host is configured", "host", host);
  email.setHostName(host);

  int port = processEngineConfiguration.getMailServerPort();
  email.setSmtpPort(port);

  email.setTLS(processEngineConfiguration.getMailServerUseTLS());

  String user = processEngineConfiguration.getMailServerUsername();
  String password = processEngineConfiguration.getMailServerPassword();
  if (user != null && password != null) {
    email.setAuthentication(user, password);
  }
}
 
Example #17
Source Project: gsn   Author: LSIR   File: EmailService.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * This method cover most of the cases of sending a simple text email. If the email to be sent has to be configured
 * in a way which is not possible whith the parameters provided (such as html email, or email with attachement, ..),
 * please use the {@link #sendCustomEmail(org.apache.commons.mail.Email)} method and refer the API
 * {@see http://commons.apache.org/email/}.
 *
 * @param to      A set of destination email address of the email. Must contain at least one address.
 * @param object  The subject of the email.
 * @param message The msg of the email.
 * @return true if the email has been sent successfully, false otherwise.
 */
public static boolean sendEmail(ArrayList<String> to, String object, String message) {
    Email email = new SimpleEmail();
    try {
        email.setSubject(object);
        email.setMsg(message);
        if (to != null)
            for (String _to : to)
                email.addTo(_to);
        sendCustomEmail(email);
        return true;
    }
    catch (EmailException e) {
        logger.warn("Please, make sure that the SMTP server configuration is correct in the file: " + SMTP_FILE);
        logger.error(e.getMessage(), e);
        return false;
    }
}
 
Example #18
Source Project: camunda-bpm-platform   Author: camunda   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void addTo(Email email, String to) {
  String[] tos = splitAndTrim(to);
  if (tos != null) {
    for (String t : tos) {
      try {
        email.addTo(t);
      } catch (EmailException e) {
        throw LOG.addRecipientException(t, e);
      }
    }
  } else {
    throw LOG.missingRecipientsException();
  }
}
 
Example #19
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected Email createEmail(String text, String html, boolean attachmentsExist) {
  if (html != null) {
    return createHtmlEmail(text, html);
  } else if (text != null) {
    if (!attachmentsExist) {
      return createTextOnlyEmail(text);
    } else {
      return createMultiPartEmail(text);
    }
  } else {
    throw new ActivitiIllegalArgumentException("'html' or 'text' is required to be defined when using the mail activity");
  }
}
 
Example #20
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void addTo(Email email, String to) {
  String[] tos = splitAndTrim(to);
  if (tos != null) {
    for (String t : tos) {
      try {
        email.addTo(t);
      } catch (EmailException e) {
        throw new ActivitiException("Could not add " + t + " as recipient", e);
      }
    }
  } else {
    throw new ActivitiException("No recipient could be found for sending email");
  }
}
 
Example #21
Source Project: nifi   Author: apache   File: GenerateAttachment.java    License: Apache License 2.0 5 votes vote down vote up
public MimeMessage SimpleEmailMimeMessage() {
    Email email = new SimpleEmail();
    try {
        email.setFrom(from);
        email.addTo(to);
        email.setSubject(subject);
        email.setMsg(message);
        email.setHostName(hostName);
        email.buildMimeMessage();
    } catch (EmailException e) {
        e.printStackTrace();
    }

    return email.getMimeMessage();
}
 
Example #22
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void addCc(Email email, String cc) {
  String[] ccs = splitAndTrim(cc);
  if (ccs != null) {
    for (String c : ccs) {
      try {
        email.addCc(c);
      } catch (EmailException e) {
        throw new ActivitiException("Could not add " + c + " as cc recipient", e);
      }
    }
  }
}
 
Example #23
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void addBcc(Email email, String bcc) {
  String[] bccs = splitAndTrim(bcc);
  if (bccs != null) {
    for (String b : bccs) {
      try {
        email.addBcc(b);
      } catch (EmailException e) {
        throw new ActivitiException("Could not add " + b + " as bcc recipient", e);
      }
    }
  }
}
 
Example #24
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: 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 #25
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void setEmailSession(Email email, String mailSessionJndi) {
  try {
    email.setMailSessionFromJNDI(mailSessionJndi);
  } catch (NamingException e) {
    throw new ActivitiException("Could not send email: Incorrect JNDI configuration", e);
  }
}
 
Example #26
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected Email createEmail(String text, String html, boolean attachmentsExist) {
  if (html != null) {
    return createHtmlEmail(text, html);
  } else if (text != null) {
    if (!attachmentsExist) {
      return createTextOnlyEmail(text);
    } else {
      return createMultiPartEmail(text);
    }
  } else {
    throw new ActivitiIllegalArgumentException("'html' or 'text' is required to be defined when using the mail activity");
  }
}
 
Example #27
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void addTo(Email email, String to) {
  String[] tos = splitAndTrim(to);
  if (tos != null) {
    for (String t : tos) {
      try {
        email.addTo(t);
      } catch (EmailException e) {
        throw new ActivitiException("Could not add " + t + " as recipient", e);
      }
    }
  } else {
    throw new ActivitiException("No recipient could be found for sending email");
  }
}
 
Example #28
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void addCc(Email email, String cc) {
  String[] ccs = splitAndTrim(cc);
  if (ccs != null) {
    for (String c : ccs) {
      try {
        email.addCc(c);
      } catch (EmailException e) {
        throw new ActivitiException("Could not add " + c + " as cc recipient", e);
      }
    }
  }
}
 
Example #29
Source Project: camunda-bpm-platform   Author: camunda   File: MailActivityBehavior.java    License: Apache License 2.0 5 votes vote down vote up
protected void setFrom(Email email, String from) {
  String fromAddress = null;

  if (from != null) {
    fromAddress = from;
  } else { // use default configured from address in process engine config
    fromAddress = Context.getProcessEngineConfiguration().getMailServerDefaultFrom();
  }

  try {
    email.setFrom(fromAddress);
  } catch (EmailException e) {
    throw LOG.addSenderException(from, e);
  }
}
 
Example #30
Source Project: activiti6-boot2   Author: dingziyang   File: MailActivityBehavior.java    License: 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);
    }
  }
}