Introduction
Spring framework provides a higher level of abstraction for sending e-mail which hides underlying details of low-level resource handling.
What all is needed for Spring e-mail to work?
Let me try to reveal it with the following step by step process:
STEP 1)
The important package is org.springframework.mail. It contains following two main interfaces:
a) MailSender
--As name states, it is used for sending an email. It declares two overloaded send() methods.
b) MailMessage
--One of the implementations of this interface is SimpleMailMessage, which encapsulates mail properties such as from, to, cc, subject and text.
If you are curious to know more details, please have a look here: http://static.springsource.org/spring/docs/1.2.9/reference/mail.html
STEP 2)
The next step would be to create a mail manager class (say MailManager) which has association/dependency with both the above interfaces. Here is a quick example:
public class MailManager implements Manager {
private MailSender mailSender;
private SimpleMailMessage message;
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}
public void setMessage(SimpleMailMessage message) {
this.message = message;
}
public void sendPwd(ForgotPwdBean forgotPwd, String pwd) {
//Create a thread safe "sandbox" of the message
SimpleMailMessage msg = new SimpleMailMessage(this.message);
msg.setTo(forgotPwd.getEmail());
msg.setSubject("Information about your account password");
msg.setText("Dear " + forgotPwd.getUid() + ",\n"
+ "You have requested password for your account.\n"
+ "Your password is: " + pwd + "\n" + "Thanks,\n"
+ "www.xyz.com");
try {
mailSender.send(msg);
} catch (MailException ex) {
//log it and go on
System.err.println(ex.getMessage());
}
}
}
STEP 3)
The Third step would be to configure your Application Context file. It should tell IOC container/BeanFactory how dependency will be injected. IOC Container can inject dependency either through 'setter' or 'constructor' Once configured, the container would inject dependency automatically. This is how dependency injection works in Spring:-
<bean id="mailManager" class="service.MailManager">
<property name="mailSender">
<ref bean="mailSender" />
property>
<property name="message">
<ref bean="mailMessage" />
property>
bean>
If you see above xml fragement. service.MailManager class has dependency with two properties ( mailSender and mailMessage) , which are referring to implemented beans (<ref bean="mailSender" />, ). These properties are actually interfaces (declared in class as well) NOT actual classes. Actual implementation classes are defined somewhere in the same application context file like this:
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="session" ref="mailSession" />
bean>
<bean id="mailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="put email here" />
bean>
STEP 4)
The next step would to setup mail session referenced by mailSession (i.e. ref="mailSession") like this in the same context file:
<bean id="mailSession" class="javax.mail.Session"
factory-method="getInstance">
<constructor-arg>
<props>
<prop key="mail.smtp.auth">trueprop>
<prop key="mail.smtp.socketFactory.port">465prop>
<prop key="mail.smtp.socketFactory.class">
javax.net.ssl.SSLSocketFactory
prop>
<prop key="mail.smtp.socketFactory.fallback">
false
prop>
props>
constructor-arg>
<constructor-arg ref="smtpAuthenticator" />
bean>
The above configuration is required to setup an authenticated session.
STEP 5)
Next step is to define an Authenticator class which is being referenced as constructor arg above ( i.e. ref="smtpAuthenticator" ). Here is the code:
package service.mailauthenticator;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SmtpAuthenticator extends Authenticator {
private String username;
private String password;
public SmtpAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
STEP 6)
And the final step is to define a bean in application context file and you are good to go!. Here is the xml:
<bean id="smtpAuthenticator"
class="service.mailauthenticator.SmtpAuthenticator">
<constructor-arg value="put email id here" />
<constructor-arg value="put password here" />
bean>
This xml is needed, so that your application automatically will long into your mail account and send mail from there.
That's it!, You are all set now. Please have a look and give a try. I would be happy to hear from any comments, suggestions and/or questions…
Thanks
Sanjay Semwal
J2EE Architect
No comments:
Post a Comment