spring实现javaMail
2015-09-24 19:34
507 查看
spring 配置文件
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 加载Properties文件 -->
<bean id="configurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->
</bean>
<!-- 申明JavaMailSenderImpl对象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8" />
<property name="host" value="smtp.163.com" />
<property name="username" value="zhouyuan2452008648" />
<property name="password" value="522724199207zya" />
<property name="javaMailProperties">
<props>
<!-- 设置认证开关 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 启动调试开关 -->
<prop key="mail.debug">true</prop>
<!-- 设置发送延时 -->
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>
static ApplicationContext actx = new ClassPathXmlApplicationContext(
"spring.xml");
static MailSender sender = (MailSender) actx.getBean("mailSender");
//static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");
public static void main(String args[]) throws Exception{
//sendSimpleMail();
sendAttach();
}
/*
* 发送简单的邮件
*/
public static void sendSimpleMail(){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom("zhouyuan2452008648@163.com");//发送方邮箱
mailMessage.setSubject("你好");//发送标题
mailMessage.setText("这个是spring实现邮件发送的简单例子");//发送文本
mailMessage.setTo(new String[]{"2452008648@qq.com"});//接收者邮箱,可以是多个
//mailMessage.setReplyTo("2452008648@qq.com");//设置答复应指向的地址
mailMessage.setSentDate(new Date());//设置发送的时间
//mailMessage.setCc("2452008648@qq.com");//抄送
//mailMessage.setBcc();//密送,其他收件人看不到密送的地址
sender.send(mailMessage);
}
/**
* 发送html邮件
* @throws MessagingException
*/
public static void sendHtml() throws Exception{
JavaMailSenderImpl mailsender = (JavaMailSenderImpl) sender;
MimeMessage mm = mailsender.createMimeMessage();
MimeMessageHelper mmh = new MimeMessageHelper(mm,true);
//设置发送方邮箱和显示昵称
mmh.setFrom(new InternetAddress("zhouyuan2452008648@163.com", "齐天大圣"));
//设置接收者邮箱
mmh.setTo(new String[]{"2452008648@qq.com"});
//设置标题
mmh.setSubject("来自天堂的邮件");
// mmh.setText("fd");
mmh.setText("<html><body><a href='http:www.baidu.com'>百度一下,你就知道</a></body></html>", true);
//ClassPathResource file = new ClassPathResource("girl.jpg");
//设置内容
//mmh.addAttachment("girl.jpg", file);
mailsender.send(mm);
}
/**
* 发送附件邮件
* @throws MessagingException
*/
public static void sendAttach() throws Exception{
JavaMailSenderImpl mailsender = (JavaMailSenderImpl) sender;
MimeMessage mm = mailsender.createMimeMessage();
MimeMessageHelper mmh = new MimeMessageHelper(mm,true,"UTF-8");
//设置发送方邮箱和显示昵称
mmh.setFrom(new InternetAddress("zhouyuan2452008648@163.com", "齐天大圣"));
//设置接收者邮箱zhouyuan008@deppon.com
mmh.setTo(new String[]{"2452008648@qq.com"});
//设置标题
mmh.setSubject("来自天堂的邮件");
mmh.setValidateAddresses(true);
mmh.setText("<html><body><a href='http://www.baidu.com'>百度一下,你就知道</a><br/>" +
"<img src=cid:image/></body></html>", true);
FileSystemResource file = new FileSystemResource(new File("src/girl.jpg"));
mmh.addInline("image",file); // 跟cid一致
ClassPathResource files = new ClassPathResource("hello.txt");
//设置内容
mmh.addAttachment(MimeUtility.encodeWord("你好.txt"),files);//解决附件名乱码的问题
mailsender.send(mm);
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 加载Properties文件 -->
<bean id="configurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<!-- 查看SimpleMailMessage源码还可以注入标题,内容等 -->
</bean>
<!-- 申明JavaMailSenderImpl对象 -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="defaultEncoding" value="UTF-8" />
<property name="host" value="smtp.163.com" />
<property name="username" value="zhouyuan2452008648" />
<property name="password" value="522724199207zya" />
<property name="javaMailProperties">
<props>
<!-- 设置认证开关 -->
<prop key="mail.smtp.auth">true</prop>
<!-- 启动调试开关 -->
<prop key="mail.debug">true</prop>
<!-- 设置发送延时 -->
<prop key="mail.smtp.timeout">0</prop>
</props>
</property>
</bean>
</beans>
static ApplicationContext actx = new ClassPathXmlApplicationContext(
"spring.xml");
static MailSender sender = (MailSender) actx.getBean("mailSender");
//static SimpleMailMessage mailMessage = (SimpleMailMessage) actx.getBean("mailMessage");
public static void main(String args[]) throws Exception{
//sendSimpleMail();
sendAttach();
}
/*
* 发送简单的邮件
*/
public static void sendSimpleMail(){
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom("zhouyuan2452008648@163.com");//发送方邮箱
mailMessage.setSubject("你好");//发送标题
mailMessage.setText("这个是spring实现邮件发送的简单例子");//发送文本
mailMessage.setTo(new String[]{"2452008648@qq.com"});//接收者邮箱,可以是多个
//mailMessage.setReplyTo("2452008648@qq.com");//设置答复应指向的地址
mailMessage.setSentDate(new Date());//设置发送的时间
//mailMessage.setCc("2452008648@qq.com");//抄送
//mailMessage.setBcc();//密送,其他收件人看不到密送的地址
sender.send(mailMessage);
}
/**
* 发送html邮件
* @throws MessagingException
*/
public static void sendHtml() throws Exception{
JavaMailSenderImpl mailsender = (JavaMailSenderImpl) sender;
MimeMessage mm = mailsender.createMimeMessage();
MimeMessageHelper mmh = new MimeMessageHelper(mm,true);
//设置发送方邮箱和显示昵称
mmh.setFrom(new InternetAddress("zhouyuan2452008648@163.com", "齐天大圣"));
//设置接收者邮箱
mmh.setTo(new String[]{"2452008648@qq.com"});
//设置标题
mmh.setSubject("来自天堂的邮件");
// mmh.setText("fd");
mmh.setText("<html><body><a href='http:www.baidu.com'>百度一下,你就知道</a></body></html>", true);
//ClassPathResource file = new ClassPathResource("girl.jpg");
//设置内容
//mmh.addAttachment("girl.jpg", file);
mailsender.send(mm);
}
/**
* 发送附件邮件
* @throws MessagingException
*/
public static void sendAttach() throws Exception{
JavaMailSenderImpl mailsender = (JavaMailSenderImpl) sender;
MimeMessage mm = mailsender.createMimeMessage();
MimeMessageHelper mmh = new MimeMessageHelper(mm,true,"UTF-8");
//设置发送方邮箱和显示昵称
mmh.setFrom(new InternetAddress("zhouyuan2452008648@163.com", "齐天大圣"));
//设置接收者邮箱zhouyuan008@deppon.com
mmh.setTo(new String[]{"2452008648@qq.com"});
//设置标题
mmh.setSubject("来自天堂的邮件");
mmh.setValidateAddresses(true);
mmh.setText("<html><body><a href='http://www.baidu.com'>百度一下,你就知道</a><br/>" +
"<img src=cid:image/></body></html>", true);
FileSystemResource file = new FileSystemResource(new File("src/girl.jpg"));
mmh.addInline("image",file); // 跟cid一致
ClassPathResource files = new ClassPathResource("hello.txt");
//设置内容
mmh.addAttachment(MimeUtility.encodeWord("你好.txt"),files);//解决附件名乱码的问题
mailsender.send(mm);
}
相关文章推荐
- java 基本类型与字符串之间的转换
- Java中关于时间间隔的计算
- 恢复eclipse默认的背景色和字体等格式
- Java学习笔记【包】
- Java 十大异常报错
- Java基础知识强化65:基本类型包装类之Integer的构造方法
- JAVA编程基础(二)
- 一些关于Java常用关键字的理解
- java写的爬虫
- java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to
- Java 动态代理机制分析及扩展,第 1 部分
- Eclipse 中使用Genymotion 作为模拟器的步骤
- javaEE 使用过滤器压缩响应内容
- Java基础知识强化64:基本类型包装类的引入
- Spring AOP在函数接口调用性能分析及其日志处理方面的应用
- java mock 工具
- spring mvc
- 转 Java集合框架的基本接口/类层次结构
- struts2 下载
- JavaEE 添加简单的日志过滤器