public void sendEmail(String mailAddress, String mailContent, Properties mailProperties, Map headerMap) throws TgwException { Email mail = null; try { mail = buildMail(mailProperties, headerMap); mail.addTo(mailAddress); setMailContent(mail, mailContent); // mail.MAIL_TRANSPORT_PROTOCOL mail.send(); log.info("completed to send email to " + mailAddress); } catch (EmailException ee) { log.error("failed to send email " + ee.getMessage()); throw new TgwException("ETGWEXT0001"); } }
/** * This method cover most of the cases of sending a simple text email. If the email to be sent has to be configured * in a way which is not possible whith the parameters provided (such as html email, or email with attachement, ..), * please use the {@link #sendCustomEmail(org.apache.commons.mail.Email)} method and refer the API * {@see http://commons.apache.org/email/}. * * @param to A set of destination email address of the email. Must contain at least one address. * @param object The subject of the email. * @param message The msg of the email. * @return true if the email has been sent successfully, false otherwise. */ public static boolean sendEmail(ArrayList<String> to, String object, String message) { Email email = new SimpleEmail(); try { email.setSubject(object); email.setMsg(message); if (to != null) for (String _to : to) email.addTo(_to); sendCustomEmail(email); return true; } catch (EmailException e) { logger.warn("Please, make sure that the SMTP server configuration is correct in the file: " + SMTP_FILE); logger.error(e.getMessage(), e); return false; } }
@Test public void testSendEmptyHTML2() throws Exception { Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST); // Create the email message final ImageHtmlEmail email = new ImageHtmlEmail(); // set the html message try { email.setHtmlMsg(""); fail("Should fail here!"); } catch (final EmailException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid message supplied")); } }
@Test public void testSendEmptyHTML() throws Exception { Logger.getLogger(ImageHtmlEmail.class.getName()).setLevel(Level.FINEST); // Create the email message final ImageHtmlEmail email = new ImageHtmlEmail(); // set the html message try { email.setHtmlMsg(null); fail("Should fail here!"); } catch (final EmailException e) { assertTrue(e.getMessage(), e.getMessage().contains("Invalid message supplied")); } }
@POST @Path("/email") public Response saveEmail(EmailSettings emailSettings, @QueryParam("testOnly") Boolean isTestOnly) { if (isTestOnly != null && isTestOnly) { try { EmailUtils.sendTestEmail(emailSettings); return RestUtils.getResponse(Status.OK, new ApiMessage("Email sent successfully. Check inbox of '" + emailSettings.getFromAddress() + "'")); } catch (EmailException ex) { return RestUtils.getResponse(Status.BAD_REQUEST, new ApiError(ex.getMessage())); } } else { emailSettings.save(); SettingsUtils.updateAllSettings(); return RestUtils.getResponse(Status.OK); } }
@POST @Path("/email") public Response saveEmail(EmailSettings emailSettings, @QueryParam("testOnly") Boolean isTestOnly) { if (isTestOnly != null && isTestOnly) { try { EmailUtils.sendTestEmail(emailSettings); return RestUtils.getResponse(Status.OK, new ApiMessage("Email sent successfully. Check inbox of '" + emailSettings.getFromAddress() + "'")); } catch (EmailException ex) { return RestUtils.getResponse(Status.BAD_REQUEST, new ApiError(ex.getMessage())); } } else { emailSettings.save(); SettingsUtils.updateAllSettings(); return RestUtils.getResponse(Status.OK); } }
public String sendEmailMessage(String mailTo,String mailFrom,String msgSubject,String msgContent) { MultiPartEmail multMail = new MultiPartEmail(); // SMTP multMail.setHostName(hostName); //CodeRepositoryUtil.getValue("SysMail", "host_name")); multMail.setSmtpPort(smtpPort); String resStr = "OK"; // 需要提供公用的消息用户名和密码 multMail.setAuthentication(userName, userPassword); //CodeRepositoryUtil.getValue("SysMail", "host_user"), //CodeRepositoryUtil.getValue("SysMail", "host_password")); try { //multMail.setFrom(CodeRepositoryUtil.getValue("SysMail", "admin_email")); multMail.setFrom(mailFrom); multMail.addTo(mailTo); multMail.setSubject(msgSubject); multMail.setMsg(msgContent); multMail.send(); return "OK"; } catch (EmailException e) { resStr=e.getMessage(); logger.error(e.getMessage(),e); //e.printStackTrace(); } return resStr; }
public static boolean sendEmail(String [] mailTo, String mailFrom, String msgSubject, String msgContent) { MultiPartEmail multMail = new MultiPartEmail(); // SMTP multMail.setHostName(mailHost); multMail.setSmtpPort(smtpPort); // 需要提供公用的邮件用户名和密码 multMail.setAuthentication( mailUser, mailPassword); try { //multMail.setFrom(CodeRepositoryUtil.getValue("SysMail", "admin_email")); multMail.setFrom(mailFrom); multMail.addTo(mailTo); multMail.setSubject(msgSubject); msgContent = msgContent.trim(); if(msgContent.endsWith("</html>") || msgContent.endsWith("</HTML>")){ multMail.addPart(msgContent, "text/html;charset=utf-8"); }else{ multMail.setMsg(msgContent); } multMail.send(); return true; } catch (EmailException e) { logger.error(e.getMessage(),e);//e.printStackTrace(); } return false; }
log.debug("Finish email send to: {}", request.getToAddress()); } catch (EmailException e) { log.error("Failed to send email: {}", e.getMessage(), e);
log.debug("Finish email send to: {}", request.getToAddress()); } catch (EmailException e) { log.error("Failed to send email: {}", e.getMessage(), e);
public static boolean sendEmail(String [] mailTo, String mailFrom, String msgSubject, String msgContent,List<File> annexs) { MultiPartEmail multMail = new MultiPartEmail(); // SMTP multMail.setHostName(mailHost); multMail.setSmtpPort(smtpPort); // 需要提供公用的邮件用户名和密码 multMail.setAuthentication( mailUser, mailPassword); try { //multMail.setFrom(CodeRepositoryUtil.getValue("SysMail", "admin_email")); multMail.setFrom(mailFrom); multMail.addTo(mailTo); multMail.setSubject(msgSubject); multMail.setMsg(msgContent); for(File attachment: annexs){ multMail.attach(attachment); } multMail.send(); return true; } catch (EmailException e) { logger.error(e.getMessage(),e);//e.printStackTrace(); } return false; }
void send(String subject, String msg, String... to) { String[] clearTo = removeInvalidEmails(to); if (isActive()) { try { Email email = createSimpleMailService(); email.setHostName(host); email.setFrom(from); email.setSubject(subjectPrefix + " " + subject); email.setMsg(msg); email.addTo(clearTo); if (this.authenticationEnabled) { email.setAuthentication(this.userName, this.password); } email.setSSLOnConnect(sslEnabled); email.send(); } catch (EmailException mailExcp) { LOGGER.error("Email could not be send because of " + mailExcp.getMessage(),mailExcp); throw new WasabiEmailException("Email: " + emailToString(subject, msg, to) + " could not be sent.", mailExcp); } } else { //if the service is not active log the email that would have been send and throw error LOGGER.info("EmailService would have sent: " + emailToString(subject, msg, to)); throw new WasabiEmailException(ErrorCode.EMAIL_NOT_ACTIVE_ERROR, "The EmailService is not active."); } }
record.addError(ProcessError.UNKNOWN_ERROR.getName(), "Unable to send email: " + ex.getMessage()); failedRecords.add(record); record.addError(ProcessError.UNKNOWN_ERROR.getName(), "Unable to send email: " + ex.getMessage()); failedRecords.add(record);
@Test public void testSendCorrectSmtpPortContainedInException() { try { getMailServer(); email = new MockEmailConcrete(); email.setHostName("bad.host.com"); email.setSSLOnConnect(true); email.setFrom(strTestMailFrom); email.addTo(strTestMailTo); email.setAuthentication(null, null); email.send(); fail("Should have thrown an exception"); } catch (final EmailException e) { assertTrue(e.getMessage().contains("bad.host.com:465")); fakeMailServer.stop(); } }