List of usage examples for org.apache.commons.mail HtmlEmail setHtmlMsg
public HtmlEmail setHtmlMsg(final String aHtml) throws EmailException
From source file:org.flowable.engine.impl.bpmn.behavior.MailActivityBehavior.java
protected HtmlEmail createHtmlEmail(String text, String html) { HtmlEmail email = new HtmlEmail(); try {//from w w w .j ava 2s .c o m email.setHtmlMsg(html); if (text != null) { // for email clients that don't support html email.setTextMsg(text); } return email; } catch (EmailException e) { throw new FlowableException("Could not create HTML email", 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 w w w. j a va 2 s .co m 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.//from w w w .jav a 2 s .c om * @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 {// ww w . j a v a 2 s.c om 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 .ja v a2s. 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 {//from w w w .j a va2s . c o 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.jwebsocket.plugins.mail.MailPlugIn.java
private void sendMail(WebSocketConnector aConnector, Token aToken) { TokenServer lServer = getServer();// ww w . j av a 2s.co m String lFrom = aToken.getString("from", "[unknown]"); String lTo = aToken.getString("to"); String lCC = aToken.getString("cc"); String lBCC = aToken.getString("bcc"); String lSubject = aToken.getString("subject"); String lBody = aToken.getString("body"); Boolean lIsHTML = aToken.getBoolean("html", false); // instantiate response token Token lResponse = lServer.createResponse(aToken); Map lMap = new FastMap(); if (lFrom != null && lFrom.length() > 0) { lMap.put("from", lFrom); } if (lTo != null && lTo.length() > 0) { lMap.put("to", lTo); } if (lCC != null && lCC.length() > 0) { lMap.put("cc", lCC); } if (lBCC != null && lBCC.length() > 0) { lMap.put("bcc", lBCC); } if (lSubject != null && lSubject.length() > 0) { lMap.put("subject", lSubject); } if (lBody != null && lBody.length() > 0) { lMap.put("body", lBody); } // Create the attachment List<EmailAttachment> lAttachments = new FastList<EmailAttachment>(); /* if( aAttachments != null ) { for( int lIdx = 0; lIdx < aAttachments.length; lIdx++ ) { EmailAttachment lAttachment = new EmailAttachment(); lAttachment.setPath( aAttachments[ lIdx ] ); lAttachment.setDisposition( EmailAttachment.ATTACHMENT ); // lAttachment.setDescription( "Picture of John" ); // lAttachment.setName( "John" ); lAttachments.add( lAttachment ); } } */ // Create the lEmail message if (mLog.isDebugEnabled()) { mLog.debug("Sending e-mail to " + lTo + " with subject '" + lSubject + "'..."); } try { Email lEmail; if (lIsHTML) { lEmail = new HtmlEmail(); } else { lEmail = new MultiPartEmail(); } lEmail.setHostName(SMTP_HOST); lEmail.setSmtpPort(SMTP_PORT); if (SMTP_AUTH) { lEmail.setAuthentication(SMTP_USER, SMTP_PASSWORD); } if (SMTP_POP3BEFORE) { lEmail.setPopBeforeSmtp(true, POP3_HOST, POP3_USER, POP3_PASSWORD); } if (lFrom != null && lFrom.length() > 0) { lEmail.setFrom(lFrom); } if (lTo != null && lTo.length() > 0) { lEmail.addTo(lTo); } if (lSubject != null && lSubject.length() > 0) { lEmail.setSubject(lSubject); } if (lBody != null && lBody.length() > 0) { if (lIsHTML) { HtmlEmail lHTML = ((HtmlEmail) lEmail); /* URL lURL = new URL("http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg"); String lCID = ((HtmlEmail )lEmail).embed(lURL, "five feet further logo"); //url = new URL( "http://five-feet-further.com/resources/css/IJX4FWDocu.css" ); // String css = ((HtmlEmail)lEmail).embed( url, "name of css" ); ((HtmlEmail )lEmail).setHtmlMsg( "<html><body>" + "<style type=\"text/css\">" + "h1 { " + " font-family:arial, helvetica, sans-serif;" + " font-weight:bold;" + " font-size:18pt;" + "}" + "</style>" + // "<link href=\"cid:" + css + "\" type=\"text/css\" rel=\"stylesheet\">" + "<p><img src=\"cid:" + lCID + "\"></p>" + "<p><img src=\"http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg\"></p>" + lItem + "</body></html>"); */ /* // Now the message body. Multipart mp = new MimeMultipart(); BodyPart textPart = new MimeBodyPart(); // sets type to "text/plain" textPart.setText("Kann Ihr Browser keine HTML-Mails darstellen?"); BodyPart pixPart = new MimeBodyPart(); pixPart.setContent(lMsg, "text/html"); // Collect the Parts into the MultiPart mp.addBodyPart(textPart); mp.addBodyPart(pixPart); // Put the MultiPart into the Message ((HtmlEmail) lEmail).setContent((MimeMultipart)mp); ((HtmlEmail) lEmail).buildMimeMessage(); /* // ((HtmlEmail) lEmail).setContent(lMsg, Email.TEXT_HTML); // lHeaders.put("Innotrade-Id", "4711-0815"); // lHTML.setHeaders(lHeaders); // ((HtmlEmail) lEmail).setCharset("UTF-8"); // ((HtmlEmail) lEmail).setMsg(lMsg); lMM.setHeader("Innotrade-Id", "4711-0815"); // ((HtmlEmail) lEmail).setContent(lTxtMsg, Email.TEXT_PLAIN); */ // String lTxtMsg = "Your Email-Client does not support HTML messages."; lHTML.setHtmlMsg(lBody); // lHTML.setTextMsg(lTxtMsg); } else { lEmail.setMsg(lBody); } } // add attachment(s), if such for (EmailAttachment lAttachment : lAttachments) { ((MultiPartEmail) lEmail).attach(lAttachment); } for (int lIdx = 0; lIdx < lAttachments.size(); lIdx++) { ((MultiPartEmail) lEmail).attach((EmailAttachment) lAttachments.get(lIdx)); } // send the Email String lMsgId = lEmail.send(); if (mLog.isInfoEnabled()) { mLog.info("Email successfully sent" + " from " + (lFrom != null ? lFrom : "(no sender)") + " to " + (lTo != null ? lTo : "(no receipient)") + ", subject " + (lSubject != null ? "'" + lSubject + "'" : "(no subject)") + ", Id " + lMsgId); } lResponse.setString("id", lMsgId); } catch (Exception lEx) { String lMsg = lEx.getClass().getSimpleName() + ": " + lEx.getMessage(); mLog.error(lMsg); lResponse.setInteger("code", -1); lResponse.setString("msg", lMsg); } // send response to requester lServer.sendToken(aConnector, lResponse); }
From source file:org.jwebsocket.plugins.mail.MailPlugInService.java
/** * * @param aToken/*from w w w . java 2 s . co m*/ * @return */ public Token sendMail(Token aToken) { String lFrom = aToken.getString("from", "[unknown]"); String lTo = aToken.getString("to"); String lCC = aToken.getString("cc"); String lBCC = aToken.getString("bcc"); String lSubject = aToken.getString("subject"); String lBody = aToken.getString("body"); Boolean lIsHTML = aToken.getBoolean("html", false); List<Object> lAttachedFiles = aToken.getList("attachments"); String lMsg; // instantiate response token Token lResponse = TokenFactory.createToken(); Map<String, String> lMap = new FastMap<String, String>(); if (lFrom != null && lFrom.length() > 0) { lMap.put("from", lFrom); } if (lTo != null && lTo.length() > 0) { lMap.put("to", lTo); } if (lCC != null && lCC.length() > 0) { lMap.put("cc", lCC); } if (lBCC != null && lBCC.length() > 0) { lMap.put("bcc", lBCC); } if (lSubject != null && lSubject.length() > 0) { lMap.put("subject", lSubject); } if (lBody != null && lBody.length() > 0) { lMap.put("body", lBody); } // Create the attachment List<EmailAttachment> lEmailAttachments = new FastList<EmailAttachment>(); if (lAttachedFiles != null) { for (Object lAttachedFile : lAttachedFiles) { EmailAttachment lAttachment = new EmailAttachment(); lAttachment.setPath((String) lAttachedFile); lAttachment.setDisposition(EmailAttachment.ATTACHMENT); // lAttachment.setDescription( "Picture of John" ); // lAttachment.setName( "John" ); lEmailAttachments.add(lAttachment); } } // Create the lEmail message if (mLog.isDebugEnabled()) { mLog.debug("Sending e-mail to " + lTo + " with subject '" + lSubject + "'..."); } try { Email lEmail; if (lIsHTML) { lEmail = new HtmlEmail(); } else { lEmail = new MultiPartEmail(); } lEmail.setHostName(mSettings.getSmtpHost()); lEmail.setSmtpPort(mSettings.getSmtpPort()); if (mSettings.getSmtpAuth()) { lEmail.setAuthentication(mSettings.getSmtpUser(), mSettings.getSmtpPassword()); } if (mSettings.getSmtpPop3Before()) { lEmail.setPopBeforeSmtp(true, mSettings.getPop3Host(), mSettings.getPop3User(), mSettings.getPop3Password()); } if (lFrom != null && lFrom.length() > 0) { lEmail.setFrom(lFrom); } if (lTo != null && lTo.length() > 0) { String[] lToSplit = lTo.split(";"); for (String lToSplit1 : lToSplit) { if (lToSplit1 != null && lToSplit1.length() > 0) { lEmail.addTo(lToSplit1.trim()); } } } if (lCC != null && lCC.length() > 0) { String[] lCCSplit = lCC.split(";"); for (String lCCSplit1 : lCCSplit) { if (lCCSplit1 != null && lCCSplit1.length() > 0) { lEmail.addCc(lCCSplit1.trim()); } } } if (lBCC != null && lBCC.length() > 0) { String[] lBCCSplit = lBCC.split(";"); for (String lBCCSplit1 : lBCCSplit) { if (lBCCSplit1 != null && lBCCSplit1.length() > 0) { lEmail.addBcc(lBCCSplit1.trim()); } } } if (lSubject != null && lSubject.length() > 0) { lEmail.setSubject(lSubject); } if (lBody != null && lBody.length() > 0) { if (lIsHTML) { HtmlEmail lHTML = ((HtmlEmail) lEmail); /* * URL lURL = new * URL("http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg"); * String lCID = ((HtmlEmail )lEmail).embed(lURL, "five feet * further logo"); * * //url = new URL( * "http://five-feet-further.com/resources/css/IJX4FWDocu.css" * ); // String css = ((HtmlEmail)lEmail).embed( url, "name * of css" ); * * ((HtmlEmail )lEmail).setHtmlMsg( "<html><body>" + "<style * type=\"text/css\">" + "h1 { " + " font-family:arial, * helvetica, sans-serif;" + " font-weight:bold;" + " * font-size:18pt;" + "}" + "</style>" + // "<link * href=\"cid:" + css + "\" type=\"text/css\" * rel=\"stylesheet\">" + "<p><img src=\"cid:" + lCID + * "\"></p>" + "<p><img * src=\"http://five-feet-further.com/aschulze/images/portrait_web_kleiner.jpg\"></p>" * + lItem + "</body></html>"); */ /* * // Now the message body. Multipart mp = new * MimeMultipart(); * * BodyPart textPart = new MimeBodyPart(); // sets type to * "text/plain" textPart.setText("Kann Ihr Browser keine * HTML-Mails darstellen?"); * * BodyPart pixPart = new MimeBodyPart(); * pixPart.setContent(lMsg, "text/html"); * * // Collect the Parts into the MultiPart * mp.addBodyPart(textPart); mp.addBodyPart(pixPart); * * // Put the MultiPart into the Message ((HtmlEmail) * lEmail).setContent((MimeMultipart)mp); ((HtmlEmail) * lEmail).buildMimeMessage(); * * /* * // ((HtmlEmail) lEmail).setContent(lMsg, * Email.TEXT_HTML); * * // lHeaders.put("Innotrade-Id", "4711-0815"); // * lHTML.setHeaders(lHeaders); // ((HtmlEmail) * lEmail).setCharset("UTF-8"); // ((HtmlEmail) * lEmail).setMsg(lMsg); lMM.setHeader("Innotrade-Id", * "4711-0815"); * * // ((HtmlEmail) lEmail).setContent(lTxtMsg, * Email.TEXT_PLAIN); */ // String lTxtMsg = "Your Email-Client does not support HTML messages."; lHTML.setHtmlMsg(lBody); // lHTML.setTextMsg(lTxtMsg); } else { lEmail.setMsg(lBody); } } // add attachment(s), if such for (EmailAttachment lAttachment : lEmailAttachments) { ((MultiPartEmail) lEmail).attach(lAttachment); } // send the Email String lMsgId = lEmail.send(); if (mLog.isInfoEnabled()) { lMsg = "Email successfully sent" + " from " + (lFrom != null ? lFrom : "(no sender)") + " to " + (lTo != null ? lTo : "(no recipient)") + " cc " + (lCC != null ? lCC : "(no recipient)") + ", subject " + (lSubject != null ? "'" + lSubject + "'" : "(no subject)") + ", msgId " + lMsgId; mLog.info(lMsg); } lResponse.setInteger("code", 0); lResponse.setString("msg", "ok"); lResponse.setString("msgId", lMsgId); } catch (EmailException lEx) { lMsg = lEx.getClass().getSimpleName() + " (" + lEx.getCause().getClass().getSimpleName() + "): " + lEx.getMessage(); mLog.error(lMsg); lResponse.setInteger("code", -1); lResponse.setString("msg", lMsg); } return lResponse; }
From source file:org.killbill.billing.plugin.notification.email.EmailSender.java
public void sendHTMLEmail(final List<String> to, final List<String> cc, final String subject, final String htmlBody) throws EmailException { final HtmlEmail email = new HtmlEmail(); email.setHtmlMsg(htmlBody); sendEmail(to, cc, subject, email);//from w ww . ja va 2s .co m }
From source file:org.meerkat.network.MailManager.java
/** * sendEmail//from ww w . j a v a2s. 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); } }