java实现发送邮件
在做聊天室项目的时候,由于需要发送邮箱验证码,所以自己查找了这方面的内容。
首先需要在Maven里面依赖
 <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency> 
位置大体如下

再点击

发送邮件以QQ邮箱举例
首先要开通SMTP服务,先进入qq邮箱。
找到设置

找到常规

找到第三方服务开通即可

我这里已经开通
更新一下。
然后就可以定义一个发消息的类,发送消息直接调用里面的方法,
package com.djj.social.tool;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
public class MailSend {
    private static MimeMessage message;
    private static String m;
    public static void sendEmail(String to) throws MessagingException {
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // QQ邮箱服务器
        String smtpHost = "smtp.qq.com";
        // 邮箱用户名,即QQ账号(自定义)
        final String username = "xxxxxxxxxxxx";
        // 邮箱授权码(自定义)
        final String password = "xxxxxxxxxxx";//开通SMTP会得到一个授权码,代替里面的x即可
        // 自己的邮箱(自定义)
        final String from = "xxxxxxxxxx@qq.com";
        // 要发送的邮箱地址(自定义)
        String toAddress = to;
        Transport transport;
        Properties props = new Properties();
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.setProperty("mail.smtp.auth", "true");
        props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.username", username);
        props.put("mail.smtp.password", password);
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        InternetAddress[] addresses = {new InternetAddress(toAddress)};
        message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, addresses);
        message.setSubject("验证码");// 发送标题(自定义)
        message.setSentDate(new Date());
        m=generateVerificationCode(5);//验证码的长度
        System.out.println(m);
        message.setText(m);// 发送内容(自定义)
        transport = session.getTransport("smtp");
        transport.connect(smtpHost, username, password);
        transport.send(message);
        System.out.println("Email has been sent");
        transport.close();
    }
    public static boolean judge(String s){
        if(m.equals(s)){
            return true;
        }else{
            return false;
        }
    }
    private static String generateVerificationCode(int length) {
        String charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder verificationCode = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            verificationCode.append(charSet.charAt(random.nextInt(charSet.length())));
        }
        return verificationCode.toString();
    }
} 
里面的judge方法用于判断输入的验证码是否正确,sendEmail方法用于发送邮件。
使用方式如下:
package com.djj.social.demo;
import com.djj.social.tool.MailSend;
import javax.mail.MessagingException;
public class test {
    public static void main(String[] args) throws MessagingException {
        
        MailSend.sendEmail("xxxxxxxxxx@qq.com");
        
        String yzm="XXXXXXX";//这个为输入的验证码
        
        if(MailSend.judge(yzm)==true)
        {
            System.out.println("验证码正确");
        }
        else
        {
            System.out.println("验证码错误");
        }
    }
}
 
                


















