发邮件

添加email的依赖项:

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

新建邮件配置spring-email.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- spring mail begin ############################################## -->
    <!--<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
        <!--<property name="locations">-->
            <!--<list>-->
                <!--<value>classpath:mail.properties</value>-->
            <!--</list>-->
        <!--</property>-->

    <!--</bean>-->

<!--在location备注properties的路径,同时存在多个properties,每个properties添加ignore-unresolvable="true"-->

    <context:property-placeholder location="classpath:mail.properties"  ignore-unresolvable="true"/>

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


    <bean id="MailSenderDemo" class="com.zju.visualization.com.zju.mail.MailSenderDemo"></bean>
    <!-- spring mail demo end ############################################## -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}"/>
        <property name="port" value="${mail.port}"/>
        <property name="username" value="${mail.username}"/>
        <property name="password" value="${mail.password}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                <!-- 如果是网易邮箱, mail.smtp.starttls.enable 设置为 false-->
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>
</beans>

新建邮件property:

# qq mail server
mail.protocol=smtp
mail.port=465
mail.host=smtp.qq.com
mail.username=*******@qq.com
mail.password=*****

# 163 mail server
#mail.protocol=smtp
#mail.port=465
#mail.host=smtp.163.com
#mail.username=xxx@163.com

#mail.password=



新建demo:

package com.zju.visualization.com.zju.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
public class MailSenderDemo {


    @Autowired
    private JavaMailSender mailSender;


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


    public JavaMailSender getMailSender(){
        return this.mailSender;
    }
}


发送邮件;

@RequestMapping(value = "/mail")
    public void sendEmail(){
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                "spring//spring-email.xml");
        MailSender sender = (MailSender) ac.getBean("MailSenderDemo");
        JavaMailSender javaMailSender = sender.getMailSender();
        MimeMessage mime = javaMailSender.createMimeMessage();
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(mime, true, "utf-8");
            helper.setTo("1104253998@qq.com");// 收件人邮箱地址
            helper.setFrom("1104253998@qq.com");// 收件人
            helper.setSubject("Spring发送邮件");// 主题
            helper.setText("杨老师您好啊");// 正文
        } catch (MessagingException me) {
            me.printStackTrace();
        }
        javaMailSender.send(mime);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值