protected HtmlEmail prepareEmail(String name, String toMail) { with("to_name", name); with("to_email", toMail); with("host", appLocation); if (!hasSigner) { with("signer", this.localization.getMessage("signer")); } HtmlEmail email = new HtmlEmail(); email.setCharset("utf-8"); try { addEmbeddables(email); addAttachments(email); email.addTo(toMail, name); boolean hasNoSubjectDefined = this.localization.getMessage(name, nameParameters).equals("???" + name + "???"); if (hasNoSubjectDefined) { throw new IllegalArgumentException("Subject not defined for email template : " + name); } else { email.setSubject(this.localization.getMessage(this.templateName, nameParameters)); } email.setHtmlMsg(this.template.getContent()); } catch (Exception e) { throw new RuntimeException(e); } return email; }
/** * 发送模板邮件 * * @param toMailAddr 收信人地址 * @param subject email主题 * @param templatePath 模板地址 * @param map 模板map */ public static void sendFtlMail( String toMailAddr, String subject, String templatePath, Map<String, Object> map) { Template template = null; Configuration freeMarkerConfig = null; HtmlEmail hemail = new HtmlEmail(); try { hemail.setHostName(getHost(from)); hemail.setSmtpPort(getSmtpPort(from)); hemail.setCharset(charSet); hemail.addTo(toMailAddr); hemail.setFrom(from, fromName); hemail.setAuthentication(username, password); hemail.setSubject(subject); freeMarkerConfig = new Configuration(); freeMarkerConfig.setDirectoryForTemplateLoading(new File(getFilePath())); // 获取模板 template = freeMarkerConfig.getTemplate(getFileName(templatePath), new Locale("Zh_cn"), "UTF-8"); // 模板内容转换为string String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map); System.out.println(htmlText); hemail.setMsg(htmlText); hemail.send(); System.out.println("email send true!"); } catch (Exception e) { e.printStackTrace(); System.out.println("email send error!"); } }
@Asynchronous public void sendEmail( final String body, final String subject, List<String> recipients, final String from, List<String> attachment, boolean html) { if (recipients != null && !recipients.isEmpty()) { HtmlEmail email = new HtmlEmail(); try { email.setHostName(MainConfig.EMAIL_SMTP_HOST); email.setSmtpPort(MainConfig.EMAIL_SMTP_PORT); if (!MainConfig.EMAIL_SMTP_USER.isEmpty() && !MainConfig.EMAIL_SMTP_PASSWORD.isEmpty()) { email.setAuthenticator( new DefaultAuthenticator(MainConfig.EMAIL_SMTP_USER, MainConfig.EMAIL_SMTP_PASSWORD)); } email.setTLS(MainConfig.EMAIL_SMTP_TLS); email.setFrom(from, MainConfig.EMAIL_FROM, "UTF-8"); email.setSubject(subject); email.setCharset("UTF-8"); if (html) { email.setHtmlMsg(body); } else { email.setTextMsg(body); } for (String s : recipients) { email.addTo(s); } if (attachment != null && !attachment.isEmpty()) { for (String s : attachment) { EmailAttachment at = new EmailAttachment(); at.setPath(s); at.setDisposition(EmailAttachment.ATTACHMENT); String parts[] = s.split("/"); if (parts.length > 0) { at.setName(parts[parts.length - 1]); } } } email.send(); } catch (Exception e) { logger.error("Can not send e-mail to " + recipients + " with subject: " + subject, e); } } else { logger.warn("Try send email with subject " + subject + " but recipients empty."); } }
/** * 发送普通邮件 * * @param toMailAddr 收信人地址 * @param subject email主题 * @param message 发送email信息 */ public static void sendCommonMail(String toMailAddr, String subject, String message) { HtmlEmail hemail = new HtmlEmail(); try { hemail.setHostName(getHost(from)); hemail.setSmtpPort(getSmtpPort(from)); hemail.setCharset(charSet); hemail.addTo(toMailAddr); hemail.setFrom(from, fromName); hemail.setAuthentication(username, password); hemail.setSubject(subject); hemail.setMsg(message); hemail.send(); System.out.println("email send true!"); } catch (Exception e) { e.printStackTrace(); System.out.println("email send error!"); } }
public static void sendPromocao(Cotacao cotacao) { HtmlEmail email = new HtmlEmail(); try { email.setHostName("mail.gentec.inf.br"); email.setSmtpPort(587); email.setAuthentication("*****@*****.**", "123456"); email.addTo("*****@*****.**"); email.setFrom("*****@*****.**", "Pedido de Cota\347\343o"); email.setCharset("ISO-8859-1"); email.setSubject("Gentec IT"); email.setHtmlMsg( (new StringBuilder("<html><body><h1> Empresa: ")) .append(cotacao.getNomeEmpresa()) .append("</h1>") .append("<br/>") .append("<div>") .append("Nome de Cliente: ") .append(cotacao.getNomeCliente()) .append("<br/>") .append("CNPJ: ") .append(cotacao.getNumeroCnpj()) .append("<br/>") .append("Telefone: ") .append(cotacao.getNumeroTelefone()) .append("<br/>") .append("E-mail: ") .append(cotacao.getEmail()) .append("<br/>") .append("Observa\347\365es: ") .append(cotacao.getObservacoes()) .append("<br/>") .append("</div>") .append("</body>") .append("</html>") .toString()); email.setTextMsg("Seu servidor de e-mail n\343o suporta mensagem HTML"); email.send(); } catch (EmailException e) { e.printStackTrace(); } }
public void sendHtmlMail(String toEmail, String HtmlMsg) { HtmlEmail email = new HtmlEmail(); email.setCharset("UTF-8"); email.setHostName("smtp.sina.com"); email.setSmtpPort(25); email.setAuthentication("*****@*****.**", "tianren"); email.setTLS(true); try { email.addTo(toEmail); email.setFrom("*****@*****.**"); email.setSubject("Sky Movie"); // Title email.setMsg("Ticket Order Info"); URL url = new URL("http://localhost:8888/TianRen/uploadAd/31049534581071.gif"); String cid = email.embed(url, "TianRen Logo"); email.setHtmlMsg( "<html><a href='http://localhost:8888/TianRen/index.jsp'><img src=\"cid:" + cid + "\" height=\"75\"></a><br/>" + HtmlMsg + "</html>"); email.send(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (EmailException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Signature public PHtmlEmail setCharset(String charset) { htmlEmail.setCharset(charset); return this; }
public void sendEmailConfirmation( String reportUrl, WoParametri woParametri, User user, List<String> fakture) { try { List<String> emails = new ArrayList<String>(); if (user.getWoPartnerSetting() != null && user.getWoPartnerSetting().get(0) != null && user.getWoPartnerSetting().get(0).getReceiveConfirm() == 1 && user.getWoPartnerSetting().get(0).getEmailAddress() != null) { emails.add(user.getWoPartnerSetting().get(0).getEmailAddress()); } if (user.getWoUserHasRights() != null && user.getWoUserHasRights().get("EMP") != null && user.getWoUserHasRights().get("EMP") && user.getWoUser() != null && user.getWoUser().getEmail() != null) { if (emails.size() == 0 || (emails.size() > 0 && !emails.get(0).equals(user.getWoUser().getEmail()))) emails.add(user.getWoUser().getEmail()); } if ("INTERNI".equals(user.getWoUser().getUserType()) && user.getWoUser().getEmail() != null) { emails.add(user.getWoUser().getEmail()); } if (emails.size() > 0) { String emailContent = woParametri .getConfirmMailContent() .substring(1, Integer.valueOf(woParametri.getConfirmMailContent().length() + "")); log.info( "Prametri su " + woParametri.getMailServerPort() + " " + woParametri.getMailaddress() + " " + woParametri.getMailserver() + " " + woParametri.getPassword()); HtmlEmail email = new HtmlEmail(); email.setHostName(woParametri.getMailserver()); email.setSmtpPort(woParametri.getMailServerPort()); email.setCharset("Windows-1250"); // email.setDebug(true); if (woParametri.getWomailaddress() != null && woParametri.getPassword() != null) { email.setStartTLSRequired(true); // obavezno u slucaju gmail-a email.setAuthenticator( new DefaultAuthenticator(woParametri.getWomailaddress(), woParametri.getPassword())); } String[] sendTo = new String[emails.size()]; for (int i = 0; i < emails.size(); i++) { System.out.println(emails.get(i)); sendTo[i] = emails.get(i); } email.addTo(sendTo); email.setFrom(woParametri.getMailaddress(), "WebOrdering"); email.setSubject("Potvrda porudžbine..."); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(reportUrl); for (String idDokumenta : fakture) { List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("idDokumenta", idDokumenta)); UrlEncodedFormEntity sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8); post.setEntity(sendentity); HttpResponse postResponse = client.execute(post); email.attach( new ByteArrayDataSource(postResponse.getEntity().getContent(), "application/pdf"), idDokumenta + ".pdf", "Document description", EmailAttachment.ATTACHMENT); } // embed the image and get the content id // String cid = email.embed("http://www.apache.org/images/asf_logo_wide.gif", "Apache // logo"); // set the html message // email.setHtmlMsg("<html> Bla bla truc truc <br><br><br> <img src=\"cid:" + cid + // "\"></html>"); // email.setHtmlMsg("<html><font face='verdana' color='#e26f16'><b>" + emailContent + // "</b></font></html>"); email.setHtmlMsg(emailContent); email.send(); } } catch (Exception e) { log.error(e.getMessage(), e); } }