使用springMail发送普通邮件

需要的spring的jar包有:spring.jar,mail.jar,commons-logging.jar,activation.jar
Java代码 复制代码
  1. package mail;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  5. import org.springframework.mail.SimpleMailMessage;   
  6. import org.springframework.mail.javamail.JavaMailSender;   
  7.   
  8. public class Main {   
  9.   
  10.     /**  
  11.      * @param args  
  12.      */  
  13.     public static void main(String[] args) {   
  14.         // TODO Auto-generated method stub   
  15.         ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");   
  16.         JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");   
  17.         SimpleMailMessage mail = new SimpleMailMessage();   
  18.         mail.setFrom("[email protected]");   
  19.         mail.setTo("[email protected]");   
  20.         mail.setSubject(" 测试spring Mail");   
  21.         mail.setText("hello,java");   
  22.         mailSender.send(mail);   
  23.     }   
  24.   
  25. }  
package mail;

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

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
		JavaMailSender mailSender= (JavaMailSender) context.getBean("mailSender");
		SimpleMailMessage mail = new SimpleMailMessage();
		mail.setFrom("[email protected]");
		mail.setTo("[email protected]");
		mail.setSubject(" 测试spring Mail");
		mail.setText("hello,java");
		mailSender.send(mail);
	}

}

config.xml配置文件:
Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.        http://www.springframework.org/schema/beans/spring-beans.xsd"   
  6. >  
  7.       <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  8.         <property name="host" value="smtp.163.com" />  
  9.         <property name="port" value="25" />  
  10.         <property name="username" value="[email protected]" />  
  11.         <property name="password" value="你的密码" />  
  12.         <property name="javaMailProperties">  
  13.             <props>  
  14.                 <prop key="mail.smtp.auth">true</prop>  
  15.             </props>  
  16.         </property>  
  17.     </bean>  
  18. </beans>  
<?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.xsd"
>
	  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.163.com" />
        <property name="port" value="25" />
        <property name="username" value="[email protected]" />
        <property name="password" value="你的密码" />
        <property name="javaMailProperties">
         	<props>
         		<prop key="mail.smtp.auth">true</prop>
         	</props>
        </property>
    </bean>
</beans>

你可能感兴趣的:(spring,bean,xml,Gmail)