安卓发送邮件验证码

先要下载发送邮件需要的jar包
   public static String myEmailSMTPHost = "smtp.qq.com";
    public static String account = "@qq.com";//自己的邮箱
    public static String password = "";//密码
    int verificationCode = (int)((Math.random()*9+1)*100000);

发送邮件代码
 sendMail(mail, verificationCode);
 
 发邮件函数:
     public static void sendMail(String to, int code) {
        Log.d("进入了邮件发送函数", "sendEmail: ");
// 1.创建连接对象,链接到邮箱服务器
        final Properties props = new Properties(); // 参数配置
        props.setProperty("mail.transport.protocol", "smtp");// 使用的协议(JavaMail规范要求)
        props.setProperty("mail.smtp.host", myEmailSMTPHost);// 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true"); // 需要请求认证


// 2.根据配置创建会话对象, 用于和邮件服务器交互
        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(account, password);
            }
        });

        try {
// 3.创建邮件对象
            final Message message = new MimeMessage(session);
// 3.1设置发件人
            message.setFrom(new InternetAddress(account));
// 3.2设置收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
// 3.3设置邮件的主题
            message.setSubject("欢迎您");
// 3.4设置邮件的正文
            message.setContent("<h1>您的验证码是:" + code, "text/html;charset=UTF-8");
// 4.发送邮件
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Transport.send(message);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            ## 重点:新版本的安卓http操作必须都要放到线程中去,否则可能会报错,或者不执行

        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

发布了46 篇原创文章 · 获赞 12 · 访问量 1591

猜你喜欢

转载自blog.csdn.net/weixin_43605701/article/details/103445909
今日推荐