郵件是Web應用的一個重要部分,經常使用於郵箱確認、找回密碼、發送廣告、通知等等。html
Java中的JavaMail API不是很易於學習使用,容易編寫出冗餘易錯的代碼。Spring 提供了對JavaMail的有效支持,使得發送郵件變得簡單愉快。java
org.springframework.mail包是Spring Mail支持的頂級包,包下的MailSender,MailMessage兩個核心接口和一個SimpleMessageMessage類。spring
其中MailSender郵件發送器,定義了發送簡單郵件的方法,void send(SimpleMailMessage simpleMessage);亦能夠發送多個郵件 void send(SimpleMailMessage[] simpleMessages);apache
MailSender的子接口JavaMailSender及其實現類JavaMailSenderImpl提供的對MIME message的支持,這樣咱們就能夠再郵件中編寫Html代碼加入圖片了。瀏覽器
MailMessage負責郵件自己的信息,包括設置主題,內容,發件人,收件人等方法。
app
MailMessage有兩個實現類,SimpleMailMessage和MimeMailMessage,SimpleMailMessage定義了簡單文本郵件,MimeMailMessage則提供了更多豐富的郵件內容支持。async
I have trouble in installing PinYin input method, so i have to continue my blog using my poor english... :)And for practice maven
Talk is cheap, so show the code.ide
First we need some basic infrastructure, like jdk,maven,and text editor or ide(recommended).學習
Then we create an maven project from quick-start stereotype and put some dependencies to the pom.xml.
What we need : spring-core,spring-context,spring-context-support, javax.mail and spring-test and some logging artifacts.
Here we begin our project.
create a spring configuration and define some beans to use
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.host}"/> <property name="port" value="${mail.port}"/> <property name="username" value="${mail.username}"/> <property name="password" value="${mail.password}"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="simpleMessage" class="org.springframework.mail.SimpleMailMessage"> <property name="from" value="${mail.username}"/> </bean>
Here we create an mailSender instance implemeted by JavaMailSenderImpl and SimpleMailMessage instance.
you could replace the placeholders with your own mail setting.
Then we could use mailSender send simpleMessage for simple text email.
Put the business in a service layer class EmailService, it has a method for sending.
@Service public class EmailService { @Autowired private JavaMailSender javaMailSender; @Autowired private SimpleMailMessage simpleMailMessage; public void sendTo(String to, String subject, String text){ simpleMailMessage.setTo(to); simpleMailMessage.setSubject(subject); simpleMailMessage.setText(text); javaMailSender.send(simpleMailMessage); } }
Then we could test this. Create a test class.
@ContextConfiguration(locations = {"/applicationContext.xml"}) public class EmailServiceTest extends AbstractTestNGSpringContextTests{ @Autowired private EmailService emailService; @Test public void sendSimpleMessageTest(){ String email = "toAddress@gmail.com"; String subject = "This is send by liuzhengyang for fun"; String text= "This is send by liuzhengyang for fun" + "and i write &!@@@"; emailService.sendTo(email, subject, text); } }
notice your subject and email content should not be Spam like . or this email may be interceptored.Maybe i have to set up my
own mail server to break limitation for sending rate and email content.
Run this test , then i could receive this simple text email.
At production , we need some more complex email then we could use MimeMailMessage.
To use MimeMailMessage we could use MimeMessagePreparator and MimeMessageHelper for simplicity.
public void sendForRegister(final String to, final String subject){ javaMailSender.send(new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); mimeMessageHelper.setFrom(javaMailSender.getUsername()); mimeMessageHelper.setTo(to); mimeMessageHelper.setSubject(subject); mimeMessageHelper.setText("<html><h1>MimeMailMessage</h1></html>", true); } }); }
For using MimeMessageHelper#setFrom , i change the declaration of JavaMailSender javaMailSender to JavaMailSenderImpl javaMailSender;
Test this , and i receive a h1 email.
And we could add attachments or inline resources to the email.And maybe the setText() in the example above make think of using Servlet to send response,
this is error prone and some hardCoding.For better choice, we could use template like Velocity.
add bean defination for velocity
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <property name="velocityProperties"> <value> resource.loader=class class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader </value> </property> </bean>
We create a velocity template
## for email confirm after registeration <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <style> body{ text-align: center; font-size: large; } #title{ color: #DD3322; } </style> </head> <body> <h3>親愛的 <span id="title">${username}</span>, 歡迎!!</h3> <div> The life is beautiful and enjoy yourself!<br/> </div> <div> 請點擊一下連接完成確認:<br/> <a href="${url}">${url}</a><br/> (若是連接不能點擊,請複製並粘貼到瀏覽器的地址欄,而後按回車鍵,該連接48小時內有效。)<br/> </div> <div> <img src="cid:img0"/> </div> </body> </html>
And then use it for mailing.
public void sendForRegister(final String to, final String subject){ javaMailSender.send(new MimeMessagePreparator() { @Override public void prepare(MimeMessage mimeMessage) throws Exception { MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); mimeMessageHelper.setFrom(javaMailSender.getUsername()); mimeMessageHelper.setTo(to); mimeMessageHelper.setSubject(subject); Map<String, Object> model = new HashMap<String, Object>(); model.put("username","somenewuserfromuserobject"); model.put("url","urlfromuserobject"); mimeMessageHelper.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,"/template/mail/registeration-confirmation.vm","UTF-8",model), true); ClassPathResource resource = new ClassPathResource("/image/mail/img1.png"); System.out.println(resource.contentLength()); mimeMessageHelper.addInline("img0", resource); } }); }
This is the basic using of Spring Mail Support that i conclued.
There are several things to improve ,such as adding aop to email business , making this as as asynchronous task.