基于Javamail如何实现发送邮件

发布时间:2022-08-11 14:06:20 作者:iii
来源:亿速云 阅读:151

这篇文章主要介绍“基于Javamail如何实现发送邮件”,在日常操作中,相信很多人在基于Javamail如何实现发送邮件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于Javamail如何实现发送邮件”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

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

基于Javamail如何实现发送邮件

开启后会得到一个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服务, 设置授权码.

基于Javamail如何实现发送邮件

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

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

到此,关于“基于Javamail如何实现发送邮件”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. SpringBoot 2.0 集成 JavaMail ,实现异步发送邮件
  2. 利用javaMail发送邮件的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javamail

上一篇:Spring Security权限管理实例分析

下一篇:普通js文件里面怎么访问vue实例this指针

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》