关于阿里云服务器上使用javamail失败的解决方案

2 篇文章 0 订阅


今天出现一个奇怪的问题, 本地的邮件能正常发送. 代码上传到阿里云服务器上就是不能成功.


在阿里云社区上看到这么一篇帖子, https://yq.aliyun.com/ask/47591

后台发现写的代码没有使用ssl的连接方式.



Properties props = new Properties();
        // 开启debug调试
        // props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.9meth.com");
        // 发送邮件协议名称
        //props.setProperty("mail.transport.protocol", "smtp");

        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");

        // 设置环境信息
        // Session session = Session.getInstance(props);
        Session session = Session.getDefaultInstance(props, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("mail", "password");
            }
        });



// 创建邮件对象
        Message msg = new MimeMessage(session);
        try {
            msg.setSubject(title);
            // 设置邮件内容
            msg.setText(content);
            // 设置发件人
            msg.setFrom(new InternetAddress("发的邮件"));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(收的邮件, false));
            Transport.send(msg);
        } catch (MessagingException e) {
            e.printStackTrace();
        }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值