2008-07-11
配置邮件服务mail
ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<!-- 配置MailSender -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="username" value="livebookstore" />
<property name="password" value="LiVe-BoOkStOrE" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
</props>
</property>
</bean>
</beans>
package example.chapter9;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class Main {
public static void main(String[] args) throws Exception {
// 直接使用JavaMail API:
Properties props = new Properties();
// 设置SMTP服务器:
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
// 使用SSL安全连接:
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
send(
props,
"livebookstore", // Username
"LiVe-BoOkStOrE", // Password
"livebookstore@gmail.com", // From
new String[] {"livebookstore2@gmail.com"}, // To
"A test mail", // Subject
"测试使用JavaMail API发送邮件" // Text
);
//-----------------------------------------------------------------Spring发送文本文件----------------------------------------
// 使用Spring提供的MailSender:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
JavaMailSender mailSender = (JavaMailSender)context.getBean("mailSender");
// 创建一个纯文本邮件:
SimpleMailMessage mail = new SimpleMailMessage();
mail.setFrom("livebookstore@gmail.com");
mail.setTo("livebookstore2@gmail.com");
mail.setSubject("Another test mail");
mail.setText("测试使用Spring MailSender发送邮件");
// 发送:
mailSender.send(mail);
//-----------------------------------------------------------------Spring发送带两个附件的邮件----------------------------------------
// 发送Mime邮件:
MimeMessage mime = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mime, true, "UTF-8");
helper.setFrom("livebookstore@gmail.com");
helper.setTo("livebookstore2@gmail.com");
helper.setSubject("Test mime mail");
// 设定为HTML格式:
helper.setText("<html><body>访问Live在线书店:<br>"
+ "<a href='http://www.livebookstore.net' target='_blank'>"
+ "<img src='cid:logo'></a></body></html>",
true);
helper.addInline("logo", new ClassPathResource("logo.gif"));
helper.addAttachment("freebsd.gif", new ClassPathResource("freebsd.gif"));
// 发送:
mailSender.send(mime);
}
//-------------------------------------------------------------普通邮件调用的方法-----------------------------------------------------
public static void send(Properties props, final String username, final String password, String from, String[] to, String subject, String text) throws MessagingException {
Session session = Session.getInstance(props, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(text);
message.setSentDate(new Date());
Address[] addressTo = new Address[to.length];
for(int i=0; i<to.length; i++) {
addressTo[i] = new InternetAddress(to[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);
message.saveChanges();
Transport.send(message, addressTo);
}
}
发表评论
- 浏览: 30500 次
- 性别:

- 来自: 大连

- 详细资料
搜索本博客
我的相册
strutsWorkflow
共 3 张
共 3 张
最近加入圈子
链接
- 冰封太子
- 个人博客
- eclipse-API 函数
- eclipse-SWT资源
- Eclipse-下载
- hibernate-官方网站
- html-教程与代码
- html-教程与代码
- html-蓝色理想
- html-语言教程
- Javascript-基础教程
- PowerDesigner视频
- 流星絮语 JAVA学习笔记
- AppServNetword
- JDBC、SQLJ 的Oracle 驱动
- BeanSoft's Java Blog
- 面试时候经常会问的一些问题(不断补充中)
- Hibernate 3入門
- java学习
- 上帝的笔记本
- xiao
- 视频
- 尚学堂
- Intellij Idea下的iBatis
- vssplugin
- XDoclet最新下载包
- prototype.js官方
- 在线视频
- Ruby
- Aptana Studio
- 面试题集合
- 徐风城
- 最好的java源码
- java blog
最新评论
-
JSP购物车实例讲解
能不能写写struts2做成的购物车模块,把购物车放在session里面?
-- by chucai -
Struts 源码学习之Action ...
initChain()真是看不明白用了Commons Chain来搞了个CoR模 ...
-- by dvdface -
IntelliJ IDEA使用技巧一 ...
学习了!
-- by ziscloud -
ssh分页实例
else if ("next".equals(state)) 可以无止境的n ...
-- by figeonline -
js应用
这文章说什么的啊?能不能给个效果图啊?晕哎
-- by zhangzldipan






评论排行榜