您的位置:首页 > 编程语言 > Java开发

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: