List of usage examples for org.apache.commons.mail HtmlEmail addCc
public Email addCc(final String email, final String name) throws EmailException
From source file:com.zxy.commons.email.MailMessageUtils.java
/** * smtp??// w w w.j a va 2s . co m * * @param subject subject * @param htmlBody htmlBody * @param properties properties * @param from from * @param toList toList * @param ccList ccList * @param bccList bccList * @param embedUrls * @throws EmailException EmailException */ @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseStringBufferForStringAppends" }) public static void sendMail(String subject, String htmlBody, Map<String, String> properties, String from, List<String> toList, List<String> ccList, List<String> bccList, Map<String, URL> embedUrls) throws EmailException { HtmlEmail htmlEmail = getEmail(); // from? if (!Strings.isNullOrEmpty(from)) { Address fromMailbox = parseMailbox(from); if (fromMailbox != null && StringUtils.isNotBlank(from)) { htmlEmail.setFrom(fromMailbox.getAddress(), fromMailbox.getName()); } } // to? if (toList != null && !toList.isEmpty()) { for (String to : toList) { if (StringUtils.isNotBlank(to)) { Address toMailbox = parseMailbox(to); htmlEmail.addTo(toMailbox.getAddress(), toMailbox.getName()); } } } // cc? if (ccList != null && !ccList.isEmpty()) { for (String cc : ccList) { if (StringUtils.isNotBlank(cc)) { Address ccMailbox = parseMailbox(cc); htmlEmail.addCc(ccMailbox.getAddress(), ccMailbox.getName()); } } } // bcc? if (bccList != null && !bccList.isEmpty()) { for (String bcc : bccList) { if (StringUtils.isNotBlank(bcc)) { Address bccMailbox = parseMailbox(bcc); htmlEmail.addBcc(bccMailbox.getAddress(), bccMailbox.getName()); } } } // htmlEmail.setSubject(subject); htmlEmail.setHtmlMsg(htmlBody); htmlEmail.setSentDate(new Date()); // if (properties != null) { htmlEmail.setHeaders(properties); } // if (embedUrls != null && !embedUrls.isEmpty()) { for (Map.Entry<String, URL> entry : embedUrls.entrySet()) { String cid = entry.getKey(); URL url = entry.getValue(); String fileName = StringUtils.substringAfterLast(url.getPath(), "/"); if (StringUtils.isBlank(fileName)) { fileName = cid; } else { fileName += IdUtils.genStringId(); } htmlEmail.embed(new URLDataSource(url), fileName, cid); } } htmlEmail.send(); }
From source file:com.esofthead.mycollab.module.mail.DefaultMailer.java
private HtmlEmail getBasicEmail(String fromEmail, String fromName, List<MailRecipientField> toEmail, List<MailRecipientField> ccEmail, List<MailRecipientField> bccEmail, String subject, String html) { try {//from w w w .j av a 2 s. c o m HtmlEmail email = new HtmlEmail(); email.setHostName(host); email.setFrom(fromEmail, fromName); email.setCharset(EmailConstants.UTF_8); for (int i = 0; i < toEmail.size(); i++) { if (isValidate(toEmail.get(i).getEmail()) && isValidate(toEmail.get(i).getName())) { email.addTo(toEmail.get(i).getEmail(), toEmail.get(i).getName()); } else { LOG.error("Invalid to email input: " + toEmail.get(i).getEmail() + "---" + toEmail.get(i).getName()); } } if (CollectionUtils.isNotEmpty(ccEmail)) { for (int i = 0; i < ccEmail.size(); i++) { if (isValidate(ccEmail.get(i).getEmail()) && isValidate(ccEmail.get(i).getName())) { email.addCc(ccEmail.get(i).getEmail(), ccEmail.get(i).getName()); } else { LOG.error("Invalid cc email input: " + ccEmail.get(i).getEmail() + "---" + ccEmail.get(i).getName()); } } } if (CollectionUtils.isNotEmpty(bccEmail)) { for (int i = 0; i < bccEmail.size(); i++) { if (isValidate(bccEmail.get(i).getEmail()) && isValidate(bccEmail.get(i).getName())) { email.addBcc(bccEmail.get(i).getEmail(), bccEmail.get(i).getName()); } else { LOG.error("Invalid bcc email input: " + bccEmail.get(i).getEmail() + "---" + bccEmail.get(i).getName()); } } } if (username != null) { email.setAuthentication(username, password); } email.setStartTLSEnabled(isTLS); email.setSubject(subject); if (StringUtils.isNotBlank(html)) { email.setHtmlMsg(html); } return email; } catch (EmailException e) { throw new MyCollabException(e); } }
From source file:com.mycollab.module.mail.DefaultMailer.java
private HtmlEmail getBasicEmail(String fromEmail, String fromName, List<MailRecipientField> toEmail, List<MailRecipientField> ccEmail, List<MailRecipientField> bccEmail, String subject, String html) { try {//from w w w . j a va 2 s . c o m HtmlEmail email = new HtmlEmail(); email.setHostName(emailConf.getHost()); email.setSmtpPort(emailConf.getPort()); email.setStartTLSEnabled(emailConf.getIsStartTls()); email.setSSLOnConnect(emailConf.getIsSsl()); email.setFrom(fromEmail, fromName); email.setCharset(EmailConstants.UTF_8); for (MailRecipientField aToEmail : toEmail) { if (isValidate(aToEmail.getEmail()) && isValidate(aToEmail.getName())) { email.addTo(aToEmail.getEmail(), aToEmail.getName()); } else { LOG.error(String.format("Invalid to email input: %s---%s", aToEmail.getEmail(), aToEmail.getName())); } } if (CollectionUtils.isNotEmpty(ccEmail)) { for (MailRecipientField aCcEmail : ccEmail) { if (isValidate(aCcEmail.getEmail()) && isValidate(aCcEmail.getName())) { email.addCc(aCcEmail.getEmail(), aCcEmail.getName()); } else { LOG.error(String.format("Invalid cc email input: %s---%s", aCcEmail.getEmail(), aCcEmail.getName())); } } } if (CollectionUtils.isNotEmpty(bccEmail)) { for (MailRecipientField aBccEmail : bccEmail) { if (isValidate(aBccEmail.getEmail()) && isValidate(aBccEmail.getName())) { email.addBcc(aBccEmail.getEmail(), aBccEmail.getName()); } else { LOG.error(String.format("Invalid bcc email input: %s---%s", aBccEmail.getEmail(), aBccEmail.getName())); } } } if (emailConf.getUser() != null) { email.setAuthentication(emailConf.getUser(), emailConf.getPassword()); } email.setSubject(subject); if (StringUtils.isNotBlank(html)) { email.setHtmlMsg(html); } return email; } catch (EmailException e) { throw new MyCollabException(e); } }
From source file:de.jaide.courier.email.MessageHandlerEMail.java
public void handleMessage(Map<String, Object> parameters) throws CourierException { /*/*from w w w .j av a 2s .co m*/ * Check if the obligatory parameters are there. */ for (String key : obligatoryMappingParameters) if (!parameters.containsKey(key)) throw new CourierException(new MissingParameterException( "The parameter '" + key + "' was expected but couldn't be found.")); /* * Now retrieve the obligatory parameters. */ String configurationName = (String) parameters.get(MAPPING_PARAM_CONFIGURATION_NAME); String templatePath = (String) parameters.get(MAPPING_PARAM_TEMPLATE_PATH); if ((templatePath != null) && (!templatePath.endsWith("/"))) templatePath += "/"; else if (templatePath == null) templatePath = "/"; Class<?> templatePathClass = (Class<?>) parameters.get(MAPPING_PARAM_TEMPLATE_PATH_CLASS); File templatePathFile = (File) parameters.get(MAPPING_PARAM_TEMPLATE_PATH_FILE); String templateName = (String) parameters.get(MAPPING_PARAM_TEMPLATE_NAME); TemplateTypeEnum templateTypeEnum = (TemplateTypeEnum) parameters.get(MAPPING_PARAM_TEMPLATE_TYPE); String recipientFirstname = (String) parameters.get(MAPPING_PARAM_RECIPIENT_FIRSTNAME); String recipientLastname = (String) parameters.get(MAPPING_PARAM_RECIPIENT_LASTNAME); String recipientEMail = (String) parameters.get(MAPPING_PARAM_RECIPIENT_EMAIL); String ccRecipientFirstname = (String) parameters.get(MAPPING_PARAM_CC_RECIPIENT_FIRSTNAME); String ccRecipientLastname = (String) parameters.get(MAPPING_PARAM_CC_RECIPIENT_LASTNAME); String ccRecipientEMail = (String) parameters.get(MAPPING_PARAM_CC_RECIPIENT_EMAIL); /* * The next three parameters are optional, as they might also be specified in the SMTP configuration file. If they are specified they * tell us to overwrite what was specified in the SMTP configuration file and use those values (firstname, lastname, e-mail) for the * sender instead. */ String senderFirstname = null; if (parameters.containsKey(MAPPING_PARAM_SENDER_FIRSTNAME)) senderFirstname = (String) parameters.get(MAPPING_PARAM_SENDER_FIRSTNAME); String senderLastname = null; if (parameters.containsKey(MAPPING_PARAM_SENDER_LASTNAME)) senderLastname = (String) parameters.get(MAPPING_PARAM_SENDER_LASTNAME); String senderEMail = null; if (parameters.containsKey(MAPPING_PARAM_SENDER_EMAIL)) senderEMail = (String) parameters.get(MAPPING_PARAM_SENDER_EMAIL); /* * Will be used for parsing the templates. */ StringWriter writer = new StringWriter(); try { /* * Construct the template that we're about to process. First set the path to load the template(s) from. In case a Directory was given * as the base for template loading purposes use that instead. */ if (templatePathFile == null) { if (templatePathClass == null) templatingConfiguration.setClassForTemplateLoading(getClass(), templatePath); else templatingConfiguration.setClassForTemplateLoading(templatePathClass, templatePath); } else templatingConfiguration.setDirectoryForTemplateLoading(templatePathFile); /* * Get the headers and Freemarker-parse them. If there are no headers then ignore the errors. The file has to be one header per line, * header name and value separated by a colon (":"). */ Template template; Map<String, String> headers = new HashMap<String, String>(); String headersFilename = retrieveTemplateFilename(templateName, MessageHandlerEMail.TEMPLATENAME_SUFFIX_HEADERS, true); if (headersFilename != null) { try { template = loadTemplate(headersFilename); template.process(parameters, writer); BufferedReader reader = new BufferedReader(new StringReader(writer.toString())); headers = new HashMap<String, String>(); String str = ""; while ((str = reader.readLine()) != null) { String[] split = str.split(":"); if (split.length > 1) headers.put(split[0].trim(), split[1].trim()); } } catch (IOException ioe) { // The header file is optional, hence we don't care if it couldn't be found } } /* * Get the subject line and Freemarker-parse it. */ String subject = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_SUBJECT, templateName, false); /* * Get the body content and Freemarker-parse it. */ String contentText = null; String contentHtml = null; /* * Load all requested versions of the template. */ if (templateTypeEnum == TemplateTypeEnum.TEXT) { contentText = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_BODY, templateName, false); } else if (templateTypeEnum == TemplateTypeEnum.HTML) { contentHtml = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_BODY, templateName, true); } else if (templateTypeEnum == TemplateTypeEnum.BOTH) { contentText = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_BODY, templateName, false); contentHtml = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_BODY, templateName, true); } else if (templateTypeEnum == TemplateTypeEnum.ANY) { try { contentText = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_BODY, templateName, false); } catch (IOException ioe) { } finally { try { contentHtml = loadAndProcessTemplate(parameters, MessageHandlerEMail.TEMPLATENAME_SUFFIX_BODY, templateName, true); } catch (IOException ioe) { throw new RuntimeException( "Neither the HTML nor the TEXT-only version of the e-mail template '" + templateName + "' could be found. Are you sure they reside in '" + templatePath + "' as '" + templateName + "_body.ftl.html' or '" + templateName + "_body.ftl.txt'?", ioe); } } } /* * Set the parameters that are identical for that sender, for all recipients. * Note: attachments may not be removed once they have been attached, hence the performance-improving caching had to be removed. */ SmtpConfiguration smtpConfiguration = (SmtpConfiguration) smtpConfigurations.get(configurationName); HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setCharset("UTF-8"); htmlEmail.setHostName(smtpConfiguration.getSmtpHostname()); htmlEmail.setSmtpPort(smtpConfiguration.getSmtpPort()); if (smtpConfiguration.isTls()) { htmlEmail.setAuthenticator( new DefaultAuthenticator(smtpConfiguration.getUsername(), smtpConfiguration.getPassword())); htmlEmail.setStartTLSEnabled(smtpConfiguration.isTls()); } htmlEmail.setSSLOnConnect(smtpConfiguration.isSsl()); /* * Changing the sender, to differ from what was specified in the particular SMTP configuration, is optional. As explained above this * will only happen if they were specified by the caller. */ if ((senderFirstname != null) || (senderLastname != null) || (senderEMail != null)) htmlEmail.setFrom(senderEMail, senderFirstname + " " + senderLastname); else htmlEmail.setFrom(smtpConfiguration.getFromEMail(), smtpConfiguration.getFromSenderName()); /* * Set the parameters that differ for each recipient. */ htmlEmail.addTo(recipientEMail, recipientFirstname + " " + recipientLastname); if ((ccRecipientFirstname != null) && (ccRecipientLastname != null) && (ccRecipientEMail != null)) htmlEmail.addCc(ccRecipientEMail, ccRecipientFirstname + " " + ccRecipientLastname); htmlEmail.setHeaders(headers); htmlEmail.setSubject(subject); /* * Set the HTML and Text version of the e-mail body. */ if (contentHtml != null) htmlEmail.setHtmlMsg(contentHtml); if (contentText != null) htmlEmail.setTextMsg(contentText); /* * Add attachments, if available. */ if (parameters.containsKey(MAPPING_PARAM_ATTACHMENTS)) { @SuppressWarnings("unchecked") List<EmailAttachment> attachments = (List<EmailAttachment>) parameters .get(MAPPING_PARAM_ATTACHMENTS); for (EmailAttachment attachment : attachments) { htmlEmail.attach(attachment); } } /* * Finished - now send the e-mail. */ htmlEmail.send(); } catch (IOException ioe) { throw new CourierException(ioe); } catch (TemplateException te) { throw new CourierException(te); } catch (EmailException ee) { throw new CourierException(ee); } finally { if (writer != null) { writer.flush(); try { writer.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } } }
From source file:com.perceptive.epm.perkolcentral.bl.EmployeeBL.java
private void sendMailToPerceptiveOpsTeam(EmployeeBO employeeBO) throws Exception { try {// w w w. j ava 2s . c o m //Send the mail HtmlEmail emailToSend = new HtmlEmail(); emailToSend.setHostName(email.getHostName()); String messageTemplateAdded = "<html>\n" + "<head>\n" + "</head>\n" + "\n" + "<body style=\"font:Georgia; font-size:12px;\">\n" + "<p>Dear All,</p>\n" + "<blockquote>\n" + " <p>A new employee has joined Perceptive Kolkata.</p>\n" + "</blockquote>\n" + "<ul>\n" + " <li><strong><em>User Name</em></strong>: <strong>%s</strong></li>\n" + " <li><em><strong>User Short Id</strong></em>: <strong>%s</strong></li>\n" + " <li><em><strong>Employee Id</strong></em>: <strong>%s</strong></li>\n" + " <li><strong><em>User Email-Id</em></strong>:<strong> %s</strong></li>\n" + " <li><em><strong>Mobile Number</strong></em>:<strong> %s</strong></li>\n" + " <li><em><strong>Job Title</strong></em> : <strong>%s</strong></li>\n" + "</ul>\n" + "<p>Please take following actions.</p>\n" + "<ul>\n" + " <li>Access to Rally.</li>\n" + " <li>Access to Salesforce.</li>\n" + " <li>Access to Confluence.</li>\n" + " <li>Access to Perceptive AD.</li>\n" + " <li>Access to TFS.</li>\n" + "</ul>\n" + "<p>Thanks,</p>\n" + "<blockquote>\n" + " <p>Perceptive Kolkata Central( http://10.195.17.14/PerceptiveKolkataCentral )</p>\n" + "</blockquote>\n" + "</body>\n" + "</html>"; String messageToSend = String.format(messageTemplateAdded, employeeBO.getEmployeeName(), employeeBO.getEmployeeUid(), employeeBO.getEmployeeId().toString(), employeeBO.getEmail(), employeeBO.getMobileNumber(), employeeBO.getJobTitle()); emailToSend.setHtmlMsg(messageToSend); //emailToSend.setTextMsg(StringEscapeUtils.escapeHtml(messageToSend)); emailToSend.getToAddresses().clear(); emailToSend.addTo("per.special.rad.operations@perceptivesoftware.com", "RAD - Operations"); emailToSend.addTo("radops@perceptivesoftware.com", "RAD - Operations Team"); Collection<EmployeeBO> allEmployeesNeedToGetMail = getAllEmployeesKeyedByGroupId() .get(Integer.valueOf("14")); for (EmployeeBO item : allEmployeesNeedToGetMail) { emailToSend.addCc(item.getEmail(), item.getEmployeeName()); } //emailToSend.setFrom("PerceptiveKolkataCentral@perceptivesoftware.com", "Perceptive Kolkata Central"); emailToSend.setFrom("EnterpriseSoftwareKolkata@lexmark.com", "Enterprise Software Kolkata"); emailToSend.setSubject( String.format("New employee joined @ Lexmark Kolkata : %s", employeeBO.getEmployeeName())); emailToSend.send(); } catch (Exception ex) { throw new ExceptionWrapper(ex); } }
From source file:org.cerberus.service.email.impl.sendMail.java
public static void sendHtmlMail(String host, int port, String body, String subject, String from, String to, String cc) throws Exception { HtmlEmail email = new HtmlEmail(); email.setSmtpPort(port);// ww w. j ava 2s . c o m email.setDebug(false); email.setHostName(host); email.setFrom(from); email.setSubject(subject); email.setHtmlMsg(body); String[] destinataire = to.split(";"); for (int i = 0; i < destinataire.length; i++) { String name; String emailaddress; if (destinataire[i].contains("<")) { String[] destinatairedata = destinataire[i].split("<"); name = destinatairedata[0].trim(); emailaddress = destinatairedata[1].replace(">", "").trim(); } else { name = ""; emailaddress = destinataire[i]; } email.addTo(emailaddress, name); } String[] copy = cc.split(";"); for (int i = 0; i < copy.length; i++) { String namecc; String emailaddresscc; if (copy[i].contains("<")) { String[] copydata = copy[i].split("<"); namecc = copydata[0].trim(); emailaddresscc = copydata[1].replace(">", "").trim(); } else { namecc = ""; emailaddresscc = copy[i]; } email.addCc(emailaddresscc, namecc); } email.setTLS(true); email.send(); }