java4.23学习总结

使用java发送邮件

准备工作:

下载两个发送邮件的包并导入到java:activation-1.1.1.jar,javax.mail.jar;

创建一个用于发送邮件的类
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 mail {
    private static MimeMessage message;
    //在发送验证码邮件后保存一份验证码,用于判定验证码是否匹配
    public static String JudgeCode;
    //to 为你要发送到的邮箱的地址
    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 = "邮箱用户名";
        // 邮箱授权码(自定义)
        final String password = "邮箱授权码";
        // 自己的邮箱(自定义)
        final String from = "自己的邮箱";
        // 要发送的邮箱地址(自定义)
        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());
        message.setText(generateVerificationCode(5));// 发送内容(自定义)
        transport = session.getTransport("smtp");
        transport.connect(smtpHost, username, password);
        transport.send(message);
        System.out.println("邮件发送成功");
        transport.close();
    }

    //用于生成验证码的方法
    //生成的方法为从26个大小写字母和数字中随机选中指定长度的字符然后保存验证码
    private static String generateVerificationCode(int length) {
        String charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder verificationCode = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            verificationCode.append(charSet.charAt(random.nextInt(charSet.length())));
        }
        JudgeCode = verificationCode.toString();
        return verificationCode.toString();
    }
}

然后我们只要通过类名来调用这个类中的sendEmail(to)静态方法就行了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值