List of usage examples for org.apache.commons.mail HtmlEmail addBcc
public Email addBcc(final String email, final String name) throws EmailException
From source file:com.zxy.commons.email.MailMessageUtils.java
/** * smtp??//from w w w . j a v a2 s . 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:controllers.ProjectApp.java
private static void sendTransferRequestMail(ProjectTransfer pt) { HtmlEmail email = new HtmlEmail(); try {/* w w w . j ava2 s.c o m*/ String acceptUrl = pt.getAcceptUrl(); String message = Messages.get("transfer.message.hello", pt.destination) + "\n\n" + Messages.get("transfer.message.detail", pt.project.name, pt.newProjectName, pt.project.owner, pt.destination) + "\n" + Messages.get("transfer.message.link") + "\n\n" + acceptUrl + "\n\n" + Messages.get("transfer.message.deadline") + "\n\n" + Messages.get("transfer.message.thank"); email.setFrom(Config.getEmailFromSmtp(), pt.sender.name); email.addTo(Config.getEmailFromSmtp(), "Yobi"); User to = User.findByLoginId(pt.destination); if (!to.isAnonymous()) { email.addBcc(to.email, to.name); } Organization org = Organization.findByName(pt.destination); if (org != null) { List<OrganizationUser> admins = OrganizationUser.findAdminsOf(org); for (OrganizationUser admin : admins) { email.addBcc(admin.user.email, admin.user.name); } } email.setSubject( String.format("[%s] @%s wants to transfer project", pt.project.name, pt.sender.loginId)); email.setHtmlMsg(Markdown.render(message)); email.setTextMsg(message); email.setCharset("utf-8"); email.addHeader("References", "<" + acceptUrl + "@" + Config.getHostname() + ">"); email.setSentDate(pt.requested); Mailer.send(email); String escapedTitle = email.getSubject().replace("\"", "\\\""); String logEntry = String.format("\"%s\" %s", escapedTitle, email.getBccAddresses()); play.Logger.of("mail").info(logEntry); } catch (Exception e) { Logger.warn("Failed to send a notification: " + email + "\n" + ExceptionUtils.getStackTrace(e)); } }
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 {// w w w . ja v a 2 s. co 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 {/*w ww .ja v a 2s.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:gov.osti.services.Metadata.java
/** * Send a POC email notification on SUBMISSION/APPROVAL of DOE CODE records. * * @param md the METADATA to send notification for *///from w ww . j a v a 2 s . c om private static void sendPOCNotification(DOECodeMetadata md) { // if HOST or MD or PROJECT MANAGER NAME isn't set, cannot send if (StringUtils.isEmpty(EMAIL_HOST) || null == md || StringUtils.isEmpty(PM_NAME)) return; Long codeId = md.getCodeId(); String siteCode = md.getSiteOwnershipCode(); Status workflowStatus = md.getWorkflowStatus(); // if SITE OWNERSHIP isn't set, cannot send if (StringUtils.isEmpty(siteCode)) return; // only applicable to APPROVED records if (!Status.Approved.equals(workflowStatus)) return; // get the SITE information Site site = SiteServices.findSiteBySiteCode(siteCode); if (null == site) { log.warn("Unable to locate SITE information for SITE CODE: " + siteCode); return; } // lookup previous Snapshot status info for item EntityManager em = DoeServletContextListener.createEntityManager(); TypedQuery<MetadataSnapshot> querySnapshot = em .createNamedQuery("MetadataSnapshot.findByCodeIdLastNotStatus", MetadataSnapshot.class) .setParameter("status", DOECodeMetadata.Status.Approved).setParameter("codeId", codeId); String lastApprovalFor = "submitted/announced"; List<MetadataSnapshot> results = querySnapshot.setMaxResults(1).getResultList(); for (MetadataSnapshot ms : results) { lastApprovalFor = ms.getSnapshotKey().getSnapshotStatus().toString().toLowerCase(); } List<String> emails = site.getPocEmails(); // if POC is setup if (emails != null && !emails.isEmpty()) { try { HtmlEmail email = new HtmlEmail(); email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8); email.setHostName(EMAIL_HOST); String lab = site.getLab(); lab = lab.isEmpty() ? siteCode : lab; String softwareTitle = md.getSoftwareTitle().replaceAll("^\\h+|\\h+$", ""); email.setFrom(EMAIL_FROM); email.setSubject("POC Notification -- " + workflowStatus + " -- DOE CODE ID: " + codeId + ", " + softwareTitle); for (String pocEmail : emails) email.addTo(pocEmail); // if email is provided, BCC the Project Manager if (!StringUtils.isEmpty(PM_EMAIL)) email.addBcc(PM_EMAIL, PM_NAME); StringBuilder msg = new StringBuilder(); msg.append("<html>"); msg.append("Dear Sir or Madam:"); String biblioLink = SITE_URL + "/biblio/" + codeId; msg.append("<p>As a point of contact for ").append(lab) .append(", we wanted to inform you that a software project, titled ").append(softwareTitle) .append(", associated with your organization was ").append(lastApprovalFor) .append(" to DOE CODE and assigned DOE CODE ID: ").append(codeId) .append(". This project record is discoverable in <a href=\"").append(SITE_URL) .append("\">DOE CODE</a>, e.g. searching by the project title or DOE CODE ID #, and can be found here: <a href=\"") .append(biblioLink).append("\">").append(biblioLink).append("</a></p>"); msg.append( "<p>If you have any questions, please do not hesitate to <a href=\"mailto:doecode@osti.gov\">Contact Us</a>.</p>"); msg.append("<p>Sincerely,</p>"); msg.append("<p>").append(PM_NAME).append("<br/>Product Manager for DOE CODE<br/>USDOE/OSTI</p>"); msg.append("</html>"); email.setHtmlMsg(msg.toString()); email.send(); } catch (EmailException e) { log.error("Unable to send POC notification to " + Arrays.toString(emails.toArray()) + " for #" + md.getCodeId()); log.error("Message: " + e.getMessage()); } } }
From source file:gov.osti.services.Metadata.java
/** * Send an email notification on APPROVAL of DOE CODE records. * * @param md the METADATA to send notification for */// ww w . jav a 2 s . c om private static void sendApprovalNotification(DOECodeMetadata md) { HtmlEmail email = new HtmlEmail(); email.setCharset(org.apache.commons.mail.EmailConstants.UTF_8); email.setHostName(EMAIL_HOST); // if HOST or record OWNER or PROJECT MANAGER NAME isn't set, cannot send if (StringUtils.isEmpty(EMAIL_HOST) || null == md || StringUtils.isEmpty(md.getOwner()) || StringUtils.isEmpty(PM_NAME)) return; // only has meaning for APPROVED records if (!Status.Approved.equals(md.getWorkflowStatus())) return; try { // get the OWNER information User owner = UserServices.findUserByEmail(md.getOwner()); if (null == owner) { log.warn("Unable to locate USER information for Code ID: " + md.getCodeId()); return; } Long codeId = md.getCodeId(); // lookup previous Snapshot status info for item EntityManager em = DoeServletContextListener.createEntityManager(); TypedQuery<MetadataSnapshot> querySnapshot = em .createNamedQuery("MetadataSnapshot.findByCodeIdLastNotStatus", MetadataSnapshot.class) .setParameter("status", DOECodeMetadata.Status.Approved).setParameter("codeId", codeId); String lastApprovalFor = "submitted/announced"; List<MetadataSnapshot> results = querySnapshot.setMaxResults(1).getResultList(); for (MetadataSnapshot ms : results) { lastApprovalFor = ms.getSnapshotKey().getSnapshotStatus().toString().toLowerCase(); } String softwareTitle = md.getSoftwareTitle().replaceAll("^\\h+|\\h+$", ""); email.setFrom(EMAIL_FROM); email.setSubject("Approved -- DOE CODE ID: " + codeId + ", " + softwareTitle); email.addTo(md.getOwner()); // if email is provided, BCC the Project Manager if (!StringUtils.isEmpty(PM_EMAIL)) email.addBcc(PM_EMAIL, PM_NAME); StringBuilder msg = new StringBuilder(); msg.append("<html>"); msg.append("Dear ").append(owner.getFirstName()).append(" ").append(owner.getLastName()).append(":"); msg.append("<P>Thank you -- your ").append(lastApprovalFor).append(" project, DOE CODE ID: <a href=\"") .append(SITE_URL).append("/biblio/").append(codeId).append("\">").append(codeId) .append("</a>, has been approved. It is now <a href=\"").append(SITE_URL) .append("\">searchable</a> in DOE CODE by, for example, title or CODE ID #.</P>"); // OMIT the following for BUSINESS TYPE software, or last ANNOUNCED software if (!DOECodeMetadata.Type.B.equals(md.getSoftwareType()) && !lastApprovalFor.equalsIgnoreCase("announced")) { msg.append( "<P>You may need to continue editing your project to announce it to the Department of Energy ") .append("to ensure announcement and dissemination in accordance with DOE statutory responsibilities. For more information please see ") .append("<a href=\"").append(SITE_URL) .append("/faq#what-does-it-mean-to-announce\">What does it mean to announce scientific code to DOE CODE?</a></P>"); } msg.append( "<P>If you have questions such as What are the benefits of getting a DOI for code or software?, see the ") .append("<a href=\"").append(SITE_URL).append("/faq\">DOE CODE FAQs</a>.</P>"); msg.append( "<P>If we can be of assistance, please do not hesitate to <a href=\"mailto:doecode@osti.gov\">Contact Us</a>.</P>"); msg.append("<P>Sincerely,</P>"); msg.append("<P>").append(PM_NAME).append("<BR/>Product Manager for DOE CODE<BR/>USDOE/OSTI</P>"); msg.append("</html>"); email.setHtmlMsg(msg.toString()); email.send(); } catch (EmailException e) { log.error("Unable to send APPROVAL notification for #" + md.getCodeId()); log.error("Message: " + e.getMessage()); } }