use of org.springframework.mail.MailPreparationException in project molgenis by molgenis.
the class ProgressImplTest method jobFailsAndMailFails.
@Test
public void jobFailsAndMailFails() {
doThrow(new MailPreparationException("fail!")).when(mailSender).send(any(SimpleMailMessage.class));
jobExecution.setProgressMessage("Downloading...");
jobExecution.setFailureEmail("test@test");
progress.start();
String exceptionMessage = "x is not a number";
Exception ex = new IllegalArgumentException(exceptionMessage);
progress.failed(ex);
Mockito.verify(mailSender).send(any(SimpleMailMessage.class));
assertEquals(jobExecution.getProgressMessage(), exceptionMessage + " (Mail not sent: fail!)");
}
use of org.springframework.mail.MailPreparationException in project wombat by PLOS.
the class FreemarkerMailServiceImpl method createBodyPart.
private BodyPart createBodyPart(ContentType contentType, Template htmlTemplate, Model context) throws IOException, MessagingException {
BodyPart htmlPage = new MimeBodyPart();
String encoding = getConfiguration().getDefaultEncoding();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(0x100);
Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
htmlTemplate.setOutputEncoding(encoding);
htmlTemplate.setEncoding(encoding);
try {
htmlTemplate.process(context, writer);
} catch (TemplateException e) {
throw new MailPreparationException("Can't generate " + contentType.getMimeType() + " subscription mail", e);
}
htmlPage.setDataHandler(createBodyPartDataHandler(outputStream.toByteArray(), contentType.toString() + "; charset=" + getConfiguration().getDefaultEncoding()));
return htmlPage;
}
use of org.springframework.mail.MailPreparationException in project ma-core-public by infiniteautomation.
the class EmailSender method send.
public void send(InternetAddress from, String[] toAddr, String subject, EmailContent content) {
try {
InternetAddress[] toIAddr = new InternetAddress[toAddr.length];
for (int i = 0; i < toAddr.length; i++) toIAddr[i] = new InternetAddress(toAddr[i]);
send(from, toIAddr, null, null, subject, content);
} catch (AddressException e) {
throw new MailPreparationException(e);
}
}
use of org.springframework.mail.MailPreparationException in project ma-core-public by infiniteautomation.
the class EmailSender method send.
public void send(String fromAddr, String fromPersonal, String[] toAddr, String subject, EmailContent content) {
try {
InternetAddress[] toIAddr = new InternetAddress[toAddr.length];
for (int i = 0; i < toAddr.length; i++) toIAddr[i] = new InternetAddress(toAddr[i]);
send(new InternetAddress(fromAddr, fromPersonal), toIAddr, null, null, subject, content);
} catch (AddressException e) {
throw new MailPreparationException(e);
} catch (UnsupportedEncodingException e) {
throw new MailPreparationException(e);
}
}
use of org.springframework.mail.MailPreparationException in project alfresco-repository by Alfresco.
the class MailActionExecuter method getRecipients.
@SuppressWarnings("unchecked")
private Collection<Pair<String, Locale>> getRecipients(Action ruleAction) {
Map<String, Pair<String, Locale>> recipients = new HashMap<String, Pair<String, Locale>>();
// set recipient
String to = (String) ruleAction.getParameterValue(PARAM_TO);
if (to != null && to.length() != 0) {
Locale locale = null;
if (personExists(to)) {
locale = getLocaleForUser(to);
}
recipients.put(to, new Pair<String, Locale>(to, locale));
} else {
// see if multiple recipients have been supplied - as a list of authorities
Serializable authoritiesValue = ruleAction.getParameterValue(PARAM_TO_MANY);
List<String> authorities = null;
if (authoritiesValue != null) {
if (authoritiesValue instanceof String) {
authorities = new ArrayList<String>(1);
authorities.add((String) authoritiesValue);
} else {
authorities = (List<String>) authoritiesValue;
}
}
if (authorities != null && authorities.size() != 0) {
for (String authority : authorities) {
AuthorityType authType = AuthorityType.getAuthorityType(authority);
if (authType.equals(AuthorityType.USER)) {
// Validate the email, allowing for local email addresses
if ((authority != null) && (authority.length() != 0) && (!recipients.containsKey(authority))) {
if (personExists(authority)) {
String address = getPersonEmail(authority);
if (address != null && address.length() != 0 && validateAddress(address)) {
Locale locale = getLocaleForUser(authority);
recipients.put(authority, new Pair<String, Locale>(address, locale));
} else {
EmailValidator emailValidator = EmailValidator.getInstance(true);
if (validateAddresses && emailValidator.isValid(authority)) {
Locale locale = getLocaleForUser(authority);
recipients.put(authority, new Pair<String, Locale>(authority, locale));
}
}
} else {
recipients.put(authority, new Pair<String, Locale>(authority, null));
}
}
} else if (authType.equals(AuthorityType.GROUP) || authType.equals(AuthorityType.EVERYONE)) {
// Notify all members of the group
Set<String> users;
if (authType.equals(AuthorityType.GROUP)) {
users = authorityService.getContainedAuthorities(AuthorityType.USER, authority, false);
} else {
users = authorityService.getAllAuthorities(AuthorityType.USER);
}
for (String userAuth : users) {
if (recipients.containsKey(userAuth)) {
continue;
}
if (personExists(userAuth)) {
// Check the user name to be a valid email and we don't need to log an error in this case
// ALF-19231
// Validate the email, allowing for local email addresses
String address = getPersonEmail(userAuth);
if (address != null && address.length() != 0 && validateAddress(address)) {
Locale locale = getLocaleForUser(userAuth);
recipients.put(userAuth, new Pair<String, Locale>(address, locale));
} else {
EmailValidator emailValidator = EmailValidator.getInstance(true);
if (validateAddresses && emailValidator.isValid(userAuth)) {
if (userAuth != null && userAuth.length() != 0) {
Locale locale = getLocaleForUser(userAuth);
recipients.put(userAuth, new Pair<String, Locale>(userAuth, locale));
}
}
}
} else {
recipients.put(userAuth, new Pair<String, Locale>(authority, null));
}
}
}
}
if (recipients.size() <= 0) {
// All recipients were invalid
throw new MailPreparationException("All recipients for the mail action were invalid");
}
} else {
// No recipients have been specified
throw new MailPreparationException("No recipient has been specified for the mail action");
}
}
return recipients.values();
}
Aggregations