Posted on 2012-03-02 14:34
yuhaibo736 阅读(9340)
评论(4) 编辑 收藏
首先导入使用jar包:activation.jar,commons-logging-1.0.4.jar,mail.jar,spring.jar
1、使用xml配置javamail:
在classpath底下新建application-mail.xml,内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
-
- <bean id="mailSender"
- class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host">
- <value>stmp.163.com</value>
- </property>
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- <prop key="mail.smtp.timeout">25000</prop>
- </props>
- </property>
- <property name="username">
- <value>xxxx@163.com</value>
- </property>
- <property name="password">
- <value>xxxxxx</value>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 注意:这里的参数(如用户名、密码)都是针对邮件发送者的 -->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host">
<value>stmp.163.com</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
<property name="username">
<value>xxxx@163.com</value>
</property>
<property name="password">
<value>xxxxxx</value>
</property>
</bean>
</beans>
或者把以上的Beans配置到applicaiont.xml里面也可以。
2、发送Email类:
- public class SendMail {
- public ApplicationContext ctx = null;
- public SendMail() {
-
- ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
- }
- public void send() {
-
- JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
- SimpleMailMessage mail = new SimpleMailMessage();
-
-
- try {
- mail.setTo("xxx@qq.com");
- mail.setFrom("xxxx@163.com");
- mail.setSubject("spring mail test!");
- mail.setText("springMail的简单发送测试");
- sender.send(mail);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
public class SendMail {
public ApplicationContext ctx = null;
public SendMail() {
//获取上下文
ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
}
public void send() {
//获取JavaMailSender bean
JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
SimpleMailMessage mail = new SimpleMailMessage(); //注意SimpleMailMessage只能用来发送text格式的邮件
try {
mail.setTo("xxx@qq.com");//接受者
mail.setFrom("xxxx@163.com");//发送者,这里还可以另起Email别名,不用和xml里的username一致
mail.setSubject("spring mail test!");//主题
mail.setText("springMail的简单发送测试");//邮件内容
sender.send(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
发送html格式的Email:
- public class SendMail {
- public ApplicationContext ctx = null;
- public SendMail() {
-
- ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- public void send() {
-
- JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
- JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
- MimeMessage mailMessage = senderImpl.createMimeMessage();
-
- MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
- try {
- messageHelper.setTo(email.getEmail());
- messageHelper.setFrom("xxx@163.com");
- messageHelper.setSubject("测试邮件");
-
- messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
- sender.send(mailMessage);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
public class SendMail {
public ApplicationContext ctx = null;
public SendMail() {
//获取上下文
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
}
public void send() {
//获取JavaMailSender bean
JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
MimeMessage mailMessage = senderImpl.createMimeMessage();
//设置utf-8或GBK编码,否则邮件会有乱码
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
try {
messageHelper.setTo(email.getEmail());//接受者
messageHelper.setFrom("xxx@163.com");//发送者
messageHelper.setSubject("测试邮件");//主题
//邮件内容,注意加参数true,表示启用html格式
messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
sender.send(mailMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
发送html格式并带有附件的Email:
- public class SendMail {
- public ApplicationContext ctx = null;
- public SendMail() {
-
- ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- public void send() {
-
- JavaMailSender sender = (JavaMailSender) ctx.getBean("mailSender");
- JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
- MimeMessage mailMessage = senderImpl.createMimeMessage();
-
- MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
- try {
- messageHelper.setTo(email.getEmail());
- messageHelper.setFrom("xxx@163.com");
- messageHelper.setSubject("测试邮件");
-
- messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
-
- messageHelper.addInline("a", new File("E:/xiezi.jpg"));
- messageHelper.addInline("b", new File("E:/logo.png"));
- File file=new File("E:/测试中文文件.rar");
-
- messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
- sender.send(mailMessage);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }