猿问

Java发送邮件老是报这错,怎么解决?

javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as ssl)

Properties prop = new Properties();

        prop.setProperty("mail.transport.protocol", "smtp"); // 设置邮件发送协议
        prop.setProperty("mail.host", "smtp.qq.com"); // 邮件服务器地址

        //prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证
        prop.setProperty("mail.smtp.auth", "true"); // 邮件服务身份验证
        //prop.setProperty("mail.smtp.localhost", "127.0.0.1");


        Session session = Session.getDefaultInstance(prop);

        // 收件人电子邮箱
        String to = "xxxxx@qq.com";

        // 发件人电子邮箱
        String from = "xxxx@qq.com";

        try{
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject("This is the Subject Line!");

            // 设置消息体
            message.setText("This is actual message");

            // 根据 Session 获取邮件传输对象
            Transport transport = session.getTransport();
            transport.connect("xxx@qq.com","xxxxx");
            // 发送消息
            transport.send(message);
            System.out.println("Sent message successfully....");
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
UYOU
浏览 365回答 1
1回答

喵喔喔

服务器要求加密,你却注释掉了 //prop.setProperty("mail.smtps.ssl.enable", "true"); // 邮件ssl验证
随时随地看视频慕课网APP

相关分类

Java
我要回答