Code Snippet

Just another Code Snippet site

Random Post :




[Java] Send HTML Mail using JNDI MailSender

JavaMailSender is retrieved using Spring @Autowired annotation. (JNDI Mail Session behind)

@Service
public class MailService {

    /** The logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);

    /** Allows sending messages. */
    @Autowired
    private JavaMailSender mailSender;

    public void sendMessage(final String from, final List<String> listOfRecipients,
            StringWriter htmlContent, final String subject) {
        LOGGER.debug("<sendMessage>");

        // For each recipients
        for (String to : listOfRecipients) {

            // Prepare message
            LOGGER.trace("Prepare message ...");
            MimeMessagePreparator preparator = prepareMessage(from, to, subject, htmlContent);

            // Send e-mails
            LOGGER.trace("Send e-mail ...");
            sendMessage(preparator);
        }
    }

    /**
     * Create an HTML Message ready to be sent by mail.
     * 
     * @param sendMail
     *            Sender email
     * @param mailRecipient
     *            Recipient email
     * @param mailSubject
     *            Mail subject
     * @param content
     *            HTML content
     * @return a {@link MimeMessagePreparator}
     */
    private MimeMessagePreparator prepareMessage(final String sendMail, final String mailRecipient,
            final String mailSubject, final StringWriter content) {
        LOGGER.debug("<prepareMessage>");

        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            @Override
            public void prepare(final MimeMessage mimeMessage) throws MessagingException {
                LOGGER.debug("<To> " + mailRecipient);
                LOGGER.debug("<From> " + sendMail);
                LOGGER.debug("<Subject> " + mailSubject);

                MimeMessageHelper mimeHelper = new MimeMessageHelper(mimeMessage, true);
                mimeHelper.addTo(mailRecipient);
                mimeHelper.setSubject(mailSubject);
                mimeHelper.setFrom(sendMail);
                mimeHelper.setReplyTo(sendMail);
                mimeHelper.setText(content.toString(), true);
            }
        };

        return preparator;
    }

    /**
     * Send email with JNDI MailSession.
     * 
     * @param message
     *            to send
     * @throws TicException
     *             in case of error
     */
    private void sendMessage(final MimeMessagePreparator message) {
        LOGGER.debug("<sendMessage>");
        try {
            mailSender.send(message);
        } catch (MailException e) {
            // TODO : handle exception
        }
    }
}

, ,

[Java] Mock ServletOutputStream

    class MockServletOutputStream extends ServletOutputStream {

        FileWriter writer;

        public MockServletOutputStream(File file) {
            try {
                writer = new FileWriter(file);
            } catch (IOException e) {
                // TODO
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void write(int b) throws IOException {
            writer.write(b);
        }

        @Override
        public void close() {
            try {
                super.close();
                writer.flush();
                writer.close();
            } catch (IOException e) {
                // TODO
            }
        }
    }
@Test
public void test1() {

        File output = new File("target/test-classes/output.txt");
        MockServletOutputStream stream = new MockServletOutputStream(output);

        HttpServletResponse response = mock(HttpServletResponse.class);
        when(response.getOutputStream()).thenReturn(stream);



}

[Oracle] Enable/Disable a foreign key

ALTER TABLE table_name
DISABLE CONSTRAINT constraint_name;
ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

Disable all constraints :

select 'ALTER TABLE ' || table_name || ' DISABLE CONSTRAINT ' || constraint_name || ';'
from   USER_constraints
-- where  constraint_name like '_K%'
ORDER BY constraint_Type desc

Enable all constraints :

select 'ALTER TABLE ' || table_name || ' MODIFY CONSTRAINT ' || constraint_name || ' ENABLE NOVALIDATE;'
from   USER_constraints
-- where  constraint_name like '_K%'
ORDER BY constraint_Type 

,

Previous Posts

Theme created by thememotive.com. Powered by WordPress.org.