[蓝莓商城]使用Spring的mail API发送邮件

蓝莓商城

今天使用Spring的mail API发送邮件。
下面说一下流程。

Maven依赖
<dependency>  
    <groupId>javax.mail</groupId>  
    <artifactId>mail</artifactId>  
    <version>1.4.7</version>  
</dependency>  
<!--Spring mail 类库-->
<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context-support</artifactId>
     <version>5.0.4.RELEASE</version>
</dependency>
创建bean

启用邮箱smtp功能
这里写图片描述

可以看到smtp的服务器地址是:smtp.sina.com。即为下面的host值。
填入该邮箱的username和password,邮件将会从该邮箱发出。

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.sina.com"></property>
        <property name="username" value="xxxx@sina.com"></property>
        <property name="password" value="xxxxx"></property>
        <property name="javaMailProperties">
            <props>
                 <prop key="mail.smtp.auth">true</prop>
                 <prop key="mail.smtp.timeout">20000</prop>
            </props>
        </property>
    </bean>

创建自定义发送的bean,

<bean id="userMailSender" class="org.lanmei.cms.email.UserMailSender">
         <property name="mailSender" ref="mailSender" /> 
</bean>

完整的配置文件:applicationContext-email.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.3.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.sina.com"></property>
        <property name="username" value="lanmeishop1@sina.com"></property>
        <property name="password" value="lanmei"></property>
        <property name="javaMailProperties">
            <props>
                 <prop key="mail.smtp.auth">true</prop>
                 <prop key="mail.smtp.timeout">20000</prop>
            </props>
        </property>
    </bean>

    <bean id="userMailSender" class="org.lanmei.cms.email.UserMailSender">
         <property name="mailSender" ref="mailSender" /> 
    </bean>
</beans>
编写UserMailSender类

mailSender已经在bean定义中进行依赖注入。

package org.lanmei.cms.email;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

public class UserMailSender {


    private JavaMailSenderImpl   mailSender;

    public void send(SimpleMailMessage mail) {
        mailSender.send(mail);
    }

    public JavaMailSenderImpl getMailSender() {
        return mailSender;
    }

    public void setMailSender(JavaMailSenderImpl mailSender) {
        this.mailSender = mailSender;
    }   
}
编写测试类


package org.lanmei.cms;

import org.junit.Test;
import org.lanmei.cms.email.UserMailSender;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;

public class UserMailSenderTest {

    @Test
    public void emailTest() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext-email.xml");

        UserMailSender sender = (UserMailSender) ac.getBean("userMailSender");

        SimpleMailMessage mail = new SimpleMailMessage();
        mail.setTo("xxxxx@qq.com");//收件人邮箱地址
        mail.setFrom("xxxxx@sina.com");//发件人,需与mailSender  bean中的username一致
        mail.setSubject("spring自带javamail发送的邮件");//主题
        mail.setText("hello this mail is from spring javaMail ");//正文
        sender.send(mail);
    }

}
完成
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值