《pro Spring》学习笔记之使用Spring+James邮件服务器发送普通文本邮件

首先,请参考这一片文章,配置好James邮件服务器

 http://blog.csdn.net/daryl715/archive/2007/09/14/1784869.aspx

由于使用免费邮(sohu,163,tom)总出现服务器连接不上,验证失败等问题,所以,本文采用开源的James邮件服务器来实现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"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >

< bean  id ="sender"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
 
< property  name ="host" >
    
< value > king.cn </ value >
  
</ property >  
  
< property  name ="username" >
    
< value > gaoxiang </ value >
  
</ property >
  
< property  name ="password" >
    
< value > gaoxiang </ value >
  
</ property >
  
< property  name ="javaMailProperties" >
        
< props >
           
< prop  key ="mail.smtp.auth" > true </ prop >
           
< prop  key ="mail.smtp.timeout" > 10000 </ prop >
        
</ props >
    
</ property >

</ bean >

< bean  id ="message"  class ="org.springframework.mail.SimpleMailMessage" >
  
< property  name ="from" >
    
< value > [email protected] </ value >
  
</ property >
   
< property  name ="to" >
    
< value > [email protected] </ value >
  
</ property >
   
< property  name ="subject" >
    
< value > subject </ value >
  
</ property >
   
< property  name ="text" >
    
< value > text </ value >
  
</ property >
</ bean >
</ beans >

 

测试代码:

 

package  ch15.SimpleMail;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;
import  org.springframework.mail.SimpleMailMessage;
import  org.springframework.mail.javamail.JavaMailSenderImpl;

public   class  SimpleMailTest  {


    
public static void main(String[] args) throws Exception {
        ApplicationContext context
=new ClassPathXmlApplicationContext("ch15/SimpleMail/applicationContext.xml");
        SimpleMailMessage mailMessage
=(SimpleMailMessage)context.getBean("message");
        JavaMailSenderImpl sender
=(JavaMailSenderImpl)context.getBean("sender");
     
        
         
for (int i = 0; i < 10; i++{
            
             sender.send(mailMessage);
             System.out.println(
"成功");
             Thread.sleep(
2000);  //发送后延时2秒钟
        }

                
                 
                 
        
            
        }

       
             
            
    

        
        


}

你可能感兴趣的:(spring,bean,exception,Class,import,邮件服务器)