@SuppressWarnings("OverloadedVarargsMethod") @Override public void send(MimeMessage... mimeMessages) throws MailException { Map<Object, Exception> failedMessages = new HashMap<>(); for (MimeMessage mimeMessage : mimeMessages) { try { RawMessage rm = createRawMessage(mimeMessage); SendRawEmailResult sendRawEmailResult = getEmailService().sendRawEmail(new SendRawEmailRequest(rm)); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Message with id: {} successfully send", sendRawEmailResult.getMessageId()); } } catch (Exception e) { //Ignore Exception because we are collecting and throwing all if any //noinspection ThrowableResultOfMethodCallIgnored failedMessages.put(mimeMessage, e); } } if (!failedMessages.isEmpty()) { throw new MailSendException(failedMessages); } }
@Test public void testSendMultipleMailsWithException() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); MimeMessage failureMail = createMimeMessage(); when(emailService.sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class))). thenReturn(new SendRawEmailResult()). thenThrow(new AmazonClientException("error")). thenReturn(new SendRawEmailResult()); try { mailSender.send(createMimeMessage(), failureMail, createMimeMessage()); fail("Exception expected due to error while sending mail"); } catch (MailSendException e) { assertEquals(1, e.getFailedMessages().size()); assertTrue(e.getFailedMessages().containsKey(failureMail)); } }
@Override public SendRawEmailResult sendRawEmail(SendRawEmailRequest sendRawEmailRequest) throws AmazonServiceException, AmazonClientException { this.sendRawEmailRequest = sendRawEmailRequest; SendRawEmailResult result = new SendRawEmailResult(); result.setMessageId("1"); return result; }
@Test public void testSendMimeMessage() throws MessagingException, IOException { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); ArgumentCaptor<SendRawEmailRequest> request = ArgumentCaptor.forClass(SendRawEmailRequest.class); when(emailService.sendRawEmail(request.capture())).thenReturn(new SendRawEmailResult().withMessageId("123")); MimeMessage mimeMessage = createMimeMessage(); mailSender.send(mimeMessage); SendRawEmailRequest rawEmailRequest = request.getValue(); assertTrue(Arrays.equals(getMimeMessageAsByteArray(mimeMessage), rawEmailRequest.getRawMessage().getData().array())); }
@Test public void testSendMultipleMimeMessages() throws Exception { AmazonSimpleEmailService emailService = mock(AmazonSimpleEmailService.class); JavaMailSender mailSender = new SimpleEmailServiceJavaMailSender(emailService); when(emailService.sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class))).thenReturn(new SendRawEmailResult().withMessageId("123")); mailSender.send(createMimeMessage(), createMimeMessage()); verify(emailService, times(2)).sendRawEmail(ArgumentMatchers.isA(SendRawEmailRequest.class)); }
/** * Method to send a text email with attachments or HTML emails with * attachments (inline or not). * @param m email message to be sent. * @return id of message that has been sent. * @throws MailNotSentException if mail couldn't be sent. */ private String sendRawEmail(EmailMessage<MimeMessage> m) throws MailNotSentException { String messageId; long currentTimestamp = System.currentTimeMillis(); prepareClient(); if (!mEnabled) { //don't send message if not enabled return null; } try { synchronized (this) { //prevents throttling and excessive memory usage checkQuota(currentTimestamp); //if no subject, set to empty string to avoid errors if (m.getSubject() == null) { m.setSubject(""); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //convert message into mime multi part and write it to output //stream into memory Session session = Session.getInstance(new Properties()); MimeMessage mimeMessage = new MimeMessage(session); if (m.getSubject() != null) { mimeMessage.setSubject(m.getSubject(), "utf-8"); } m.buildContent(mimeMessage); mimeMessage.writeTo(outputStream); //m.buildMultipart().writeTo(outputStream); RawMessage rawMessage = new RawMessage(ByteBuffer.wrap( outputStream.toByteArray())); SendRawEmailRequest rawRequest = new SendRawEmailRequest( rawMessage); rawRequest.setDestinations(m.getTo()); rawRequest.setSource(mMailFromAddress); SendRawEmailResult result = mClient.sendRawEmail(rawRequest); messageId = result.getMessageId(); //update timestamp of last sent email mLastSentMailTimestamp = System.currentTimeMillis(); //wait to avoid throwttling exceptions to avoid making any //further requests this.wait(mWaitIntervalMillis); } } catch (Throwable t) { throw new MailNotSentException(t); } return messageId; }