List of usage examples for org.apache.commons.mail HtmlEmail send
public String send() throws EmailException
From source file:org.apache.unomi.plugins.mail.actions.SendMailAction.java
public int execute(Action action, Event event) { String from = (String) action.getParameterValues().get("from"); String to = (String) action.getParameterValues().get("to"); String cc = (String) action.getParameterValues().get("cc"); String bcc = (String) action.getParameterValues().get("bcc"); String subject = (String) action.getParameterValues().get("subject"); String template = (String) action.getParameterValues().get("template"); ST stringTemplate = new ST(template); stringTemplate.add("profile", event.getProfile()); stringTemplate.add("event", event); // load your HTML email template String htmlEmailTemplate = stringTemplate.render(); // define you base URL to resolve relative resource locations try {/* w w w.j a v a 2s . co m*/ new URL("http://www.apache.org"); } catch (MalformedURLException e) { // } // create the email message HtmlEmail email = new ImageHtmlEmail(); // email.setDataSourceResolver(new DataSourceResolverImpl(url)); email.setHostName(mailServerHostName); email.setSmtpPort(mailServerPort); email.setAuthenticator(new DefaultAuthenticator(mailServerUsername, mailServerPassword)); email.setSSLOnConnect(mailServerSSLOnConnect); try { email.addTo(to); email.setFrom(from); if (cc != null && cc.length() > 0) { email.addCc(cc); } if (bcc != null && bcc.length() > 0) { email.addBcc(bcc); } email.setSubject(subject); // set the html message email.setHtmlMsg(htmlEmailTemplate); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send(); } catch (EmailException e) { logger.error("Cannot send mail", e); } return EventService.NO_CHANGE; }
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);/*w w w.j a v a 2 s . 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(); }
From source file:org.chenillekit.mail.services.impl.MailServiceImpl.java
/** * send a HTML message.// www . j av a2s . co m * * @param headers the mail headers * @param htmlBody the mail body (HTML based) * @param dataSources array of data sources to attach at this mail * * @return true if mail successfull send */ public boolean sendHtmlMail(MailMessageHeaders headers, String htmlBody, DataSource... dataSources) { try { HtmlEmail email = new HtmlEmail(); setEmailStandardData(email); setMailMessageHeaders(email, headers); if (dataSources != null) { for (DataSource dataSource : dataSources) email.attach(dataSource, dataSource.getName(), dataSource.getName()); } email.setCharset(headers.getCharset()); email.setHtmlMsg(htmlBody); String msgId = email.send(); return true; } catch (EmailException e) { // FIXME Handle gracefully throw new RuntimeException(e); } }
From source file:org.gravidence.gravifon.email.ApacheCommonsEmailSender.java
@Override public boolean send(String toAddress, String toName, String subject, String htmlMessage, String textMessage) { HtmlEmail email = new HtmlEmail(); email.setHostName(host);//from ww w . j a v a 2s .c om email.setSslSmtpPort(port); email.setAuthenticator(new DefaultAuthenticator(username, password)); email.setSSLOnConnect(true); try { if (StringUtils.isBlank(toName)) { email.addTo(toAddress); } else { email.addTo(toAddress, toName); } email.setFrom(fromAddress, fromName); email.setSubject(subject); if (htmlMessage != null) { email.setHtmlMsg(htmlMessage); } if (textMessage != null) { email.setTextMsg(textMessage); } email.send(); } catch (EmailException ex) { // TODO think about throwing GravifonException LOGGER.warn(String.format("Failed to send an email to %s", toAddress), ex); return false; } return true; }
From source file:org.infoglue.cms.util.mail.MailService.java
/** * * @param from the sender of the email.// w w w. j ava2s . co m * @param to the recipient of the email. * @param subject the subject of the email. * @param content the body of the email. * @throws SystemException if the email couldn't be sent due to some mail server exception. */ public void sendHTML(String from, String to, String cc, String bcc, String bounceAddress, String replyTo, String subject, String content, String encoding) throws SystemException { try { HtmlEmail email = new HtmlEmail(); String mailServer = CmsPropertyHandler.getMailSmtpHost(); String mailPort = CmsPropertyHandler.getMailSmtpPort(); String systemEmailSender = CmsPropertyHandler.getSystemEmailSender(); email.setHostName(mailServer); if (mailPort != null && !mailPort.equals("")) email.setSmtpPort(Integer.parseInt(mailPort)); boolean needsAuthentication = false; try { needsAuthentication = new Boolean(CmsPropertyHandler.getMailSmtpAuth()).booleanValue(); } catch (Exception ex) { needsAuthentication = false; } if (needsAuthentication) { final String userName = CmsPropertyHandler.getMailSmtpUser(); final String password = CmsPropertyHandler.getMailSmtpPassword(); email.setAuthentication(userName, password); } email.setBounceAddress(systemEmailSender); email.setCharset(encoding); if (logger.isInfoEnabled()) { logger.info("systemEmailSender:" + systemEmailSender); logger.info("to:" + to); logger.info("from:" + from); logger.info("mailServer:" + mailServer); logger.info("mailPort:" + mailPort); logger.info("cc:" + cc); logger.info("bcc:" + bcc); logger.info("replyTo:" + replyTo); logger.info("subject:" + subject); } if (to.indexOf(";") > -1) { cc = to; to = from; } String limitString = CmsPropertyHandler.getEmailRecipientLimit(); if (limitString != null && !limitString.equals("-1")) { try { Integer limit = new Integer(limitString); int count = 0; if (cc != null) count = count + cc.split(";").length; if (bcc != null) count = count + bcc.split(";").length; logger.info("limit: " + limit + ", count: " + count); if (count > limit) throw new Exception("You are not allowed to send mail to more than " + limit + " recipients at a time. This is specified in app settings."); } catch (NumberFormatException e) { logger.error("Exception validating number of recipients in mailservice:" + e.getMessage(), e); } } email.addTo(to, to); email.setFrom(from, from); if (cc != null) email.setCc(createInternetAddressesList(cc)); if (bcc != null) email.setBcc(createInternetAddressesList(bcc)); if (replyTo != null) email.setReplyTo(createInternetAddressesList(replyTo)); email.setSubject(subject); email.setHtmlMsg(content); email.setTextMsg("Your email client does not support HTML messages"); email.send(); logger.info("Email sent!"); } catch (Exception e) { logger.error("An error occurred when we tried to send this mail:" + e.getMessage(), e); throw new SystemException("An error occurred when we tried to send this mail:" + e.getMessage(), e); } }
From source file:org.jcronjob.service.NoticeService.java
public void sendMessage(Long receiverId, Long workId, String emailAddress, String mobiles, String content) { try {/*from w w w. j a v a 2 s . c o m*/ HtmlEmail email = new HtmlEmail(); email.setCharset("UTF-8"); email.setHostName(config.getSmtpHost()); email.setSslSmtpPort(config.getSmtpPort().toString()); email.setAuthentication(config.getSenderEmail(), config.getPassword()); email.setFrom(config.getSenderEmail()); email.setSubject("cronjob"); email.setHtmlMsg(msgToHtml(receiverId, content)); email.addTo(emailAddress.split(",")); email.send(); Log log = new Log(); log.setType(0); log.setWorkerId(workId); log.setMessage(content); for (String receiver : emailAddress.split(",")) { log.setReceiver(receiver); log.setSendTime(new Date()); homeService.saveLog(log); } log.setType(1); for (String mobile : mobiles.split(",")) { //??POST String sendUrl = String.format(config.getSendUrl(), mobile, String.format(config.getTemplate(), content)); String url = sendUrl.substring(0, sendUrl.indexOf("?")); String postData = sendUrl.substring(sendUrl.indexOf("?") + 1); String message = HttpUtils.doPost(url, postData, "UTF-8"); log.setReceiver(mobile); log.setResult(message); log.setSendTime(new Date()); homeService.saveLog(log); logger.info(message); } } catch (Exception e) { e.printStackTrace(System.err); } }
From source file:org.jevis.jealarm.AlarmHandler.java
/** * Send the Alarm mail//from ww w. jav a2 s. c o m * * @param conf * @param alarm * @param body */ public void sendAlarm(Config conf, Alarm alarm, String body) { try { HtmlEmail email = new HtmlEmail(); // Email email = new SimpleEmail(); email.setHostName(conf.getSmtpServer()); email.setSmtpPort(conf.getSmtpPort()); email.setAuthenticator(new DefaultAuthenticator(conf.getSmtpUser(), conf.getSmtpPW())); email.setSSLOnConnect(conf.isSmtpSSL()); email.setFrom(conf.smtpFrom); email.setSubject(alarm.getSubject()); for (String recipient : alarm.getRecipient()) { email.addTo(recipient); } for (String bcc : alarm.getBcc()) { email.addBcc(bcc); } email.setHtmlMsg(body); email.send(); System.out.println("Alarm send: " + alarm.getSubject()); } catch (Exception ex) { System.out.println("cound not send Email"); ex.printStackTrace(); } }
From source file:org.jkandasa.email.blaster.EmailUtils.java
public static void sendEmail(AppProperties appProperties, Address address) throws EmailException, IOException { HtmlEmail email = initializeEmail(appProperties); StringBuilder attachmentBuilder = new StringBuilder(); String[] attachments = appProperties.getAttachments().split(","); for (String attachment : attachments) { File img = new File(attachment); if (appProperties.isEmbeddedAttachments()) { attachmentBuilder.append("<br><img src=cid:").append(email.embed(img)).append(">"); } else {//w ww. j av a 2s .co m email.attach(img); } } email.setSubject(getSubject(appProperties, address)); email.setHtmlMsg(getMessage(appProperties, address).replaceAll(ATTACHMENTS, attachmentBuilder.toString())); email.addTo(address.getEmails().split(EMAIL_SEPARATOR)); String sendReturn = email.send(); _logger.debug("Send Status:[{}]", sendReturn); _logger.debug("Email successfully sent to [{}]", address); }
From source file:org.meerkat.network.MailManager.java
/** * sendEmail//from w ww .j a v a 2s . c o m * @param subject * @param message */ public final void sendEmail(String subject, String message) { this.refreshSettings(); HtmlEmail email = new HtmlEmail(); email.setHostName(getSMTPServer()); email.setSmtpPort(Integer.valueOf(getSMTPPort())); email.setSubject(subject); try { email.setHtmlMsg(message); } catch (EmailException e2) { log.error("Error in mail message. ", e2); } // SMTP security String security = getSMTPSecurity(); if (security.equalsIgnoreCase("STARTTLS")) { email.setTLS(true); } else if (security.equalsIgnoreCase("SSL/TLS")) { email.setSSL(true); email.setSslSmtpPort(String.valueOf(getSMTPPort())); } email.setAuthentication(getSMTPUser(), getSMTPPassword()); try { String[] toList = getTO().split(","); for (int i = 0; i < toList.length; i++) { email.addTo(toList[i].trim()); } } catch (EmailException e1) { log.error("EmailException: addTo(" + getTO() + "). " + e1.getMessage()); } try { email.setFrom(getFROM()); } catch (EmailException e1) { log.error("EmailException: setFrom(" + getFROM() + "). " + e1.getMessage()); } // Send the email try { email.send(); } catch (EmailException e) { log.error("Failed to send email!", e); } }
From source file:org.meerkat.network.MailManager.java
/** * testEmailSettingsFromWebService/*ww w . j a va 2s . c o m*/ * @param from * @param to * @param smtpServer * @param smtpPort * @param smtpSecurity * @param smtpUser * @param smtpPassword * @return */ public final String sendTestEmailSettingsFromWebService(String from, String to, String smtpServer, String smtpPort, String smtpSecurity, String smtpUser, String smtpPassword) { String resultString = "OK"; HtmlEmail email = new HtmlEmail(); email.setHostName(smtpServer); email.setSmtpPort(Integer.valueOf(smtpPort)); email.setSubject(testSubject); try { email.setHtmlMsg(testMessage); } catch (EmailException e2) { resultString = e2.getMessage(); return resultString; } // SMTP security if (smtpSecurity.equalsIgnoreCase("STARTTLS")) { email.setTLS(true); } else if (smtpSecurity.equalsIgnoreCase("SSLTLS")) { email.setSSL(true); email.setSslSmtpPort(String.valueOf(smtpPort)); } email.setAuthentication(smtpUser, smtpPassword); try { String[] toList = to.split(","); for (int i = 0; i < toList.length; i++) { email.addTo(toList[i].trim()); } } catch (EmailException e1) { resultString = "TO: " + e1.getMessage(); return resultString; } try { email.setFrom(from); } catch (EmailException e1) { resultString = "FROM: " + e1.getMessage(); return resultString; } // Send the email try { email.send(); } catch (EmailException e) { resultString = e.getMessage(); return resultString; } return resultString; }