首页 > 代码库 > 基于spring 的邮件发送java小程序

基于spring 的邮件发送java小程序

基于spring框架

简单三步:

  1. 导包

  2. 配置XML

  3. 接口调用





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-2.5.xsd">

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

<bean id="simpMail" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="http://www.mamicode.com/heyanyong163@163.com"/>
</bean>
  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">                <!--主机,用户名,密码等配置-->
  <property name="host"><value>smtp.163.com</value></property>
  <property name="username"><value>heyanyong163@163.com</value></property>
  <property name="password"><value>8888</value></property>
  <property name="port"><value>25</value></property>
 
  <property name="javaMailProperties">
    <props>
      <prop key="mail.smtp.auth">true</prop>
      <prop key="mail.smtp.timeout">25000</prop> 
    </props>
  </property>
 
  </bean>

</beans>




主测试方法:

package mytools.framework.jmail;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

public class JmailMain 
{
    public static void main( String[] args )
    {
     String[] locatoins={"applicationContext.xml"};
     ApplicationContext context=new ClassPathXmlApplicationContext(locatoins);
     JavaMailSender sender=(JavaMailSender) context.getBean("mailSender");
     SimpleMailMessage simpMail=(SimpleMailMessage) context.getBean("simpMail");
     simpMail.setTo("596907468@qq.com");  //目的邮箱
     simpMail.setSubject("主题");         //主题
     simpMail.setText("内容");            //内容
     sender.send(simpMail);
    }
}


基于spring 的邮件发送java小程序