List of usage examples for org.apache.commons.mail Email addCc
public Email addCc(final String... emails) throws EmailException
From source file:org.jwebsocket.plugins.mail.MailPlugInService.java
/** * * @param aToken/* w w w . java 2 s .c o 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
private void sendEmail(final List<String> to, final List<String> cc, final String subject, final Email email) throws EmailException { if (logOnly) { return;/*w w w. j a v a 2 s . c om*/ } email.setSmtpPort(useSmtpPort); if (useSmtpAuth) { email.setAuthentication(smtpUserName, smtpUserPassword); } email.setHostName(smtpServerName); email.setFrom(from); email.setSubject(subject); if (to != null) { for (final String recipient : to) { email.addTo(recipient); } } if (cc != null) { for (final String recipient : cc) { email.addCc(recipient); } } email.setSSL(useSSL); logService.log(LogService.LOG_INFO, String.format("Sending email to %s, cc %s, subject %s", to, cc, subject)); email.send(); }
From source file:org.killbill.billing.util.email.DefaultEmailSender.java
private void sendEmail(final List<String> to, final List<String> cc, final String subject, final Email email) throws EmailApiException { try {/*from ww w .ja v a2s .co m*/ email.setSmtpPort(config.getSmtpPort()); if (config.useSmtpAuth()) { email.setAuthentication(config.getSmtpUserName(), config.getSmtpPassword()); } email.setHostName(config.getSmtpServerName()); email.setFrom(config.getDefaultFrom()); email.setSubject(subject); if (to != null) { for (final String recipient : to) { email.addTo(recipient); } } if (cc != null) { for (final String recipient : cc) { email.addCc(recipient); } } email.setSSL(config.useSSL()); log.info("Sending email to='{}', cc='{}', subject='{}'", to, cc, subject); email.send(); } catch (EmailException ee) { throw new EmailApiException(ee, ErrorCode.EMAIL_SENDING_FAILED); } }
From source file:org.ms123.common.workflow.tasks.TaskMailExecutor.java
protected void addCc(Email email, String cc) { String[] ccs = splitAndTrim(cc); if (ccs != null) { for (String c : ccs) { try { email.addCc(c); } catch (EmailException e) { throw new RuntimeException("TaskMailExecutor:Could not add " + c + " as cc recipient", e); }/*from w ww . j av a 2 s.com*/ } } }
From source file:org.ms123.common.workflow.TaskSendExecutor.java
protected void addCc(Email email, String cc) { String[] ccs = splitAndTrim(cc); if (ccs != null) { for (String c : ccs) { try { email.addCc(c); } catch (EmailException e) { throw new RuntimeException("TaskSendExecutor:Could not add " + c + " as cc recipient", e); }/* www . java 2 s.c o m*/ } } }
From source file:org.paxml.bean.EmailTag.java
private Email createEmail(Collection<String> to, Collection<String> cc, Collection<String> bcc) throws EmailException { Email email; if (attachment == null || attachment.isEmpty()) { email = new SimpleEmail(); } else {/*from ww w . ja va 2s. co m*/ MultiPartEmail mpemail = new MultiPartEmail(); for (Object att : attachment) { mpemail.attach(makeAttachment(att.toString())); } email = mpemail; } if (StringUtils.isNotEmpty(username)) { String pwd = null; if (password instanceof Secret) { pwd = ((Secret) password).getDecrypted(); } else if (password != null) { pwd = password.toString(); } email.setAuthenticator(new DefaultAuthenticator(username, pwd)); } email.setHostName(findHost()); email.setSSLOnConnect(ssl); if (port > 0) { if (ssl) { email.setSslSmtpPort(port + ""); } else { email.setSmtpPort(port); } } if (replyTo != null) { for (Object r : replyTo) { email.addReplyTo(r.toString()); } } email.setFrom(from); email.setSubject(subject); email.setMsg(text); if (to != null) { for (String r : to) { email.addTo(r); } } if (cc != null) { for (String r : cc) { email.addCc(r); } } if (bcc != null) { for (String r : bcc) { email.addBcc(r); } } email.setSSLCheckServerIdentity(sslCheckServerIdentity); email.setStartTLSEnabled(tls); email.setStartTLSRequired(tls); return email; }
From source file:org.structr.common.MailHelper.java
private static void setup(final Email mail, final String to, final String toName, final String from, final String fromName, final String cc, final String bcc, final String bounce, final String subject) throws EmailException { // FIXME: this might be slow if the config file is read each time final Properties config = Services.getInstance().getCurrentConfig(); final String smtpHost = config.getProperty(Services.SMTP_HOST, "localhost"); final String smtpPort = config.getProperty(Services.SMTP_PORT, "25"); final String smtpUser = config.getProperty(Services.SMTP_USER); final String smtpPassword = config.getProperty(Services.SMTP_PASSWORD); final String smtpUseTLS = config.getProperty(Services.SMTP_USE_TLS, "true"); final String smtpRequireTLS = config.getProperty(Services.SMTP_REQUIRE_TLS, "false"); mail.setCharset(charset);/*w w w . j a v a2s .co m*/ mail.setHostName(smtpHost); mail.setSmtpPort(Integer.parseInt(smtpPort)); mail.setStartTLSEnabled(Boolean.parseBoolean(smtpUseTLS)); mail.setStartTLSRequired(Boolean.parseBoolean(smtpRequireTLS)); mail.setCharset(charset); mail.setHostName(smtpHost); mail.setSmtpPort(Integer.parseInt(smtpPort)); if (StringUtils.isNotBlank(smtpUser) && StringUtils.isNotBlank(smtpPassword)) { mail.setAuthentication(smtpUser, smtpPassword); } mail.addTo(to, toName); mail.setFrom(from, fromName); if (StringUtils.isNotBlank(cc)) { mail.addCc(cc); } if (StringUtils.isNotBlank(bcc)) { mail.addBcc(bcc); } if (StringUtils.isNotBlank(bounce)) { mail.setBounceAddress(bounce); } mail.setSubject(subject); }
From source file:scouter.plugin.server.alert.email.EmailPlugin.java
/** * ? ? ?? <br>//from www.j a v a 2 s.c o m * ? ?? ?? <br> * @param pack ?? ? pack */ @ServerPlugin(PluginConstants.PLUGIN_SERVER_ALERT) public void alert(final AlertPack pack) { /* Email */ if (conf.getBoolean("ext_plugin_email_send_alert", true)) { /* * Get log level (0 : INFO, 1 : WARN, 2 : ERROR, 3 : FATAL) * 0 ?? ?? ?. */ int level = conf.getInt("ext_plugin_email_level", 0); /* ? level ? ? ? */ if (level <= pack.level) { new Thread() { public void run() { try { // Get server configurations for email String hostname = conf.getValue("ext_plugin_email_smtp_hostname", "smtp.gmail.com"); // smtp int port = conf.getInt("ext_plugin_email_smtp_port", 587); // smtp ? String username = conf.getValue("ext_plugin_email_username", "haniumscouter@gmail.com"); // ? ?? String password = conf.getValue("ext_plugin_email_password", "dkqorhvk!@#$"); // ? ?? boolean tlsEnabled = conf.getBoolean("ext_plugin_email_tls_enabled", true); // tls (gamil? true) String from = conf.getValue("ext_plugin_email_from_address", "haniumscouter@gmail.com"); // ?? ? ?? String to = conf.getValue("ext_plugin_email_to_address", "occidere@naver.com"); // ? ??(? , ) String cc = conf.getValue("ext_plugin_email_cc_address"); // cc ?? assert hostname != null; assert port > 0; assert username != null; assert password != null; assert from != null; assert to != null; // Get agent Name. ??. String name = AgentManager.getAgentName(pack.objHash) == null ? "N/A" : AgentManager.getAgentName(pack.objHash); if (name.equals("N/A") && pack.message.endsWith("connected.")) { int idx = pack.message.indexOf("connected"); if (pack.message.indexOf("reconnected") > -1) name = pack.message.substring(0, idx - 6); else name = pack.message.substring(0, idx - 4); } // Make email subject String subject = "[" + AlertLevel.getName(pack.level) + "] " + pack.objType.toUpperCase() + "(" + name + ") : " + pack.title; String title = pack.title; String msg = pack.message; /* * Agent ? (inactivate) ? ? . * ? , ? ? Agent ? ? * Agent ? title? ? . */ if (title.equals("INACTIVE_OBJECT")) { title = name + " (Inactivated) ?!"; msg = pack.message.substring(0, pack.message.indexOf("OBJECT") - 1); } //? ? ? ? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd? HH mm ss"); // Make email message String message = "[?? ? ver.20170815]" + Util.NEW_LINE + "[ ] : " + title + Util.NEW_LINE + "[ ] : " + sdf.format(new Date(pack.time)) + Util.NEW_LINE + "[ ] : " + pack.objType.toUpperCase() + Util.NEW_LINE + "[? ] : " + name + Util.NEW_LINE + "[ ] : " + AlertLevel.getName(pack.level) + Util.NEW_LINE + "[ ] : " + msg + Util.NEW_LINE; // Create an Email instance Email email = new SimpleEmail(); email.setHostName(hostname); email.setSmtpPort(port); email.setAuthenticator(new DefaultAuthenticator(username, password)); email.setStartTLSEnabled(tlsEnabled); email.setFrom(from); email.setSubject(subject); email.setMsg(message); //? , for (String addr : to.split(",")) { email.addTo(addr); } //cc , if (cc != null) { for (String addr : cc.split(",")) email.addCc(addr); } // Send the email email.send(); println("Email about " + name + " sent to [" + to + "] successfully."); Logger.println("Email about " + name + " sent to [" + to + "] successfully."); } catch (Exception e) { println("[? ] : " + e.getMessage()); Logger.printStackTrace(e); if (conf._trace) { e.printStackTrace(); } } } }.start(); } } }
From source file:uap.workflow.engine.bpmn.behavior.MailActivityBehavior.java
protected void addCc(Email email, String cc) { String[] ccs = splitAndTrim(cc); if (ccs != null) { for (String c : ccs) { try { email.addCc(c); } catch (EmailException e) { throw new WorkflowException("Could not add " + c + " as cc recipient", e); }/*ww w. jav a 2 s.c om*/ } } }