关于发邮件报错535 Error:authentication failed解决方法

博客介绍了Django对接163发送邮件出现535错误的解决方法,一是开启smtp协议,二是使用授权码登录,而非邮箱密码登录,为解决相关信息技术问题提供了有效途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

django 对接163发送邮件出现535错误方法
在这里插入图片描述

1.开启smtp协议
2.使用授权码登录,不要使用邮箱密码登录

### JavaMail 发送 163 企业邮箱认证失败解决方案 在使用 JavaMail 通过 163 企业邮箱发送邮件时,如果遇到 `AuthenticationFailedException` 异常,并提示 `535 Error`,这通常表示身份验证失败。此类问题的常见原因包括用户名或密码错误、SMTP 认证未启用、安全协议配置不正确等。 #### 常见原因与解决方法 1. **检查用户名和密码是否正确** 确保用于登录 SMTP 服务器的用户名和密码准确无误。对于 163 企业邮箱,通常需要使用完整的邮箱地址作为用户名,并确保密码为授权码而非登录密码(某些邮箱服务要求使用专门生成的第三方应用授权码)[^1]。 2. **开启 SMTP 认证功能** 登录 163 企业邮箱后台管理界面,确认已启用 SMTP 发信服务。部分企业邮箱可能默认关闭该功能,需手动开启并配置允许访问的 IP 地址范围。 3. **配置 TLS 加密连接** 163 邮箱要求使用加密方式连接 SMTP 服务器。在创建 `Session` 对象时,应设置以下属性以启用 TLS: ```java Properties props = new Properties(); props.put("mail.smtp.host", "smtp.qiye.163.com"); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.smtp.auth", "true"); ``` 上述配置使用了 SSL/TLS 加密通道,端口 465 是 163 企业邮箱常用的加密 SMTP 端口[^3]。 4. **使用高版本 JavaMail 库** 若使用的是较旧版本的 `javax.mail` 包(如 JavaMail 1.4.x),建议升级至最新版本(如 JavaMail 1.6.x)。新版库对现代加密协议支持更好,可避免因 TLS 版本不兼容导致的认证失败问题[^3]。 5. **实现自定义 `Authenticator` 类** 在创建 `Session` 时,应提供一个继承自 `Authenticator` 的类,并重写其 `getPasswordAuthentication()` 方法以返回正确的用户名和密码凭证: ```java Authenticator auth = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your-email@domain.com", "your-authorization-code"); } }; Session session = Session.getInstance(props, auth); ``` 6. **测试连接与日志输出** 开启 JavaMail 的调试模式有助于排查具体错误信息: ```java session.setDebug(true); ``` 启用后将在控制台输出详细的 SMTP 交互过程,便于定位问题所在。 ### 示例代码:完整发送流程 ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class EmailSender { public static void main(String[] args) { String host = "smtp.qiye.163.com"; String from = "your-email@domain.com"; String password = "your-authorization-code"; String to = "recipient@example.com"; Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false"); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject("Test Email from JavaMail"); message.setText("This is a test email sent using JavaMail."); Transport.send(message); System.out.println("Email sent successfully."); } catch (MessagingException e) { e.printStackTrace(); } } } ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值