使用springMail发送普通邮件

需要的spring的jar包有:spring.jar,mail.jar,commons-logging.jar,activation.jar
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 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>

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