private static void configEmail(String subject, Email email, String... recipients) throws EmailException { if (recipients == null) throw new EmailException("Recipients not found."); Mail mail = MailPlugin.getMail(); email.setCharset(mail.getCharset()); email.setSocketTimeout(mail.getTimeout()); email.setSocketConnectionTimeout(mail.getConnectout()); email.setHostName(mail.getHost()); if (!mail.getSslport().isEmpty()) email.setSslSmtpPort(mail.getSslport()); if (!mail.getPort().isEmpty()) email.setSmtpPort(Integer.parseInt(mail.getPort())); email.setSSLOnConnect(mail.isSsl()); email.setStartTLSEnabled(mail.isTls()); email.setDebug(mail.isDebug()); email.setAuthentication(mail.getUser(), mail.getPassword()); email.setFrom(mail.getFrom(), mail.getName()); email.setSubject(subject); email.addTo(recipients); }
/** * Prints the stack trace of this exception to the standard error stream. */ @Override public void printStackTrace() { printStackTrace(System.err); }
@Test public void shouldSendTestEmailWithSTARTTLS() { smtpServer.getServer().setEnableTLS(true); smtpServer.getServer().setRequireTLS(true); configure(); when(configuration.getSecureConnection()).thenReturn("STARTTLS"); try { underTest.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); fail("An SSL exception was expected a a proof that STARTTLS is enabled"); } catch (EmailException e) { // We don't have a SSL certificate so we are expecting a SSL error assertThat(e.getCause().getMessage()).isEqualTo("Could not convert socket to TLS"); } }
Email email = new HtmlEmail(); email.setHostName(host); email.setStartTLSEnabled(starttlsEnabled); if (starttlsEnabled) { email.setSslSmtpPort(port); } else { email.setSmtpPort(Integer.parseInt(port)); email.setCharset("UTF-8"); if (isHtmlMsg) { ((HtmlEmail) email).setHtmlMsg(content); } else { ((HtmlEmail) email).setTextMsg(content); email.send(); logger.error(e.getLocalizedMessage(), e); return false;
Email email = new HtmlEmail(); email.setHostName(host); if (username != null && username.trim().length() > 0) { email.setAuthentication(username, password); email.addTo(receiver); email.setSubject(subject); email.setCharset("UTF-8"); ((HtmlEmail) email).setHtmlMsg(content); email.send(); email.getMailSession(); logger.error(e.getLocalizedMessage(),e); return false;
Email email = new SimpleEmail(); try { email.setHostName("smtp.googlemail.com"); email.setSmtpPort(465); email.setAuthenticator( new DefaultAuthenticator("hogwarts-mailer@gmail.com", "password")); email.setSSLOnConnect(true); // disable in case of EmailException email.setFrom("hogwarts-school@gmail.com", "Hogwarts School"); email.setSubject("Hogwarts Acceptance Letter"); email.setMsg("We are pleased to inform you that you have a place at " + "Hogwarts School of Witchcraft and Wizardry."); email.addTo("harry-potter@hedwig.com"); email.send(); } catch(EmailException ee) { ee.printStackTrace(); }
} else if (HTML == mimeType) { HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setHtmlMsg(renderEmail(emailTemplate.getHtmlBody(), context).toString()); email = htmlEmail; } else { // both HtmlEmail htmlEmail = new HtmlEmail(); htmlEmail.setHtmlMsg(renderEmail(emailTemplate.getHtmlBody(), context).toString()); htmlEmail.setTextMsg(renderEmail(emailTemplate.getTextBody(), context).toString()); email.setFrom(emailTemplate.getFromEmailAddress(), emailTemplate.getFromName()); email.addTo(request.getToAddress(), request.getToDisplayName()); email.setSubject(emailTemplate.getSubject()); log.debug("Finish email send to: {}", request.getToAddress()); } catch (EmailException e) { log.error("Failed to send email: {}", e.getMessage(), e);
public void sendHtmlEmail(String toEmail, String toName, String subject, String htmlMsg, String senderMail, String senderName, String mail_userName, String mail_password, String smtpPort) throws Exception { try { HtmlEmail email = new HtmlEmail(); MailServerInfo msi = (MailServerInfo)serverinfos.get(this.getMailDomain(senderMail)); String hostName = msi.getSmtpServer(); String sender_Mail = senderMail == null ? msi.getMailSender() : senderMail; String sender_Name = senderName == null ? msi.getMailSenderName() : senderName; String mailUserName = mail_userName == null ? msi.getMail_userName() : mail_userName; String mailPassword = mail_password == null ? msi.getMail_password() : mail_password; email.setAuthentication(mailUserName, mailPassword); email.setHostName(hostName); email.setSmtpPort(Integer.parseInt(smtpPort)); email.addTo(toEmail, toName); email.setFrom(sender_Mail, sender_Name); email.setSubject(subject); // email.setContent("HtmlEmail", "text/plain; charset=utf-8"); email.setHtmlMsg(htmlMsg); email.setTextMsg("Your email client does not support HTML messages"); email.setDebug(true); email.send(); } catch (EmailException e) { e.printStackTrace(); throw e; } }
Email email = new SimpleEmail(); email.setHostName("smtp.office365.com"); email.setSmtpPort(587); email.setAuthenticator(new DefaultAuthenticator("a@b.com", "****")); email.setStartTLSEnabled(true); try { email.setFrom("a@b.com"); email.setSubject("Job Failure"); email.setDebug(true); email.setMsg("This is a test mail ... :-)" ); email.addTo("a@y.com"); email.send(); } catch (EmailException e) { e.printStackTrace(); }
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."); } }
public void sendHtmlEmails(Map toEmails, String subject, String htmlMsg) throws Exception { try { HtmlEmail email = new HtmlEmail(); this.setEmail(email, toEmails, subject, "HtmlEmail"); email.setHtmlMsg(htmlMsg); email .setTextMsg("Your email client does not support HTML messages"); email.send(); } catch (EmailException e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } }
/** * 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; } }
throw new EmailException("Invalid message supplied"); setTextMsg(msg); .append(HTML_MESSAGE_END); setHtmlMsg(htmlMsgBuf.toString());
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"); } }
public void send(Email email) throws EmailException{ boolean sendForReal = env.has(SEND_REAL_EMAIL) || env.getName().equals("production"); if (sendForReal) { logger.info("REAL MAIL ::: {} ::: {}", email.getSubject(), email.getToAddresses()); if (email.getFromAddress() == null) { email.setFrom(env.get(FROM)); } if (env.has(REPLY_TO)) { email.addReplyTo(env.get(REPLY_TO)); } email.setMailSession(session); try { client.sendRawEmail(new SendRawEmailRequest() .withRawMessage(mail2Content(email))); } catch (Exception e) { throw new EmailException(e); } } else { new MockMailer().send(email); } } }
/** * Add a list of TO recipients to the email. The email * addresses will also be used as the personal names. * The names will be encoded by the charset of * {@link #setCharset(java.lang.String) setCharset()}. * If it is not set, it will be encoded using * the Java platform's default charset (UTF-16) if it contains * non-ASCII characters; otherwise, it is used as is. * * @param emails A String array. * @return An Email. * @throws EmailException Indicates an invalid email address. * @since 1.3 */ public Email addTo(final String... emails) throws EmailException { if (emails == null || emails.length == 0) { throw new EmailException("Address List provided was invalid"); } for (final String email : emails) { addTo(email, null); } return this; }
/** * Attempts to parse the specified <code>String</code> as a URL that will * then be embedded in the message. * * @param urlString String representation of the URL. * @param name The name that will be set in the filename header field. * @return A String with the Content-ID of the URL. * @throws EmailException when URL supplied is invalid or if {@code name} is null * or empty; also see {@link javax.mail.internet.MimeBodyPart} for definitions * * @see #embed(URL, String) * @since 1.1 */ public String embed(final String urlString, final String name) throws EmailException { try { return embed(new URL(urlString), name); } catch (final MalformedURLException e) { throw new EmailException("Invalid URL", e); } }
this.message = this.createMimeMessage(this.getMailSession()); this.updateContentType(this.contentType); && session.getProperty(EmailConstants.MAIL_FROM) == null) throw new EmailException("From address required"); throw new EmailException("At least one receiver address required"); throw new EmailException(me);
@Test public void fail_with_BadRequestException_when_EmailException_is_generated() throws Exception { logInAsSystemAdministrator(); IllegalArgumentException exception1 = new IllegalArgumentException("root cause"); IllegalArgumentException exception2 = new IllegalArgumentException("parent cause", exception1); IllegalArgumentException exception3 = new IllegalArgumentException("child cause", exception2); EmailException emailException = new EmailException("last message", exception3); doThrow(emailException).when(emailNotificationChannel).sendTestEmail(anyString(), anyString(), anyString()); try { executeRequest("john@doo.com", "Test Message from SonarQube", "This is a test message from SonarQube at http://localhost:9000"); fail(); } catch (BadRequestException e) { assertThat(e.errors()).containsExactly( "root cause", "parent cause", "child cause", "last message"); } }
@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")); } }