JAVA发送邮箱验证码

JAVA发送邮箱验证码

在JAVA中实现邮箱获取验证码

邮箱的SMTP服务器,一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com,163邮箱就是smtp.163.com
下面是QQ邮箱为例

  • 首先我们要导入maven的依赖
<dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-email</artifactId>
      <version>1.5</version>
    </dependency>
  • controller中的方法
@RequestMapping("myEmail")
    @ResponseBody
    public String myEmail(String email, Model m){
        System.out.println("-------------------------------"+email);
        Random rd = new Random();
        Integer r = rd.nextInt(100000);
        String s = r.toString();
        try {
            HtmlEmail emailId  = new HtmlEmail();
            //邮箱的SMTP服务器,一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
            emailId.setHostName("smtp.qq.com");
            //设置发送的字符类型
            emailId.setCharset("utf-8");
            //设置收件人
            emailId.addTo(email);
            //发送人的邮箱为自己的,用户名可以随便填
            emailId.setFrom("xxx@qq.com","测试");
            //设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            emailId.setAuthentication("xxx@qq.com","petwovtizhyxcchd");
            //设置发送主题//创建一个HtmlEmail实例对象
            //
            emailId.setSubject("短信注册");
            //设置发送内容
            emailId.setMsg(s);
            //进行发送
            emailId.send();
        }catch (Exception e){

        }
        m.addAttribute("emailId",s);
        return s;
    }

还要设置邮箱的SMTP服务器,在邮箱设置中找到并开启(POP3/SMTP服务)协议
如果邮箱没有收到验证码,可能会在邮箱的垃圾箱里面

要使用 Java 发送邮件验证码,需用到 Java Mail API 库。以下是发送邮件验证码的基本步骤: 1. 确定发件人、收件人、SMTP 服务器地址和端口号; 2. 创建 JavaMail Session 对象,并设置发件人身份验证信息; 3. 使用 JavaMail Session 对象创建一个邮件消息; 4. 设置邮件消息的内容、主题、收发件人等信息; 5. 发送邮件消息。 具体实现代码可以参考以下示例代码: ```java import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendEmail { public static void main(String[] args) { // 发件人信息 String from = "your_email@example.com"; String password = "your_email_password"; // 收件人信息 String to = "recipient_email@example.com"; // SMTP 服务器信息 String host = "smtp.example.com"; int port = 587; // 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.port", port+""); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.starttls.enable", "true"); // 获取默认会话对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { // 创建邮件消息 MimeMessage message = new MimeMessage(session); // 设置发件人 message.setFrom(new InternetAddress(from)); // 设置收件人 message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 设置邮件主题 message.setSubject("验证码"); // 生成随机验证码 int code = (int)(Math.random() * 9000 + 1000); // 设置邮件内容 message.setText("您的验证码是:" + code); // 发送邮件 Transport.send(message); System.out.println("邮件已发送。"); } catch (MessagingException e) { System.out.println("发送邮件出现异常:" + e.getMessage()); } } } ``` 以上代码以 Gmail SMTP 服务器为例,需要将 `smtp.example.com` 和端口号 `587` 修改为相应的 SMTP 服务器地址和端口号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值