关于阿里云服务器发送邮件被拒接的问题

本文详细介绍了如何使用Java编程语言结合QQ邮箱API实现发送带有HTML格式和附件的邮件,包括设置邮件服务器参数、账号密码验证、创建邮件对象、设置邮件内容及附件路径等关键步骤。
  //方法描述
    @RequestMapping("/sendEmail")
    @ResponseBody
    public void sendEmail(HttpServletResponse response, String email, String vedioPath) throws MessagingException {
        //创建连接对象 连接到邮件服务器
        Properties properties = new Properties();
        //设置发送邮件的基本参数
        //发送邮件服务器(注意,此处根据你的服务器来决定,如果使用的是QQ服务器,请填写smtp.qq.com)
        properties.put("mail.smtp.host", "smtp.qq.com");
        //发送端口(根据实际情况填写,一般均为25)
        // properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        properties.setProperty("mail.smtp.socketFactory.port", "465");
        //设置发送邮件的账号和密码
        javax.mail.Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //两个参数分别是发送邮件的账户和密码(注意,如果配置后不生效,请检测是否开启了 POP3/SMTP 服务,QQ邮箱对应设置位置在: [设置-账户-POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务])
                return new PasswordAuthentication("xxxxxxxxxxxxx@qq.com", "xxxxxxxxxx");
                // return new PasswordAuthentication("xxxxxxxxxxx@qq.com", "xxxxxxxxx");
            }
        });
        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人
        // try {
        message.setFrom(new InternetAddress("xxxxxxxxxx@qq.com"));
        //设置收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //设置主题
        message.setSubject("上网了平台-AE视频订单");
        //设置邮件正文  第二个参数是邮件发送的类型
        message.setContent("<html lang='zh-CN'><head ><meta charset='utf-8'>"
                        + "</head><body>尊敬的客户,您的视频已制作完成,请您下载查阅"
                        + "<a href='xxxxxxxxxxxxxxxx?vedioPath=" + vedioPath + "'>【您的定制视频】</a></body></html>",
                "text/html;charset=utf-8");
        Transport.send(message);
        /*} catch (MessagingException e) {
            System.out.println(e.getMessage());
            writeJsonData(response,
                    new CommonModel(Constants.StatusCode.sendFail.VALUE, Constants.StatusCode.sendFail.MSG));
        }*/
        writeJsonData(response,
                new CommonModel(Constants.StatusCode.Success.VALUE, Constants.StatusCode.Success.MSG));
    }

阿里云QQ邮箱发送端口为 25 默认端口是被禁止的。所以改为465端口采用SSL协议传输邮件

 

### 使用 Telnet 测试 SMTP 连接 可以通过 `telnet` 命令手动测试阿里云服务器与 SMTP 服务器之间的连接。此方法可以快速验证网络层是否允许连接到目标 SMTP 端口(如 25、465 或 587)。 执行以下命令尝试连接: ```bash telnet smtp.aliyun.com 465 ``` 如果连接成功,将显示类似如下内容: ``` Trying xx.xx.xx.xx... Connected to smtp.aliyun.com. Escape character is '^]'. ``` 表示 TCP 层连接已建立,SMTP 服务可达[^1]。 --- ### 使用 OpenSSL 测试 SSL/TLS 握手过程 为了进一步验证 SSL/TLS 握手是否正常,可以使用 `openssl s_client` 工具连接 SMTPS(SMTP over SSL)端口: ```bash openssl s_client -connect smtp.aliyun.com:465 -crlf ``` 该命令会显示完整的 SSL 握手过程和证书信息,有助于排查证书信任、协议版本不兼容等问题[^3]。 --- ### 使用 Python 脚本测试 SMTP 发送流程 通过编写简单的 Python 脚本,可以模拟未认证的邮件发送行为,用于检测是否存在开放中继或基本的 SMTP 功能是否正常。示例代码如下: ```python import smtplib def test_smtp_connection(server, port=25): try: smtp = smtplib.SMTP(server, port, timeout=10) smtp.ehlo() if smtp.has_extn('STARTTLS'): smtp.starttls() smtp.ehlo() smtp.sendmail('test@sender.com', ['test@receiver.com'], 'Test email') smtp.quit() return True except Exception as e: print(f"Error: {e}") return False # 测试连接阿里云 SMTP 服务器 result = test_smtp_connection("smtp.aliyun.com", 465) print("SMTP connection test result:", result) ``` 此脚本可验证从 EHLO 到 TLS 升级再到邮件发送的整个流程,适用于调试 SMTP 协议交互问题[^2]。 --- ### 使用 JavaMail 的调试模式查看详细日志 在 Java 应用中使用 JavaMail 发送邮件时,可通过启用调试模式输出详细的 SMTP 和 SSL 握手日志: ```java Session session = Session.getInstance(props, authenticator); session.setDebug(true); // 启用调试日志 ``` 运行后控制台将打印出完整的通信过程,包括 SSL/TLS 握手细节,便于定位握手失败的具体原因。 --- ### 检查防火墙与安全组配置 确保阿里云服务器的安全组规则允许出站访问 SMTP 所需端口(如 25、465、587)。部分云服务商默认禁止 25 端口的出站流量以防止垃圾邮件传播。此时应优先使用 465 或 587 端口进行测试[^1]。 --- ### 总结 上述方法涵盖了从基础网络连接测试、SSL/TLS 握手验证、SMTP 协议交互测试到应用层调试日志开启等多个方面,能够全面覆盖阿里云服务器上 SMTP 连接测试的需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值