List of usage examples for org.apache.commons.mail MultiPartEmail setTo
public Email setTo(final Collection<InternetAddress> aCollection) throws EmailException
From source file:org.kantega.respiro.mail.ServerConfig.java
public MultiPartEmail newMail() { MultiPartEmail mail = new MultiPartEmail(); mail.setHostName(host);/* w w w . j ava 2s . c o m*/ if (!ssl) mail.setSmtpPort(port); else mail.setSslSmtpPort(valueOf(port)); if (username != null && password != null) mail.setAuthentication(username, password); try { if (!to.isEmpty()) mail.setTo(to); if (!cc.isEmpty()) mail.setCc(cc); if (!bcc.isEmpty()) mail.setBcc(bcc); if (fromMail != null) mail.setFrom(fromMail); } catch (EmailException e) { throw new RuntimeException(e); } return mail; }
From source file:org.kitodo.selenium.testframework.helper.MailSender.java
/** * Sends an email.//from w ww. j a v a 2s .c o m * * @param user * The user name for login in to email account. * @param password * The password for login in to email account. * @param subject * The email subject. * @param message * The email message. * @param attachedFile * The attached file. * @param recipient * The recipient email address. */ public static void sendEmail(String user, String password, String subject, String message, File attachedFile, String recipient) throws EmailException, AddressException { if (user != null && password != null && recipient != null) { InternetAddress address = new InternetAddress(recipient); ArrayList<InternetAddress> addressList = new ArrayList<>(); addressList.add(address); EmailAttachment attachment = new EmailAttachment(); if (attachedFile != null) { // Create the attachment attachment.setPath(attachedFile.getAbsolutePath()); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("SeleniumScreenShot"); attachment.setName("screenshot.png"); } MultiPartEmail email = new MultiPartEmail(); email.setHostName("smtp.gmail.com"); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator(user, password)); email.setSSLOnConnect(true); email.setFrom("Travis CI Screenshot <kitodo.dev@gmail.com>"); if (subject != null) { email.setSubject(subject); } if (message != null) { email.setMsg(message); } email.setTo(addressList); email.attach(attachment); email.send(); } else { logger.error("Email was not send due to missing environmental variables"); } }
From source file:org.sakaiproject.nakamura.email.outgoing.LiteOutgoingEmailMessageListener.java
protected MultiPartEmail constructMessage(Content contentNode, List<String> recipients, javax.jcr.Session session, org.sakaiproject.nakamura.api.lite.Session sparseSession) throws EmailDeliveryException, StorageClientException, AccessDeniedException, PathNotFoundException, RepositoryException, EmailException { MultiPartEmail email = new MultiPartEmail(); Set<String> toRecipients = new HashSet<String>(); toRecipients = setRecipients(recipients, sparseSession); String to = null;/*from w w w . j ava2 s . co m*/ try { // set from: to the reply as address email.setFrom(replyAsAddress, replyAsName); if (toRecipients.size() == 1) { // set to: to the rcpt if sending to just one person to = convertToEmail(toRecipients.iterator().next(), sparseSession); email.setTo(Lists.newArrayList(new InternetAddress(to))); } else { // set to: to 'undisclosed recipients' when sending to a group of recipients // this mirrors what shows up in RFC's and most major MTAs // http://www.postfix.org/postconf.5.html#undisclosed_recipients_header email.addHeader("To", "undisclosed-recipients:;"); } } catch (EmailException e) { LOGGER.error("Cannot send email. From: address as configured is not valid: {}", replyAsAddress); } catch (AddressException e) { LOGGER.error("Cannot send email. To: address is not valid: {}", to); } // if we're dealing with a group of recipients, add them to bcc: to hide email // addresses from the other recipients if (toRecipients.size() > 1) { for (String r : toRecipients) { try { // we don't need to copy the sender on the message if (r.equals(contentNode.getProperty(MessageConstants.PROP_SAKAI_FROM))) { continue; } email.addBcc(convertToEmail(r, sparseSession)); } catch (EmailException e) { throw new EmailDeliveryException( "Invalid To Address [" + r + "], message is being dropped :" + e.getMessage(), e); } } } if (contentNode.hasProperty(MessageConstants.PROP_SAKAI_BODY)) { String messageBody = String.valueOf(contentNode.getProperty(MessageConstants.PROP_SAKAI_BODY)); // if this message has a template, use it LOGGER.debug( "Checking for sakai:templatePath and sakai:templateParams properties on the outgoing message's node."); if (contentNode.hasProperty(MessageConstants.PROP_TEMPLATE_PATH) && contentNode.hasProperty(MessageConstants.PROP_TEMPLATE_PARAMS)) { Map<String, String> parameters = getTemplateProperties( (String) contentNode.getProperty(MessageConstants.PROP_TEMPLATE_PARAMS)); String templatePath = (String) contentNode.getProperty(MessageConstants.PROP_TEMPLATE_PATH); LOGGER.debug("Got the path '{0}' to the template for this outgoing message.", templatePath); Node templateNode = session.getNode(templatePath); if (templateNode.hasProperty("sakai:template")) { String template = templateNode.getProperty("sakai:template").getString(); LOGGER.debug("Pulled the template body from the template node: {0}", template); messageBody = templateService.evaluateTemplate(parameters, template); LOGGER.debug("Performed parameter substitution in the template: {0}", messageBody); } } else { LOGGER.debug( "Message node '{0}' does not have sakai:templatePath and sakai:templateParams properties", contentNode.getPath()); } try { email.setMsg(messageBody); } catch (EmailException e) { throw new EmailDeliveryException( "Invalid Message Body, message is being dropped :" + e.getMessage(), e); } } if (contentNode.hasProperty(MessageConstants.PROP_SAKAI_SUBJECT)) { email.setSubject((String) contentNode.getProperty(MessageConstants.PROP_SAKAI_SUBJECT)); } ContentManager contentManager = sparseSession.getContentManager(); for (String streamId : contentNode.listStreams()) { String description = null; if (contentNode.hasProperty( StorageClientUtils.getAltField(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION, streamId))) { description = (String) contentNode.getProperty(StorageClientUtils .getAltField(MessageConstants.PROP_SAKAI_ATTACHMENT_DESCRIPTION, streamId)); } LiteEmailDataSource ds = new LiteEmailDataSource(contentManager, contentNode, streamId); try { email.attach(ds, streamId, description); } catch (EmailException e) { throw new EmailDeliveryException( "Invalid Attachment [" + streamId + "] message is being dropped :" + e.getMessage(), e); } } return email; }