java发送qq邮箱不成功_springboot通过qq邮箱发邮件失败??

本文主要介绍了在使用SpringBoot尝试通过QQ邮箱发送邮件时遇到的问题,包括端口选择错误和连接配置不当导致的异常。作者提供了解决方案,包括使用正确的端口号587和确保配置信息准确无误,最终成功发送邮件。
摘要由CSDN通过智能技术生成

题主贴出的配置和代码有两个问题:

1、端口号。根据QQ邮箱的官方说明,端口号可以是465或587,但我测试的时候465是超时的,587就正常。

2、运行贴出的代码尝试发邮件,会报错,报错信息

java.net.ConnectException: Connection refused: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;

根据这个信息,host和port这两个参数都没有作为参数传入,运行的时候压根就没有连去QQ邮箱的服务器。

因为弄了一轮都发不出,于是归零重新找资料实现。

下面是我的代码,参考的是spring的官方文档。

配置文件沿用题主的。用了springboot,版本号2.2.3.RELEASE。

package zsh.sf_answer1.mail;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.annotation.Configuration;

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Spring Boot发送邮件的步骤: 1. 导入Spring Boot提供的依赖[^1]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 在配置文件中设置邮箱信息: ```properties spring.mail.host=smtp.qq.com spring.mail.port=587 spring.mail.username=your-email@qq.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建一个邮件服务类,用于发送邮件[^2]: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 4. 在需要发送邮件的地方调用邮件服务类的方法发送邮件: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { String to = "recipient@example.com"; String subject = "Test Email"; String text = "This is a test email."; emailService.sendEmail(to, subject, text); return "Email sent successfully."; } } ``` 以上是使用Spring Boot发送邮件的简单示例。你可以根据自己的需求进行相应的配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值