springMail的JavaMailSender和MimeMessagePreparator发送邮件

来源:互联网 发布:cntv for mac 编辑:程序博客网 时间:2024/06/03 14:38

转自(http://blog.csdn.net/ch469299503/article/details/7857572)

//这是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"
 xmlns:util="http://www.springframework.org/schema/util"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
   http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!--这是发送邮件的列-->
   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="smtp.163.com"></property><!--这是指定发送邮件的服务器-->
    <property name="javaMailProperties"><!--这是对邮箱的密码、账号进行验证-->
     <props>
      <prop key="mail.smtp.auth">true</prop>
     </props>
    </property>
    <property name="username" value="admin"></property>
    <property name="password" value="123456"></property>
   </bean>
   
   <bean id="myOwnMailSender" class="cn.itcast.sender.MyOwnMailSender">
     <property name="mailSender" ref="mailSender"></property>
   </bean>
</beans>

//这是发送邮件的类

package cn.itcast.sender;

import Java.NET.InetAddress;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

public  class MyOwnMailSender{
 
 private JavaMailSender mailSender;

 public JavaMailSender getMailSender() {
  return mailSender;
 }
 public void setMailSender(JavaMailSender mailSender) {
  this.mailSender = mailSender;
 }
 
 public void sendMail()
 {
  MimeMessagePreparator preparator = new MimeMessagePreparator(){

   public void prepare(MimeMessage mimeMessage) throws Exception {
    //注意:这汇总方式发送的邮件不能发送到qq邮箱去
    //recipient-->接受者
    //这是发送给接受的对象
    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("receiver@163.com"));
    //这是设置发送者
    mimeMessage.setFrom(new InternetAddress("sender@163.com"));
    //这是主题
    mimeMessage.setSubject("问候");
    //这是内容
    mimeMessage.setText("这就是内容");
    
    
   }

   
   
  };
  mailSender.send(preparator);
 }
 

}

//这是测试类

package cn.itcast.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.sender.MyOwnMailSender;

public class JavaMailTest {
 
 
 public static void main(String[] args) {
  org.springframework.context.ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  MyOwnMailSender mailSender = (MyOwnMailSender) context.getBean("myOwnMailSender");
  mailSender.sendMail();
 }
 

}

原创粉丝点击