Java Code Examples for javax.mail.Message#getAllRecipients()

The following examples show how to use javax.mail.Message#getAllRecipients() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaMailSenderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void sendMessage(Message message, Address[] addresses) throws MessagingException {
	if ("fail".equals(message.getSubject())) {
		throw new MessagingException("failed");
	}
	if (addresses == null || (message.getAllRecipients() == null ? addresses.length > 0 :
			!ObjectUtils.nullSafeEquals(addresses, message.getAllRecipients()))) {
		throw new MessagingException("addresses not correct");
	}
	if (message.getSentDate() == null) {
		throw new MessagingException("No sentDate specified");
	}
	if (message.getSubject() != null && message.getSubject().contains("custom")) {
		assertEquals(new GregorianCalendar(2005, 3, 1).getTime(), message.getSentDate());
	}
	this.sentMessages.add(message);
}
 
Example 2
Source File: JavaMailSenderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void sendMessage(Message message, Address[] addresses) throws MessagingException {
	if ("fail".equals(message.getSubject())) {
		throw new MessagingException("failed");
	}
	if (addresses == null || (message.getAllRecipients() == null ? addresses.length > 0 :
			!ObjectUtils.nullSafeEquals(addresses, message.getAllRecipients()))) {
		throw new MessagingException("addresses not correct");
	}
	if (message.getSentDate() == null) {
		throw new MessagingException("No sentDate specified");
	}
	if (message.getSubject() != null && message.getSubject().contains("custom")) {
		assertEquals(new GregorianCalendar(2005, 3, 1).getTime(), message.getSentDate());
	}
	this.sentMessages.add(message);
}
 
Example 3
Source File: EmailMessagingService.java    From blackduck-alert with Apache License 2.0 5 votes vote down vote up
public void sendMessages(EmailProperties emailProperties, Session session, List<Message> messages) throws AlertException {
    Set<String> errorMessages = new HashSet<>();
    Set<String> invalidRecipients = new HashSet<>();
    for (Message message : messages) {
        Address[] recipients = null;
        try {
            recipients = message.getAllRecipients();
            if (Boolean.valueOf(emailProperties.getJavamailOption(EmailPropertyKeys.JAVAMAIL_AUTH_KEY))) {
                sendAuthenticated(emailProperties, message, session);
            } else {
                Transport.send(message);
            }
        } catch (MessagingException e) {
            if (recipients != null) {
                Stream.of(recipients).map(Address::toString).forEach(invalidRecipients::add);
            }
            errorMessages.add(e.getMessage());
            logger.error("Could not send this email to the following recipients: {}. Reason: {}", recipients, e.getMessage());
        }
    }
    if (!errorMessages.isEmpty()) {
        String errorMessage;
        if (invalidRecipients.isEmpty()) {
            errorMessage = "Errors sending emails. " + StringUtils.join(errorMessages, ", ");
        } else {
            errorMessage = "Error sending emails to the following recipients: " + invalidRecipients;
        }
        throw new AlertException(errorMessage);
    }
}
 
Example 4
Source File: MailUtil.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Send the message
 *
 * @param msg
 *            the message to send
 * @param transport
 *            the transport used to send the message
 * @throws MessagingException
 *             If a messaging error occured
 * @throws AddressException
 *             If invalid address
 */
private static void sendMessage( Message msg, Transport transport ) throws MessagingException
{
    if ( msg.getAllRecipients( ) != null )
    {
        // Send the message
        transport.sendMessage( msg, msg.getAllRecipients( ) );
    }
    else
    {
        throw new AddressException( "Mail adress is null" );
    }
}