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;
}
}
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");
}
}
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);
}
}
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;
}
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);
}
Aggregations