zoukankan      html  css  js  c++  java
  • 使用spring的JavaMailSender发送邮件

    自己的代码魔改太多起不到简单易懂的效果,我还是直接上原来的人的代码吧。

    代码结构如下:

    这里写图片描述

    1、pom.xml中加入以下依赖:

    <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context-support</artifactId>
             <version>4.2.6.RELEASE</version>
        </dependency>
    <dependency>
             <groupId>javax.mail</groupId>
             <artifactId>mail</artifactId>
             <version>1.4.7</version>
    </dependency>
    

     2、applicationContext.xml配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
       <bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
             <list>
                <value>/mail.properties</value>
             </list>
          </property>
       </bean>
       <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
       <bean id="MailSenderDemo" class="com.mail.MailSenderDemo">
       </bean>
    </beans>
    

     3、mail.properties如下:主要是邮箱服务器、个人邮箱用户名和开启smtp服务后获得的授权码(非密码),

    mail.host=smtp.163.com
    mail.username=*****@163.com
    mail.password=******

     4、spring-mail.xml如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
       <property name="host" value="${mail.host}"></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="${mail.username}"></property>
       <property name="password" value="${mail.password}"></property>
    </bean>
    </beans>
    

     5.MailSenderDemo.java如下:

    public class MailSenderDemo {
        @Autowired
        private JavaMailSender mailSender;
        public void send(SimpleMailMessage mail){
         mailSender.send(mail);
        }
    }

    6、测试类SendTest.java如下:

    public class SendTest {
    
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mail.xml",
                    "applicationContext.xml");
            MailSenderDemo sender = (MailSenderDemo)ac.getBean("MailSenderDemo");
            SimpleMailMessage mail = new SimpleMailMessage();
            mail.setTo("gu.erlei@ustcinfo.com");//收件人邮箱地址
            mail.setFrom("gu.erlei@ustcinfo.com");//收件人
            mail.setSubject("spring自带javamail发送的邮件");//主题
            mail.setText("hello this mail is from spring javaMail ");//正文
            sender.send(mail);
        }
    }

    结果

    这里写图片描述

  • 相关阅读:
    【POJ 2044】 Weather Forecast
    【POJ 1703】 Find them,Catch them
    【SCOI 2005】 骑士精神
    字长与指针
    XModem协议
    SecureCRT乱码问题解决方法
    usb设备驱动程序
    如何检测 51单片机IO口的下降沿
    matlab神经网络工具箱创建神经网络
    九针串口接线问题, 232, 485
  • 原文地址:https://www.cnblogs.com/ydymz/p/8464573.html
Copyright © 2011-2022 走看看