SpringBoot使用GoDaddy域名Office365 SMTP发邮件认证成功后报错
解决GoDaddy+Office365 SMTP发送邮件501无效地址问题
问题原因
从调试日志能明确看到,SMTP认证成功后,发送的MAIL FROM地址是<my_laptop@[192.168.1.72]>——这是JavaMail自动生成的本地主机地址,Office365的SMTP服务器不接受这种格式的发件人,必须使用你用于认证的合法邮箱地址my-name@my-business.co.uk。
修复方法
方法1:构建邮件时显式指定发件人
在创建邮件消息的代码中,强制设置发件人地址,确保与SMTP认证的用户名一致:
MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); // 显式设置发件人,必须和认证邮箱匹配 helper.setFrom("my-name@my-business.co.uk"); helper.setTo("收件人邮箱@example.com"); helper.setSubject("邮件主题"); helper.setText("邮件内容"); mailSender.send(message);
方法2:在JavaMail配置中全局指定发件人
直接在配置代码里添加mail.smtp.from属性,避免JavaMail自动生成无效地址:
private static void godaddy(JavaMailSenderImpl mailSender) { mailSender.setHost("smtp.office365.com"); mailSender.setPort(587); mailSender.setUsername("my-name@my-business.co.uk"); mailSender.setPassword("my-password"); Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.starttls.enable", "true"); javaMailProperties.put("mail.smtp.auth", "true"); javaMailProperties.put("mail.transport.protocol", "smtp"); javaMailProperties.put("mail.debug", "true"); javaMailProperties.put("mail.smtp.ssl.enable", "false"); javaMailProperties.put("mail.smtp.ssl.trust", "*"); // 添加这行,强制指定发件人地址 javaMailProperties.put("mail.smtp.from", "my-name@my-business.co.uk"); mailSender.setJavaMailProperties(javaMailProperties); }
原问题配置代码
private static void godaddy(JavaMailSenderImpl mailSender) { mailSender.setHost("smtp.office365.com"); mailSender.setPort(587); mailSender.setUsername("my-name@my-business.co.uk"); mailSender.setPassword("my-password"); Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.starttls.enable", "true"); javaMailProperties.put("mail.smtp.auth", "true"); javaMailProperties.put("mail.transport.protocol", "smtp"); javaMailProperties.put("mail.debug", "true"); javaMailProperties.put("mail.smtp.ssl.enable", "false"); javaMailProperties.put("mail.smtp.ssl.trust", "*"); mailSender.setJavaMailProperties(javaMailProperties); }
原错误日志
STARTTLS 220 2.0.0 SMTP server ready EHLO 192.168.1.72 250-LO4P265CA0073.outlook.office365.com Hello [86.145.143.97] 250-SIZE 157286400 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-AUTH LOGIN XOAUTH2 250-8BITMIME 250-BINARYMIME 250-CHUNKING 250 SMTPUTF8 DEBUG SMTP: Found extension "SIZE", arg "157286400" DEBUG SMTP: Found extension "PIPELINING", arg "" DEBUG SMTP: Found extension "DSN", arg "" DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" DEBUG SMTP: Found extension "AUTH", arg "LOGIN XOAUTH2" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Found extension "BINARYMIME", arg "" DEBUG SMTP: Found extension "CHUNKING", arg "" DEBUG SMTP: Found extension "SMTPUTF8", arg "" DEBUG SMTP: protocolConnect login, host=smtp.office365.com, user=my-name@my-business.co.uk, password= DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 DEBUG SMTP: Using mechanism LOGIN DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN succeeded DEBUG SMTP: use8bit false MAIL FROM:<my_laptop@[192.168.1.72]> 501 5.1.7 Invalid address DEBUG SMTP: got response code 501, with response: 501 5.1.7 Invalid address
内容的提问来源于stack exchange,提问作者Nava
相关产品推荐
相关产品推荐

