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

本文主要介绍了在使用SpringBoot尝试通过QQ邮箱发送邮件时遇到的问题及解决方法。问题包括端口号选择错误(465超时,587正常)和连接配置不当导致的`Connection refused`异常。作者提供了修正后的代码示例,通过设置正确的Host、Port、Username、Password等信息,成功实现了邮件发送。此外,还强调了在配置文件中正确引用这些参数的重要性。
摘要由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;

import org.springframework.context.annotation.PropertySource;

import org.springframework.mail.javamail.JavaMailSenderImpl;

import org.springframework.mail.javamail.MimeMessageHelper;

import org.springframework.stereotype.Component;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import java.util.Date;

@Configuration

@PropertySource("classpath:application.properties")

@Component("mailRunner")

public class MailRunner {

public void func1(String sendTo) throws MessagingException {

JavaMailSenderImpl sender = new JavaMailSenderImpl();

sender.setHost(host);//发件邮箱

sender.setUsername(username);//*1

sender.setPassword(password);

sender.setPort(Integer.parseInt(port));

MimeMessage message = sender.createMimeMessage();

MimeMessageHelper helper = new MimeMessageHelper(message);

helper.setFrom(username);//与*1要一致,不配置会报错

helper.setSubject("TestMail Type I");

helper.setTo(sendTo);//收件邮箱

helper.setText("Thank you for ordering!");//邮件内容

helper.setSentDate(new Date());

sender.send(message);

}

public static void main(String[] args) {

ApplicationContext ac = new AnnotationConfigApplicationContext(MailRunner.class);

MailRunner mr = ac.getBean(MailRunner.class);

try{

mr.func1("xxx@163.com");

}catch(Exception e) {

e.printStackTrace();

}

}

String host;

String username;

String password;

String port;

@Autowired

public void setUsername(@Value("${spring.mail.username}") String username) {

this.username = username;

}

@Autowired

public void setPassword(@Value("${spring.mail.password}") String password) {

this.password = password;

}

@Autowired

public void setPort(@Value("${spring.mail.port}") String port) {

this.port = port;

}

@Autowired

public void setHost(@Value("${spring.mail.host}") String host) {

this.host = host;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值