java邮箱发送验证码

1.引进依赖

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.3</version>
    </dependency>
3.
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.atguigu.msmservice.service.MsmService;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


@Service
public class MsmServiceImpl implements MsmService {
 
    private static final String USER = "你的邮箱号码"; // 发件人称号,同邮箱地址※
    private static final String PASSWORD = "你的授权码"; // 授权码,开启SMTP时显示※

    @Override
    public boolean send(String code, String phone) {
        //判断邮箱号是否为空
        if (StringUtils.isEmpty(phone)) {
            return false;
       }
            try {
                final Properties props = new Properties();
                props.put("mail.smtp.auth", "true");
//            注意发送邮件的方法中,发送给谁的,发送给对应的app,※
//            要改成对应的app。扣扣的改成qq的,网易的要改成网易的。※
                props.put("mail.smtp.host", "smtp.qq.com");
//            props.put("mail.smtp.host", "smtp.163.com");

                // 发件人的账号
                props.put("你的邮箱号码", USER);
                //发件人的密码
                props.put("你的邮箱密码", PASSWORD);

                // 构建授权信息,用于进行SMTP进行身份验证
                Authenticator authenticator = new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        // 用户名、密码
                        String userName = props.getProperty("你的邮箱号码");
                        String password = props.getProperty("你的邮箱密码");
                        return new PasswordAuthentication(userName, password);
                    }
                };
                // 使用环境属性和授权信息,创建邮件会话
                Session mailSession = Session.getInstance(props, authenticator);
                // 创建邮件消息
                MimeMessage message = new MimeMessage(mailSession);
                // 设置发件人
                String username = props.getProperty("1639392840@qq.com");
                InternetAddress form = new InternetAddress(username);
                message.setFrom(form);

                // 设置收件人
                InternetAddress toAddress = new InternetAddress(phone);
                message.setRecipient(Message.RecipientType.TO, toAddress);
                String text = "您好,您正在使用邮箱注册,验证码为"+code+"。5分钟有效,如若把验证码泄露他人,后果请自行承担";
                String title = "验证码";

                // 设置邮件标题
                message.setSubject(title);

                // 设置邮件的内容体
                message.setContent(text, "text/html;charset=UTF-8");
                // 发送邮件
                Transport.send(message);
                System.out.println("验证码为:"+code);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值