25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13 ■ Adding E-mail and Scheduling Tasks<br />

Listing 13-2 shows your XML configuration. As you can see, you are using the property placeholder<br />

to read the email.properties file. Also, you are creating a mailSender bean that references the<br />

org.springframework.mail.javamail.JavaMailSenderImpl class. This class is the <strong>Spring</strong>’s e-mail helper class<br />

and it has several setters like host, port, username, password, and a javaMailProperties. You are setting these<br />

property values by putting in placeholders. Listing 13-3 shows the e-mail properties file.<br />

Listing 13-3. email.properties<br />

email.host=smtp.gmail.com<br />

email.port=587<br />

email.username=username<br />

email.password=password<br />

■ ■Note If you are using a different e-mail provider such as Hotmail or Yahoo, you can check out their help pages to find<br />

servers, ports, and more advance settings so you can configure the properties file and the javaMailProperties setters<br />

accordingly.<br />

Now let’s see Listing 13-4, the unit test. You are going to add some time measurement to see how long it will take<br />

to send an e-mail. Why? This is because the JavaMailSenderImpl has a synchronous call for sending a message to the<br />

server, meaning it must wait until the server accepts and responds that the e-mail was successfully sent.<br />

Listing 13-4. MyDocumentsTest.java<br />

package com.apress.isf.spring.test;<br />

import static org.junit.Assert.assertNotNull;<br />

import java.util.Date;<br />

import org.junit.Ignore;<br />

import org.junit.Test;<br />

import org.junit.runner.RunWith;<br />

import org.slf4j.Logger;<br />

import org.slf4j.LoggerFactory;<br />

import org.springframework.beans.factory.annotation.Autowired;<br />

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

import com.apress.isf.spring.email.EmailService;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-context.xml")<br />

public class MyDocumentsTest {<br />

private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class);<br />

@Autowired<br />

EmailService email;<br />

185

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!