List of usage examples for javax.mail SendFailedException SendFailedException
public SendFailedException(String s, Exception e)
From source file:org.jasig.ssp.service.impl.MessageServiceImpl.java
@Override @Transactional(readOnly = false)// ww w. j a v a 2s .c o m public boolean sendMessage(@NotNull final Message message) throws ObjectNotFoundException, SendFailedException, UnsupportedEncodingException { LOGGER.info("BEGIN : sendMessage()"); LOGGER.info(addMessageIdToError(message) + "Sending message: {}", message.toString()); try { final MimeMessage mimeMessage = javaMailSender.createMimeMessage(); final MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage); // process FROM addresses InternetAddress from; String appName = configService.getByName("app_title").getValue(); //We used the configured outbound email address for every outgoing message //If a message was initiated by an end user, their name will be attached to the 'from' while //the configured outbound address will be the actual address used for example "Amy Aministrator (SSP) <myconfiguredaddress@foobar.com>" String name = appName + " Administrator"; if (message.getSender() != null && !message.getSender().getEmailAddresses().isEmpty() && !message.getSender().getId().equals(Person.SYSTEM_ADMINISTRATOR_ID)) { InternetAddress[] froms = getEmailAddresses(message.getSender(), "from:", message.getId()); if (froms.length > 0) { name = message.getSender().getFullName() + " (" + appName + ")"; } } from = new InternetAddress(configService.getByName("outbound_email_address").getValue(), name); if (!this.validateEmail(from.getAddress())) { throw new AddressException("Invalid from: email address [" + from.getAddress() + "]"); } mimeMessageHelper.setFrom(from); message.setSentFromAddress(from.toString()); mimeMessageHelper.setReplyTo(from); message.setSentReplyToAddress(from.toString()); // process TO addresses InternetAddress[] tos = null; if (message.getRecipient() != null && message.getRecipient().hasEmailAddresses()) { // NOPMD by jon.adams tos = getEmailAddresses(message.getRecipient(), "to:", message.getId()); } else { tos = getEmailAddresses(message.getRecipientEmailAddress(), "to:", message.getId()); } if (tos.length > 0) { mimeMessageHelper.setTo(tos); message.setSentToAddresses(StringUtils.join(tos, ",").trim()); } else { StringBuilder errorMsg = new StringBuilder(); errorMsg.append(addMessageIdToError(message) + " Message " + message.toString() + " could not be sent. No valid recipient email address found: '"); if (message.getRecipient() != null) { errorMsg.append(message.getRecipient().getPrimaryEmailAddress()); } else { errorMsg.append(message.getRecipientEmailAddress()); } LOGGER.error(errorMsg.toString()); throw new MessagingException(errorMsg.toString()); } // process BCC addresses try { InternetAddress[] bccs = getEmailAddresses(getBcc(), "bcc:", message.getId()); if (bccs.length > 0) { mimeMessageHelper.setBcc(bccs); message.setSentBccAddresses(StringUtils.join(bccs, ",").trim()); } } catch (Exception exp) { LOGGER.warn("Unrecoverable errors were generated adding carbon copy to message: " + message.getId() + "Attempt to send message still initiated.", exp); } // process CC addresses try { InternetAddress[] carbonCopies = getEmailAddresses(message.getCarbonCopy(), "cc:", message.getId()); if (carbonCopies.length > 0) { mimeMessageHelper.setCc(carbonCopies); message.setSentCcAddresses(StringUtils.join(carbonCopies, ",").trim()); } } catch (Exception exp) { LOGGER.warn("Unrecoverable errors were generated adding bcc to message: " + message.getId() + "Attempt to send message still initiated.", exp); } mimeMessageHelper.setSubject(message.getSubject()); mimeMessageHelper.setText(message.getBody()); mimeMessage.setContent(message.getBody(), "text/html"); send(mimeMessage); message.setSentDate(new Date()); messageDao.save(message); } catch (final MessagingException e) { LOGGER.error("ERROR : sendMessage() : {}", e); handleSendMessageError(message); throw new SendFailedException(addMessageIdToError(message) + "The message parameters were invalid.", e); } LOGGER.info("END : sendMessage()"); return true; }
From source file:org.jasig.ssp.service.impl.MessageServiceImpl.java
private void send(final MimeMessage mimeMessage) throws SendFailedException { if (shouldSendMail()) { LOGGER.debug("_ : JavaMailSender.send()"); try {// w w w . ja v a2 s .c o m javaMailSender.send(mimeMessage); } catch (final MailSendException e) { try { LOGGER.warn("Send failed, going to wait and try again"); Thread.sleep(20 * 1000L); javaMailSender.send(mimeMessage); } catch (final InterruptedException e1) { LOGGER.error("Thread error", e1); } catch (final MailSendException e2) { throw new SendFailedException("Unable to send message.", e2); } } } else { LOGGER.warn( "_ : JavaMailSender was not called; message was marked sent but was not actually sent. To enable mail, update the configuration of the app."); } }