腾讯企业邮箱阿里云端口25发送失败

写一个企业邮箱发送的问题,每个用户都能发送邮件,这个还是比较好写的,百度一大推,但是我是在本地测试的,我把代码部署到阿里云上去之后,一直发送失败,查看日志文件,说连接腾讯邮箱服务器失败,端口使用的是25,后面经过资料查找,说什么阿里云不支持25端口,要使用ssl的465.后面经过测试果然ok.记录一下.

每个用户都能够发送邮件,为了密码安全,全部在腾讯企业邮箱设置客户端安全密码

@Data
public class MailBean {
    private String[] to; // 收件人
    private String[] cc;//抄送人
    private String from; // 发件人
    private String host; // SMTP主机
    private String username; // 发件人的用户名
    private String password; // 发件人的密码
    private String subject; // 邮件主题
    private String content; // 邮件正文
    private InputStream fileInputStream; // 文件流
    private String filename; // 附件的文件名



}

public boolean sendMail(MailBean mb) throws IOException {
        String host = mb.getHost();
        final String password = mb.getPassword();
        final String from = mb.getFrom();
        String[] to = mb.getTo();
        String[] cc=mb.getCc();
        String subject = mb.getSubject();
        String content = mb.getContent();
        String fileName = mb.getFilename();
        InputStream file = mb.getFileInputStream();

        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", "smtp");
        //服务器
        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        //端口
        prop.setProperty("mail.smtp.port", "465");
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, password);
            }
        });
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];//添加收件人
            for(int i=0;inew InternetAddress(to[i]);
            }

            InternetAddress[] ccAddress = new InternetAddress[cc.length];//添加抄送人
            for(int i=0;inew InternetAddress(cc[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, toAddress);
            msg.setRecipients(Message.RecipientType.CC, ccAddress);
            msg.setSubject(toChinese(subject));

            Multipart mp = new MimeMultipart();
            MimeBodyPart body = new MimeBodyPart();
            body.setContent(content, "text/html; charset=UTF-8");
            mp.addBodyPart(body);
            /* 往邮件中添加附件 */
            if (file != null) {
                MimeBodyPart mbpFile = new MimeBodyPart();
                DataSource dataSource=new ByteArrayDataSource(file, "application/png");
                DataHandler dataHandler=new DataHandler(dataSource);;
                mbpFile.setDataHandler(dataHandler);
                mbpFile.setFileName(fileName);
                mp.addBodyPart(mbpFile);
            }
            msg.setContent(mp);
            msg.setSentDate(new Date());
            Transport.send(msg);
        } catch (MessagingException me) {
            me.printStackTrace();
            logger.error(me.getMessage());
            return false;
        }
        return true;
    }

记录一下!

你可能感兴趣的:(SpringBoot,ssl,企业邮箱)