@Test public void testExecuteThrowsException() throws Exception { prepareTransportClassForStaticMock(); Mockito.doNothing().when(mimeBodyPartMock).setContent(BODY, TEXT_PLAIN + CHARSET_CST + DEFAULT_CHARACTERSET); PowerMockito.whenNew(FileDataSource.class).withArguments(anyString()).thenReturn(fileDataSourceMock); Mockito.doReturn(fileMock).when(fileDataSourceMock).getFile(); Mockito.doReturn(false).when(fileMock).exists(); PowerMockito.whenNew(DataHandler.class).withArguments(fileDataSourceMock).thenReturn(dataHandlerMock); doNothing().when(mimeBodyPartMock).setDataHandler(dataHandlerMock); doThrow(new MessagingException("IOException")).when(mimeBodyPartMock).setDataHandler(dataHandlerMock); inputBuilder.hostname(SMTP_HOSTANME) .port(PORT) .from(FROM) .to(TO + DEFAULT_DELIMITER + TO2) .cc(CC) .bcc(BCC) .subject(SUBJECT) .body(BODY) .attachments(ATTACHMENTS); exception.expect(Exception.class); exception.expectMessage(CANNOT_ATTACH); sendMailService.execute(inputBuilder.build()); }
public Map<String, String> execute(SendMailInput sendMailInput) throws Exception { Map<String, String> result = new HashMap<>(); Transport transport = null; try { this.input = sendMailInput; java.util.Properties props = new java.util.Properties(); props.put(String.format(PropNames.MAIL_HOST, Constants.SMTP), input.getHostname()); props.put(String.format(PropNames.MAIL_PORT, Constants.SMTP), StringUtils.EMPTY + input.getPort()); if (null != input.getUsername() && input.getUsername().length() > 0) { props.put(String.format(PropNames.MAIL_USER, input.getProtocol()), input.getUsername()); props.put(String.format(PropNames.MAIL_PASSWORD, input.getProtocol()), input.getPassword()); props.put(String.format(PropNames.MAIL_AUTH, input.getProtocol()), String.valueOf(true)); } if (input.isEnableTLS()) { SSLUtils.addSSLSettings(true, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY); SSLUtils.configureWithTLS(props, input); } else { props.put(String.format(PropNames.MAIL_STARTTLS_ENABLE, input.getProtocol()), String.valueOf(false)); } if (input.getTimeout() > 0) { props.put(String.format(PropNames.MAIL_TIMEOUT, input.getProtocol()), input.getTimeout()); } if (StringUtils.isNotEmpty(input.getProxyHost())) { ProxyUtils.setPropertiesProxy(props, input); } if (input.isEncryptedMessage()) { addEncryptionSettings(); } MimeMultipart multipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); if (input.isHtmlEmail()) { processHTMLBodyWithBASE64Images(multipart); mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_HTML + ";charset=" + input.getCharacterSet()); } else { mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_PLAIN + ";charset=" + input.getCharacterSet()); } mimeBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); mimeBodyPart = encryptMimeBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart); if (null != input.getAttachments() && input.getAttachments().length() > 0) { for (String attachment : input.getAttachments().split(Pattern.quote(input.getDelimiter()))) { FileDataSource source = new FileDataSource(attachment); if (!source.getFile().exists()) { throw new FileNotFoundException("Cannot attach " + attachment); } if (!Files.isReadable(source.getFile().toPath())) { throw new InvalidParameterException(attachment + " don't have read permision"); } MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(MimeUtility.encodeText( attachment.substring(attachment.lastIndexOf(java.io.File.separator) + 1), input.getCharacterSet(), input.getEncodingScheme()) ); messageBodyPart = encryptMimeBodyPart(messageBodyPart); multipart.addBodyPart(messageBodyPart); } } Session session = Session.getInstance(props, null); SMTPMessage msg = new SMTPMessage(session); msg.setContent(multipart); msg.setFrom(new InternetAddress(input.getFrom())); msg.setSubject(MimeUtility.encodeText( input.getSubject().replaceAll("[\\n\\r]", " "), input.getCharacterSet(), input.getEncodingScheme()) ); if (input.isReadReceipt()) { msg.setNotifyOptions(NOTIFY_DELAY + NOTIFY_FAILURE + NOTIFY_SUCCESS); } String[] recipients = input.getTo().split(Pattern.quote(input.getDelimiter())); InternetAddress[] toRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { toRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.TO, toRecipients); if (input.getCc() != null && input.getCc().trim().length() > 0) { recipients = input.getCc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] ccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { ccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.CC, ccRecipients); } } if (input.getBcc() != null && input.getBcc().trim().length() > 0) { recipients = input.getBcc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] bccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { bccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } } if (input.getHeaderNames() != null && !input.getHeaderNames().isEmpty()) { msg = addHeadersToSMTPMessage(msg, input.getHeaderNames(), input.getHeaderValues()); } msg.saveChanges(); if (StringUtils.isNotEmpty(input.getUsername())) { transport = session.getTransport(input.getProtocol()); transport.connect(input.getHostname(), input.getPort(), input.getUsername(), input.getPassword()); transport.sendMessage(msg, msg.getAllRecipients()); } else { Transport.send(msg); } result.put(OutputNames.RETURN_RESULT, Constants.MAIL_WAS_SENT); result.put(OutputNames.RETURN_CODE, ReturnCodes.SUCCESS); } finally { if (null != transport) { transport.close(); } } return result; }
SendMailService { public Map<String, String> execute(SendMailInput sendMailInput) throws Exception { Map<String, String> result = new HashMap<>(); Transport transport = null; try { this.input = sendMailInput; java.util.Properties props = new java.util.Properties(); props.put(String.format(PropNames.MAIL_HOST, Constants.SMTP), input.getHostname()); props.put(String.format(PropNames.MAIL_PORT, Constants.SMTP), StringUtils.EMPTY + input.getPort()); if (null != input.getUsername() && input.getUsername().length() > 0) { props.put(String.format(PropNames.MAIL_USER, input.getProtocol()), input.getUsername()); props.put(String.format(PropNames.MAIL_PASSWORD, input.getProtocol()), input.getPassword()); props.put(String.format(PropNames.MAIL_AUTH, input.getProtocol()), String.valueOf(true)); } if (input.isEnableTLS()) { SSLUtils.addSSLSettings(true, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY); SSLUtils.configureWithTLS(props, input); } else { props.put(String.format(PropNames.MAIL_STARTTLS_ENABLE, input.getProtocol()), String.valueOf(false)); } if (input.getTimeout() > 0) { props.put(String.format(PropNames.MAIL_TIMEOUT, input.getProtocol()), input.getTimeout()); } if (StringUtils.isNotEmpty(input.getProxyHost())) { ProxyUtils.setPropertiesProxy(props, input); } if (input.isEncryptedMessage()) { addEncryptionSettings(); } MimeMultipart multipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); if (input.isHtmlEmail()) { processHTMLBodyWithBASE64Images(multipart); mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_HTML + ";charset=" + input.getCharacterSet()); } else { mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_PLAIN + ";charset=" + input.getCharacterSet()); } mimeBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); mimeBodyPart = encryptMimeBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart); if (null != input.getAttachments() && input.getAttachments().length() > 0) { for (String attachment : input.getAttachments().split(Pattern.quote(input.getDelimiter()))) { FileDataSource source = new FileDataSource(attachment); if (!source.getFile().exists()) { throw new FileNotFoundException("Cannot attach " + attachment); } if (!Files.isReadable(source.getFile().toPath())) { throw new InvalidParameterException(attachment + " don't have read permision"); } MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(MimeUtility.encodeText( attachment.substring(attachment.lastIndexOf(java.io.File.separator) + 1), input.getCharacterSet(), input.getEncodingScheme()) ); messageBodyPart = encryptMimeBodyPart(messageBodyPart); multipart.addBodyPart(messageBodyPart); } } Session session = Session.getInstance(props, null); SMTPMessage msg = new SMTPMessage(session); msg.setContent(multipart); msg.setFrom(new InternetAddress(input.getFrom())); msg.setSubject(MimeUtility.encodeText( input.getSubject().replaceAll("[\\n\\r]", " "), input.getCharacterSet(), input.getEncodingScheme()) ); if (input.isReadReceipt()) { msg.setNotifyOptions(NOTIFY_DELAY + NOTIFY_FAILURE + NOTIFY_SUCCESS); } String[] recipients = input.getTo().split(Pattern.quote(input.getDelimiter())); InternetAddress[] toRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { toRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.TO, toRecipients); if (input.getCc() != null && input.getCc().trim().length() > 0) { recipients = input.getCc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] ccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { ccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.CC, ccRecipients); } } if (input.getBcc() != null && input.getBcc().trim().length() > 0) { recipients = input.getBcc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] bccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { bccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } } if (input.getHeaderNames() != null && !input.getHeaderNames().isEmpty()) { msg = addHeadersToSMTPMessage(msg, input.getHeaderNames(), input.getHeaderValues()); } msg.saveChanges(); if (StringUtils.isNotEmpty(input.getUsername())) { transport = session.getTransport(input.getProtocol()); transport.connect(input.getHostname(), input.getPort(), input.getUsername(), input.getPassword()); transport.sendMessage(msg, msg.getAllRecipients()); } else { Transport.send(msg); } result.put(OutputNames.RETURN_RESULT, Constants.MAIL_WAS_SENT); result.put(OutputNames.RETURN_CODE, ReturnCodes.SUCCESS); } finally { if (null != transport) { transport.close(); } } return result; } }
SendMailService { public Map<String, String> execute(SendMailInput sendMailInput) throws Exception { Map<String, String> result = new HashMap<>(); Transport transport = null; try { this.input = sendMailInput; java.util.Properties props = new java.util.Properties(); props.put(String.format(PropNames.MAIL_HOST, Constants.SMTP), input.getHostname()); props.put(String.format(PropNames.MAIL_PORT, Constants.SMTP), StringUtils.EMPTY + input.getPort()); if (null != input.getUsername() && input.getUsername().length() > 0) { props.put(String.format(PropNames.MAIL_USER, input.getProtocol()), input.getUsername()); props.put(String.format(PropNames.MAIL_PASSWORD, input.getProtocol()), input.getPassword()); props.put(String.format(PropNames.MAIL_AUTH, input.getProtocol()), String.valueOf(true)); } if (input.isEnableTLS()) { SSLUtils.addSSLSettings(true, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY); SSLUtils.configureWithTLS(props, input); } else { props.put(String.format(PropNames.MAIL_STARTTLS_ENABLE, input.getProtocol()), String.valueOf(false)); } if (input.getTimeout() > 0) { props.put(String.format(PropNames.MAIL_TIMEOUT, input.getProtocol()), input.getTimeout()); } if (StringUtils.isNotEmpty(input.getProxyHost())) { ProxyUtils.setPropertiesProxy(props, input); } if (input.isEncryptedMessage()) { addEncryptionSettings(); } MimeMultipart multipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); if (input.isHtmlEmail()) { processHTMLBodyWithBASE64Images(multipart); mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_HTML + ";charset=" + input.getCharacterSet()); } else { mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_PLAIN + ";charset=" + input.getCharacterSet()); } mimeBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); mimeBodyPart = encryptMimeBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart); if (null != input.getAttachments() && input.getAttachments().length() > 0) { for (String attachment : input.getAttachments().split(Pattern.quote(input.getDelimiter()))) { FileDataSource source = new FileDataSource(attachment); if (!source.getFile().exists()) { throw new FileNotFoundException("Cannot attach " + attachment); } if (!Files.isReadable(source.getFile().toPath())) { throw new InvalidParameterException(attachment + " don't have read permision"); } MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(MimeUtility.encodeText( attachment.substring(attachment.lastIndexOf(java.io.File.separator) + 1), input.getCharacterSet(), input.getEncodingScheme()) ); messageBodyPart = encryptMimeBodyPart(messageBodyPart); multipart.addBodyPart(messageBodyPart); } } Session session = Session.getInstance(props, null); SMTPMessage msg = new SMTPMessage(session); msg.setContent(multipart); msg.setFrom(new InternetAddress(input.getFrom())); msg.setSubject(MimeUtility.encodeText( input.getSubject().replaceAll("[\\n\\r]", " "), input.getCharacterSet(), input.getEncodingScheme()) ); if (input.isReadReceipt()) { msg.setNotifyOptions(NOTIFY_DELAY + NOTIFY_FAILURE + NOTIFY_SUCCESS); } String[] recipients = input.getTo().split(Pattern.quote(input.getDelimiter())); InternetAddress[] toRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { toRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.TO, toRecipients); if (input.getCc() != null && input.getCc().trim().length() > 0) { recipients = input.getCc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] ccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { ccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.CC, ccRecipients); } } if (input.getBcc() != null && input.getBcc().trim().length() > 0) { recipients = input.getBcc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] bccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { bccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } } if (input.getHeaderNames() != null && !input.getHeaderNames().isEmpty()) { msg = addHeadersToSMTPMessage(msg, input.getHeaderNames(), input.getHeaderValues()); } msg.saveChanges(); if (StringUtils.isNotEmpty(input.getUsername())) { transport = session.getTransport(input.getProtocol()); transport.connect(input.getHostname(), input.getPort(), input.getUsername(), input.getPassword()); transport.sendMessage(msg, msg.getAllRecipients()); } else { Transport.send(msg); } result.put(OutputNames.RETURN_RESULT, Constants.MAIL_WAS_SENT); result.put(OutputNames.RETURN_CODE, ReturnCodes.SUCCESS); } finally { if (null != transport) { transport.close(); } } return result; }  }
SendMailService { public Map<String, String> execute(SendMailInput sendMailInput) throws Exception { Map<String, String> result = new HashMap<>(); Transport transport = null; try { this.input = sendMailInput; java.util.Properties props = new java.util.Properties(); props.put(String.format(PropNames.MAIL_HOST, Constants.SMTP), input.getHostname()); props.put(String.format(PropNames.MAIL_PORT, Constants.SMTP), StringUtils.EMPTY + input.getPort()); if (null != input.getUsername() && input.getUsername().length() > 0) { props.put(String.format(PropNames.MAIL_USER, input.getProtocol()), input.getUsername()); props.put(String.format(PropNames.MAIL_PASSWORD, input.getProtocol()), input.getPassword()); props.put(String.format(PropNames.MAIL_AUTH, input.getProtocol()), String.valueOf(true)); } if (input.isEnableTLS()) { SSLUtils.addSSLSettings(true, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY); SSLUtils.configureWithTLS(props, input); } else { props.put(String.format(PropNames.MAIL_STARTTLS_ENABLE, input.getProtocol()), String.valueOf(false)); } if (input.getTimeout() > 0) { props.put(String.format(PropNames.MAIL_TIMEOUT, input.getProtocol()), input.getTimeout()); } if (StringUtils.isNotEmpty(input.getProxyHost())) { ProxyUtils.setPropertiesProxy(props, input); } if (input.isEncryptedMessage()) { addEncryptionSettings(); } MimeMultipart multipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); if (input.isHtmlEmail()) { processHTMLBodyWithBASE64Images(multipart); mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_HTML + ";charset=" + input.getCharacterSet()); } else { mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_PLAIN + ";charset=" + input.getCharacterSet()); } mimeBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); mimeBodyPart = encryptMimeBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart); if (null != input.getAttachments() && input.getAttachments().length() > 0) { for (String attachment : input.getAttachments().split(Pattern.quote(input.getDelimiter()))) { FileDataSource source = new FileDataSource(attachment); if (!source.getFile().exists()) { throw new FileNotFoundException("Cannot attach " + attachment); } if (!Files.isReadable(source.getFile().toPath())) { throw new InvalidParameterException(attachment + " don't have read permision"); } MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(MimeUtility.encodeText( attachment.substring(attachment.lastIndexOf(java.io.File.separator) + 1), input.getCharacterSet(), input.getEncodingScheme()) ); messageBodyPart = encryptMimeBodyPart(messageBodyPart); multipart.addBodyPart(messageBodyPart); } } Session session = Session.getInstance(props, null); SMTPMessage msg = new SMTPMessage(session); msg.setContent(multipart); msg.setFrom(new InternetAddress(input.getFrom())); msg.setSubject(MimeUtility.encodeText( input.getSubject().replaceAll("[\\n\\r]", " "), input.getCharacterSet(), input.getEncodingScheme()) ); if (input.isReadReceipt()) { msg.setNotifyOptions(NOTIFY_DELAY + NOTIFY_FAILURE + NOTIFY_SUCCESS); } String[] recipients = input.getTo().split(Pattern.quote(input.getDelimiter())); InternetAddress[] toRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { toRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.TO, toRecipients); if (input.getCc() != null && input.getCc().trim().length() > 0) { recipients = input.getCc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] ccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { ccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.CC, ccRecipients); } } if (input.getBcc() != null && input.getBcc().trim().length() > 0) { recipients = input.getBcc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] bccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { bccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } } if (input.getHeaderNames() != null && !input.getHeaderNames().isEmpty()) { msg = addHeadersToSMTPMessage(msg, input.getHeaderNames(), input.getHeaderValues()); } msg.saveChanges(); if (StringUtils.isNotEmpty(input.getUsername())) { transport = session.getTransport(input.getProtocol()); transport.connect(input.getHostname(), input.getPort(), input.getUsername(), input.getPassword()); transport.sendMessage(msg, msg.getAllRecipients()); } else { Transport.send(msg); } result.put(OutputNames.RETURN_RESULT, Constants.MAIL_WAS_SENT); result.put(OutputNames.RETURN_CODE, ReturnCodes.SUCCESS); } finally { if (null != transport) { transport.close(); } } return result; }  Map<String, String> execute(SendMailInput sendMailInput); }
SendMailService { public Map<String, String> execute(SendMailInput sendMailInput) throws Exception { Map<String, String> result = new HashMap<>(); Transport transport = null; try { this.input = sendMailInput; java.util.Properties props = new java.util.Properties(); props.put(String.format(PropNames.MAIL_HOST, Constants.SMTP), input.getHostname()); props.put(String.format(PropNames.MAIL_PORT, Constants.SMTP), StringUtils.EMPTY + input.getPort()); if (null != input.getUsername() && input.getUsername().length() > 0) { props.put(String.format(PropNames.MAIL_USER, input.getProtocol()), input.getUsername()); props.put(String.format(PropNames.MAIL_PASSWORD, input.getProtocol()), input.getPassword()); props.put(String.format(PropNames.MAIL_AUTH, input.getProtocol()), String.valueOf(true)); } if (input.isEnableTLS()) { SSLUtils.addSSLSettings(true, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY); SSLUtils.configureWithTLS(props, input); } else { props.put(String.format(PropNames.MAIL_STARTTLS_ENABLE, input.getProtocol()), String.valueOf(false)); } if (input.getTimeout() > 0) { props.put(String.format(PropNames.MAIL_TIMEOUT, input.getProtocol()), input.getTimeout()); } if (StringUtils.isNotEmpty(input.getProxyHost())) { ProxyUtils.setPropertiesProxy(props, input); } if (input.isEncryptedMessage()) { addEncryptionSettings(); } MimeMultipart multipart = new MimeMultipart(); MimeBodyPart mimeBodyPart = new MimeBodyPart(); if (input.isHtmlEmail()) { processHTMLBodyWithBASE64Images(multipart); mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_HTML + ";charset=" + input.getCharacterSet()); } else { mimeBodyPart.setContent(input.getBody(), MimeTypes.TEXT_PLAIN + ";charset=" + input.getCharacterSet()); } mimeBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); mimeBodyPart = encryptMimeBodyPart(mimeBodyPart); multipart.addBodyPart(mimeBodyPart); if (null != input.getAttachments() && input.getAttachments().length() > 0) { for (String attachment : input.getAttachments().split(Pattern.quote(input.getDelimiter()))) { FileDataSource source = new FileDataSource(attachment); if (!source.getFile().exists()) { throw new FileNotFoundException("Cannot attach " + attachment); } if (!Files.isReadable(source.getFile().toPath())) { throw new InvalidParameterException(attachment + " don't have read permision"); } MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setHeader(Encodings.CONTENT_TRANSFER_ENCODING, input.getContentTransferEncoding()); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(MimeUtility.encodeText( attachment.substring(attachment.lastIndexOf(java.io.File.separator) + 1), input.getCharacterSet(), input.getEncodingScheme()) ); messageBodyPart = encryptMimeBodyPart(messageBodyPart); multipart.addBodyPart(messageBodyPart); } } Session session = Session.getInstance(props, null); SMTPMessage msg = new SMTPMessage(session); msg.setContent(multipart); msg.setFrom(new InternetAddress(input.getFrom())); msg.setSubject(MimeUtility.encodeText( input.getSubject().replaceAll("[\\n\\r]", " "), input.getCharacterSet(), input.getEncodingScheme()) ); if (input.isReadReceipt()) { msg.setNotifyOptions(NOTIFY_DELAY + NOTIFY_FAILURE + NOTIFY_SUCCESS); } String[] recipients = input.getTo().split(Pattern.quote(input.getDelimiter())); InternetAddress[] toRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { toRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.TO, toRecipients); if (input.getCc() != null && input.getCc().trim().length() > 0) { recipients = input.getCc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] ccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { ccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.CC, ccRecipients); } } if (input.getBcc() != null && input.getBcc().trim().length() > 0) { recipients = input.getBcc().split(Pattern.quote(input.getDelimiter())); if (recipients.length > 0) { InternetAddress[] bccRecipients = new InternetAddress[recipients.length]; for (int count = 0; count < recipients.length; count++) { bccRecipients[count] = new InternetAddress(recipients[count]); } msg.setRecipients(Message.RecipientType.BCC, bccRecipients); } } if (input.getHeaderNames() != null && !input.getHeaderNames().isEmpty()) { msg = addHeadersToSMTPMessage(msg, input.getHeaderNames(), input.getHeaderValues()); } msg.saveChanges(); if (StringUtils.isNotEmpty(input.getUsername())) { transport = session.getTransport(input.getProtocol()); transport.connect(input.getHostname(), input.getPort(), input.getUsername(), input.getPassword()); transport.sendMessage(msg, msg.getAllRecipients()); } else { Transport.send(msg); } result.put(OutputNames.RETURN_RESULT, Constants.MAIL_WAS_SENT); result.put(OutputNames.RETURN_CODE, ReturnCodes.SUCCESS); } finally { if (null != transport) { transport.close(); } } return result; }  Map<String, String> execute(SendMailInput sendMailInput);  }