<?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:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  

  
<bean id="mailSender"    class="org.springframework.mail.javamail.JavaMailSenderImpl">   <property name="host">  

  
<value>127.0.0.1</value><!-- 本地服务器  如果是其他,请填如:smtp.sohu.com-->

   
</property>   

 
<property name="javaMailProperties">

    
<props>  

          
<prop key="mail.smtp.auth">true</prop>  

                
<prop key="mail.smtp.timeout">25000</prop>  

            
</props>  

        
</property>  

        
<property name="username">  

            
<value>postmaster@mai.com</value> <!-- 我这里用本地的邮箱名--> 

        
</property>  

        
<property name="password">  

            
<value>123456</value>  

        
</property>  

    
</bean>  

</beans>

然后建立一个java文件

 

package org.fantlam.spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.mail.SimpleMailMessage;

import org.springframework.mail.javamail.JavaMailSender;

 

public class SpringMail1 {

public static void main(String args[]){

 ApplicationContext ctx 
=new ClassPathXmlApplicationContext("applicationContext.xml");   

  JavaMailSender sender 
= (JavaMailSender) ctx.getBean("mailSender");   

 SimpleMailMessage mail 
= new SimpleMailMessage();

//这里SimpleMailMessage只能用来发送text格式的邮件

  
try {   

  mail.setTo(
"fantlam@163.com");//接收者   

  mail.setFrom(
"sohu@mai.com");//按前面讲的,可以随便起

  mail.setSubject(
"spring mail test!");//主题   

  mail.setText(
"springMail的简单发送测试");//邮件内容   

  sender.send(mail);   

 } 
catch (Exception e) {   

  e.printStackTrace();   

 }   

 

}

}




import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
 
/**
  * 
@param args
  
*/
 
public static void main(String[] args) {
   Properties props 
= System.getProperties();  
         
// 设置smtp服务器  
         props.setProperty("mail.smtp.host""smtp.126.com");  
         
// 现在的大部分smpt都需要验证了  
         props.put("mail.smtp.auth""true");  
  
         Session s 
= Session.getInstance(props);  
         
// 为了查看运行时的信息  
         s.setDebug(true);  
         
// 由邮件会话新建一个消息对象  
         MimeMessage message = new MimeMessage(s);  
         
try {  
             
// 发件人  
             InternetAddress from = new InternetAddress("ashutc@126.com");  
             message.setFrom(from);  
             
// 收件人  
             InternetAddress to = new InternetAddress("ashutc@126.com");  
             message.setRecipient(Message.RecipientType.TO, to);  
             
// 邮件标题  
             message.setSubject("test");  
             String content 
= "测试内容";  
             
// 邮件内容,也可以使纯文本"text/plain"  
             message.setContent(content, "text/html;charset=GBK");  
  
             
/****下面代码是发送附件****** 
             String fileName = "d:\\hello.txt"; 
             MimeBodyPart messageBodyPart = new MimeBodyPart(); 
             messageBodyPart.setText("Hi"); 
             Multipart multipart = new MimeMultipart(); 
             multipart.addBodyPart(messageBodyPart); 
 
             messageBodyPart = new MimeBodyPart(); 
             DataSource source = new FileDataSource(fileName); 
             messageBodyPart.setDataHandler(new DataHandler(source)); 
             messageBodyPart.setFileName(fileName); 
             multipart.addBodyPart(messageBodyPart); 
 
             message.setContent(multipart);
*/ 
               
             message.saveChanges();  
             Transport transport 
= s.getTransport("smtp");  
             
// smtp验证,就是你用来发邮件的邮箱用户名密码  
             transport.connect("smtp.126.com""ashutc""*******");  
             
// 发送  
             transport.sendMessage(message, message.getAllRecipients());  
             transport.close();  
  
         } 
catch (Exception e) {  
             e.printStackTrace();  
         }  
 }
}