手写过javamail发送邮件,深知其api之繁琐,看过spring发送邮件的例子之后,深感spring的封装让复杂活变得简单多了。
转自老帖子http://www.iteye.com/topic/348564
首先导入使用jar包:activation.jar,commons-logging-1.0.4.jar,mail.jar,spring.jar
1、使用xml配置javamail:
在classpath底下新建application-mail.xml,内容如下:
Xml代码
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="http://www.springframework.org/schema/beans"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns:aop="http://www.springframework.org/schema/aop"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
7. <!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
8. <bean id="mailSender"
9. class="org.springframework.mail.javamail.JavaMailSenderImpl">
10. <property name="host">
11. <value>stmp.163.com</value>
12. </property>
13. <property name="javaMailProperties">
14. <props>
15. <prop key="mail.smtp.auth">true</prop>
16. <prop key="mail.smtp.timeout">25000</prop>
17. </props>
18. </property>
19. <property name="username">
20. <value>xxxx@163.com</value>
21. </property>
22. <property name="password">
23. <value>xxxxxx</value>
24. </property>
25. </bean>
26. </beans>
或者把以上的Beans配置到applicaiont.xml里面也可以。
Gmail
1. <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
2. <property name="host" value="smtp.gmail.com" />
3. <property name="username" value="obullxl@gmail.com" />
4. <property name="password" value="********" />
5. <property name="javaMailProperties">
6. <props>
7. <prop key="mail.smtp.auth">true</prop>
8. <prop key="mail.smtp.timeout">25000</prop>
9. <prop key="mail.smtp.port">465</prop>
10. <prop key="mail.smtp.socketFactory.port">465</prop>
11. <prop key="mail.smtp.socketFactory.fallback">false</prop>
12. <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
13. </props>
14. </property>
15. </bean>
2、发送Email类:
Java代码
1. public class SendMail {
2. public ApplicationContext ctx = null;
3. public SendMail() {
4. //获取上下文
5. ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
6. }
7. public void send() {
8. //获取JavaMailSender bean
9. JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
10. SimpleMailMessage mail = new SimpleMailMessage(); //<span >注意SimpleMailMessage只能用来发送text格式的邮件</span>
11.
12.
13. try {
14. mail.setTo("xxx@qq.com");//接受者
15. mail.setFrom("xxxx@163.com");//发送者,这里还可以另起Email别名,不用和xml里的username一致
16. mail.setSubject("spring mail test!");//主题
17. mail.setText("springMail的简单发送测试");//邮件内容
18. sender.send(mail);
19. } catch (Exception e) {
20. e.printStackTrace();
21. }
22. }
发送html格式的Email:
Java代码
1. public class SendMail {
2. public ApplicationContext ctx = null;
3. public SendMail() {
4. //获取上下文
5. ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
6. }
7. public void send() {
8. //获取JavaMailSender bean
9. JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
10. JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
11. MimeMessage mailMessage = senderImpl.createMimeMessage();
12. //设置utf-8或GBK编码,否则邮件会有乱码
13. MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
14. try {
15. messageHelper.setTo(email.getEmail());//接受者
16. messageHelper.setFrom("xxx@163.com");//发送者
17. messageHelper.setSubject("测试邮件");//主题
18. //邮件内容,注意加参数true,表示启用html格式
19. messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
20. sender.send(mailMessage);
21. } catch (Exception e) {
22. e.printStackTrace();
23. }
24. }
发送html格式并带有附件的Email:
Java代码
1. public class SendMail {
2. public ApplicationContext ctx = null;
3. public SendMail() {
4. //获取上下文
5. ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
6. }
7. public void send() {
8. //获取JavaMailSender bean
9. JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
10. JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
11. MimeMessage mailMessage = senderImpl.createMimeMessage();
12. //设置utf-8或GBK编码,否则邮件会有乱码
13. MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
14. try {
15. messageHelper.setTo(email.getEmail());//接受者
16. messageHelper.setFrom("xxx@163.com");//发送者
17. messageHelper.setSubject("测试邮件");//主题
18. //邮件内容,注意加参数true
19. messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
20. //附件内容
21. messageHelper.addInline("a", new File("E:/xiezi.jpg"));
22. messageHelper.addInline("b", new File("E:/logo.png"));
23. File file=new File("E:/测试中文文件.rar");
24. // 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
25. messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
26. sender.send(mailMessage);
27. } catch (Exception e) {
28. e.printStackTrace();
29. }
30. }
原文地址:http://spiritfrog.iteye.com/blog/708799