Search in sources :

Example 6 with MailPreparationException

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!)");
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailPreparationException(org.springframework.mail.MailPreparationException) Test(org.testng.annotations.Test) AbstractMolgenisSpringTest(org.molgenis.data.AbstractMolgenisSpringTest)

Example 7 with MailPreparationException

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;
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) TemplateException(freemarker.template.TemplateException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Example 8 with MailPreparationException

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);
    }
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException)

Example 9 with MailPreparationException

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);
    }
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) InternetAddress(javax.mail.internet.InternetAddress) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with MailPreparationException

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();
}
Also used : Locale(java.util.Locale) MailPreparationException(org.springframework.mail.MailPreparationException) Serializable(java.io.Serializable) EmailValidator(org.apache.commons.validator.routines.EmailValidator) Set(java.util.Set) HashMap(java.util.HashMap) AuthorityType(org.alfresco.service.cmr.security.AuthorityType) Pair(org.alfresco.util.Pair)

Aggregations

MailPreparationException (org.springframework.mail.MailPreparationException)10 HashMap (java.util.HashMap)3 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)3 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)3 Test (org.testng.annotations.Test)3 Serializable (java.io.Serializable)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 Set (java.util.Set)2 AddressException (javax.mail.internet.AddressException)2 InternetAddress (javax.mail.internet.InternetAddress)2 AuthorityType (org.alfresco.service.cmr.security.AuthorityType)2 MailAuthenticationException (org.springframework.mail.MailAuthenticationException)2 MailException (org.springframework.mail.MailException)2 MailParseException (org.springframework.mail.MailParseException)2 MailSendException (org.springframework.mail.MailSendException)2 TemplateException (freemarker.template.TemplateException)1 AuthenticationFailedException (jakarta.mail.AuthenticationFailedException)1 MessagingException (jakarta.mail.MessagingException)1