java邮件发送报错: Couldn‘t connect to host, port: smtp.exmail.qq.com, 25; timeout -1

java邮件发送报错: Couldn't connect to host, port: smtp.exmail.qq.com, 25; timeout -1

1,发送邮件代码

public static boolean sendEmail(String from, String to, String userName, String password,
                                    String fileName, ByteArrayOutputStream baos, String subject,String content) {
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器 ->QQ 邮件服务器
        properties.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        properties.put("mail.smtp.auth", "true");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password); //发件人邮件用户名、授权码
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));
            // Set To: 头部头字段
            Address[] internetAddressTo = new InternetAddress().parse(to);
            message.setRecipients(MimeMessage.RecipientType.TO, internetAddressTo);
            // Set Subject: 头部头字段
            message.setSubject(subject);
            /*添加附件*/
            Multipart multipart = new MimeMultipart();
            if (baos != null) {
                MimeBodyPart fileBody = new MimeBodyPart();
                DataSource source = new ByteArrayDataSource(baos.toByteArray(), "application/msexcel");
                fileBody.setDataHandler(new DataHandler(source));
                // 中文乱码问题
                fileBody.setFileName(fileName + ".xlsx");
                multipart.addBodyPart(fileBody);
            }

            //设置正文
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(content,"text/html;charset=utf-8");
            multipart.addBodyPart(text);

            message.setContent(multipart);
            // 发送消息
            Transport.send(message);
            return true;
        } catch (Exception e) {
            logger.error("邮件发送异常2: ",e);
        }
        return false;
    }

2,报错原因

1,发送邮件的时候是有默认端口号的,端口号为:25,但是不管是阿里还是腾讯云都是对这个端口号做了封禁,是不可使用的,所以我们需要修改端口号,使用465作为发送端口,修改后如下

/**
     * @param from     发件人电子邮箱
     * @param to       收件人邮箱
     * @param userName 发件人邮件用户名
     * @param password 授权码
     * @param fileName 文件名称
     * @param baos
     * @return
     */
    public static boolean sendEmail(String from, String to, String userName, String password,
                                    String fileName, ByteArrayOutputStream baos, String subject,String content) {
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器 ->QQ 邮件服务器
        properties.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password); //发件人邮件用户名、授权码
            }
        });

        try {
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);
            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));
            // Set To: 头部头字段
            Address[] internetAddressTo = new InternetAddress().parse(to);
            message.setRecipients(MimeMessage.RecipientType.TO, internetAddressTo);
            // Set Subject: 头部头字段
            message.setSubject(subject);
            /*添加附件*/
            Multipart multipart = new MimeMultipart();
            if (baos != null) {
                MimeBodyPart fileBody = new MimeBodyPart();
                DataSource source = new ByteArrayDataSource(baos.toByteArray(), "application/msexcel");
                fileBody.setDataHandler(new DataHandler(source));
                // 中文乱码问题
                fileBody.setFileName(fileName + ".xlsx");
                multipart.addBodyPart(fileBody);
            }

            //设置正文
            MimeBodyPart text = new MimeBodyPart();
            text.setContent(content,"text/html;charset=utf-8");
            multipart.addBodyPart(text);

            message.setContent(multipart);
            // 发送消息
            Transport.send(message);
            return true;
        } catch (Exception e) {
            logger.error("邮件发送异常2: ",e);
        }
        return false;
    }
上一篇:Linux知识心得20 定时任务


下一篇:yapi漏洞挂马程序chongfu.sh处理