Search in sources :

Example 1 with EmailException

use of org.apache.commons.mail.EmailException in project sonarqube by SonarSource.

the class EmailNotificationChannel method sendTestEmail.

/**
   * Send test email.
   *
   * @throws EmailException when unable to send
   */
public void sendTestEmail(String toAddress, String subject, String message) throws EmailException {
    try {
        EmailMessage emailMessage = new EmailMessage();
        emailMessage.setTo(toAddress);
        emailMessage.setSubject(subject);
        emailMessage.setMessage(message);
        send(emailMessage);
    } catch (EmailException e) {
        LOG.debug("Fail to send test email to: " + toAddress, e);
        throw e;
    }
}
Also used : EmailMessage(org.sonar.plugins.emailnotifications.api.EmailMessage) EmailException(org.apache.commons.mail.EmailException)

Example 2 with EmailException

use of org.apache.commons.mail.EmailException in project sonarqube by SonarSource.

the class SendActionTest method fail_with_BadRequestException_when_EmailException_is_generated.

@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");
    }
}
Also used : EmailException(org.apache.commons.mail.EmailException) BadRequestException(org.sonar.server.exceptions.BadRequestException) Test(org.junit.Test)

Example 3 with EmailException

use of org.apache.commons.mail.EmailException in project pinot by linkedin.

the class AlertTaskRunner method sendFailureEmail.

private void sendFailureEmail(Throwable t) throws JobExecutionException {
    HtmlEmail email = new HtmlEmail();
    String collection = alertConfig.getCollection();
    String metric = alertConfig.getMetric();
    String subject = String.format("[ThirdEye Anomaly Detector] FAILED ALERT ID=%d (%s:%s)", alertConfig.getId(), collection, metric);
    String textBody = String.format("%s%n%nException:%s", alertConfig.toString(), ExceptionUtils.getStackTrace(t));
    try {
        EmailHelper.sendEmailWithTextBody(email, thirdeyeConfig.getSmtpConfiguration(), subject, textBody, thirdeyeConfig.getFailureFromAddress(), thirdeyeConfig.getFailureToAddress());
    } catch (EmailException e) {
        throw new JobExecutionException(e);
    }
}
Also used : JobExecutionException(org.quartz.JobExecutionException) HtmlEmail(org.apache.commons.mail.HtmlEmail) EmailException(org.apache.commons.mail.EmailException)

Example 4 with EmailException

use of org.apache.commons.mail.EmailException in project openhab1-addons by openhab.

the class Mail method sendMail.

/**
     * Sends an email with attachment(s) via SMTP
     *
     * @param to the email address of the recipient
     * @param subject the subject of the email
     * @param message the body of the email
     * @param attachmentUrlList a list of URL strings of the contents to send as attachments
     *
     * @return <code>true</code>, if sending the email has been successful and
     *         <code>false</code> in all other cases.
     */
@ActionDoc(text = "Sends an email with attachment via SMTP")
public static boolean sendMail(@ParamDoc(name = "to") String to, @ParamDoc(name = "subject") String subject, @ParamDoc(name = "message") String message, @ParamDoc(name = "attachmentUrlList") List<String> attachmentUrlList) {
    boolean success = false;
    if (MailActionService.isProperlyConfigured) {
        Email email = new SimpleEmail();
        if (attachmentUrlList != null && !attachmentUrlList.isEmpty()) {
            email = new MultiPartEmail();
            for (String attachmentUrl : attachmentUrlList) {
                // Create the attachment
                try {
                    EmailAttachment attachment = new EmailAttachment();
                    attachment.setURL(new URL(attachmentUrl));
                    attachment.setDisposition(EmailAttachment.ATTACHMENT);
                    String fileName = attachmentUrl.replaceFirst(".*/([^/?]+).*", "$1");
                    attachment.setName(isNotBlank(fileName) ? fileName : "Attachment");
                    ((MultiPartEmail) email).attach(attachment);
                } catch (MalformedURLException e) {
                    logger.error("Invalid attachment url.", e);
                } catch (EmailException e) {
                    logger.error("Error adding attachment to email.", e);
                }
            }
        }
        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setStartTLSEnabled(startTLSEnabled);
        email.setSSLOnConnect(sslOnConnect);
        if (isNotBlank(username)) {
            if (popBeforeSmtp) {
                email.setPopBeforeSmtp(true, hostname, username, password);
            } else {
                email.setAuthenticator(new DefaultAuthenticator(username, password));
            }
        }
        try {
            if (isNotBlank(charset)) {
                email.setCharset(charset);
            }
            email.setFrom(from);
            String[] toList = to.split(";");
            for (String toAddress : toList) {
                email.addTo(toAddress);
            }
            if (!isEmpty(subject)) {
                email.setSubject(subject);
            }
            if (!isEmpty(message)) {
                email.setMsg(message);
            }
            email.send();
            logger.debug("Sent email to '{}' with subject '{}'.", to, subject);
            success = true;
        } catch (EmailException e) {
            logger.error("Could not send e-mail to '" + to + "'.", e);
        }
    } else {
        logger.error("Cannot send e-mail because of missing configuration settings. The current settings are: " + "Host: '{}', port '{}', from '{}', startTLSEnabled: {}, sslOnConnect: {}, username: '{}', password '{}'", new Object[] { hostname, String.valueOf(port), from, String.valueOf(startTLSEnabled), String.valueOf(sslOnConnect), username, password });
    }
    return success;
}
Also used : MalformedURLException(java.net.MalformedURLException) Email(org.apache.commons.mail.Email) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) MultiPartEmail(org.apache.commons.mail.MultiPartEmail) EmailAttachment(org.apache.commons.mail.EmailAttachment) EmailException(org.apache.commons.mail.EmailException) DefaultAuthenticator(org.apache.commons.mail.DefaultAuthenticator) SimpleEmail(org.apache.commons.mail.SimpleEmail) URL(java.net.URL) ActionDoc(org.openhab.core.scriptengine.action.ActionDoc)

Example 5 with EmailException

use of org.apache.commons.mail.EmailException in project killbill by killbill.

the class DefaultEmailSender method sendPlainTextEmail.

@Override
public void sendPlainTextEmail(final List<String> to, final List<String> cc, final String subject, final String body) throws IOException, EmailApiException {
    final SimpleEmail email = new SimpleEmail();
    try {
        email.setMsg(body);
    } catch (EmailException e) {
        throw new EmailApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
    }
    sendEmail(to, cc, subject, email);
}
Also used : EmailException(org.apache.commons.mail.EmailException) SimpleEmail(org.apache.commons.mail.SimpleEmail)

Aggregations

EmailException (org.apache.commons.mail.EmailException)29 HtmlEmail (org.apache.commons.mail.HtmlEmail)18 Email (org.apache.commons.mail.Email)7 MultiPartEmail (org.apache.commons.mail.MultiPartEmail)7 EmailAttachment (org.apache.commons.mail.EmailAttachment)5 ExecutionException (java.util.concurrent.ExecutionException)4 SimpleEmail (org.apache.commons.mail.SimpleEmail)4 RenderResult (cn.bran.japid.template.RenderResult)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 InternetAddress (javax.mail.internet.InternetAddress)3 ImageHtmlEmail (org.apache.commons.mail.ImageHtmlEmail)3 VelocityContext (org.apache.velocity.VelocityContext)3 MailException (cn.bran.play.exceptions.MailException)2 Status (com.ctrip.platform.dal.daogen.domain.Status)2