基于Javamail实现发送邮件(QQ/网易邮件服务器)

 更新时间:2022年08月11日 11:30:30   作者:是码农没错了  
这篇文章主要介绍了基于Javamail实现发送邮件,分别使用QQ邮箱作为smtp邮件服务器发送邮件,使用网易邮箱作为smtp邮件服务器发送邮件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Javamail实现发送邮件的具体代码,供大家参考,具体内容如下

一. 使用QQ邮箱作为smtp邮件服务器发送邮件

步骤1.开启QQ邮箱的POP3/SMTP服务:

开启后会得到一个16位授权码, 作为第三方使用邮件服务器的登录凭证.
注意: 修改邮箱密码后, 授权码会失效, 需要重新获取.

步骤2: 编写配置文件applicationContext-email.xml(此处使用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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"
    default-lazy-init="false">
    <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <!-- qq的SMTP邮箱服务器地址, 若使用163网易则改为:smtp.163.com -->
            <value>smtp.qq.com</value>
        </property>
<!--     <property name="port">
            SMTP邮箱服务器端口(465或587), 建议不要配置, 使用默认就行 
            <value>建议不要配置!!博主配置反而发布出去!!</value>
        </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>xxxxxxx@qq.com</value>
        </property>
        <!-- 16位授权码, 注意不是登录密码! -->
        <property name="password">
            <value>qazcrslpoghcbahh</value>
        </property>
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
    </bean>

    <bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
        <!-- 发件人信息, 需要和上面username值一样 -->
        <property name="from" value="xxxxxxx@qq.com" />
    </bean>

</beans>

步骤3: 编写测试类:

package emailtest;

import java.util.Date;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-email.xml")
public class EmailTest {

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private SimpleMailMessage simpleMailMessage;
    
    @Test
    public void sendMail() throws MessagingException{
        sendMail("xxxxx@163.com","验证码:6666","密码找回");
    }
    
    public void sendMail(String email, String content, String subject) throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        messageHelper = new MimeMessageHelper(message, true, "UTF-8");
        messageHelper.setFrom(StringUtils.trimAllWhitespace(simpleMailMessage.getFrom()));
        messageHelper.setTo(email);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, true);
        messageHelper.setSentDate(new Date());
        // 发送邮件
        javaMailSender.send(messageHelper.getMimeMessage());
        
    }
}

二. 使用网易邮箱作为smtp邮件服务器发送邮件

1.相似的, 先打开网易邮箱的POP3/SMTP服务, 设置授权码.

2.修改上述applicationContext.xml中配置信息:

服务器地址改为smtp.163.com
username更改为你的网易邮箱账号
password则是你在开启POP3/SMTP服务时设置的授权码
from的值和username值一样.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 详解Spring Boot 自定义PropertySourceLoader

    详解Spring Boot 自定义PropertySourceLoader

    这篇文章主要介绍了详解Spring Boot 自定义PropertySourceLoader,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • java 中JXL操作Excel实例详解

    java 中JXL操作Excel实例详解

    这篇文章主要介绍了java 中JXL操作Excel实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • spring boot装载自定义yml文件

    spring boot装载自定义yml文件

    这篇文章主要为大家详细介绍了spring boot装载自定义yml文件的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Java深入分析了解平衡二叉树

    Java深入分析了解平衡二叉树

    平衡二叉树又被称为AVL树(有别于AVL算法),且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。本文将详解介绍一下平衡二叉树的原理与实现,需要的可以参考一下
    2022-06-06
  • 详解java实现简单扫码登录功能(模仿微信网页版扫码)

    详解java实现简单扫码登录功能(模仿微信网页版扫码)

    这篇文章主要介绍了java实现简单扫码登录功能(模仿微信网页版扫码),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-05-05
  • JAVA面向对象 封装原理及实例解析

    JAVA面向对象 封装原理及实例解析

    这篇文章主要介绍了JAVA面向对象 封装原理及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Spring  AOP的两种使用方法

    Spring  AOP的两种使用方法

    这篇文章主要介绍了Spring AOP的两种使用方法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-08-08
  • flatten-maven-plugin使用教程

    flatten-maven-plugin使用教程

    这篇文章主要介绍了flatten-maven-plugin使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-07-07
  • Java全面细致讲解==和equals的使用

    Java全面细致讲解==和equals的使用

    这篇文章主要介绍了Java中==和equals()的区别,,==可以使用在基本数据类型变量和引用数据类型变量中,equals()是方法,只能用于引用数据类型,需要的朋友可以参考下
    2022-05-05
  • IDEA 插件 mapper和xml互相跳转操作

    IDEA 插件 mapper和xml互相跳转操作

    这篇文章主要介绍了IDEA 插件 mapper和xml互相跳转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02

最新评论