com.sun.mail.util.MailConnectException: Couldn't connect to host异常解决
最近在做一个接口项目,需要捕获所有的异常信息,并发送邮件给管理员。使用了JavaMail来实现,然后发现在本地(windows环境)上可以正常发送邮件,而放到了linux环境上就无法发送邮件。相关错误如下:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 220.181.15.111, 25; timeout -1;nested exception is:java.net.ConnectException: Connection timed out (Connection timed out). Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 220.181.15.111, 25; timeout -1; nested exception is: java.net.ConnectException: Connection timed out (Connection timed out) at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:446) ~[spring-context-support-5.0.6.RELEASE.jar:5.0.6.RELEASE]
重要信息Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host
和25
。首先想到的是linux的端口25没有开通,遂联系相关人员开通,但是可能出于安全考虑没有同意开通。
没办法,只能更换端口了,需要对配置进行修改
原始配置
- spring.mail.host=220.181.15.111 #smtp.126.com 的ip地址
- spring.mail.username=邮箱
- spring.mail.password=权限密码
- spring.mail.default-encoding=UTF-8
修改为
- spring.mail.host=220.181.15.111
- spring.mail.username=邮箱
- spring.mail.password=权限密码
- spring.mail.default-encoding=UTF-8
- spring.mail.properties.mail.smtp.auth=true
- spring.mail.properties.mail.smtp.starttls.enable=true
- spring.mail.properties.mail.smtp.starttls.required=true
- spring.mail.port=465
- spring.mail.properties.mail.smtp.socketFactory.port = 465
- spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
- spring.mail.properties.mail.smtp.socketFactory.fallback = false
之后就可以正常发送邮件了。
而且在linux中可能会出现无法解析域名的问题,这时候可以将smtp主机域名修改为ip地址。ip地址可以使用命令行的ping命令来获取,如下:
顺便讲下spring boot 使用JavaMail发送邮件的方式吧。
首先添加依赖
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-mail</artifactId>
- </dependency>
然后在配置文件(application.properties)中添加mail配置
- spring.mail.host=220.181.15.111
- spring.mail.username=邮箱
- spring.mail.password=权限密码
- spring.mail.default-encoding=UTF-8
- spring.mail.properties.mail.smtp.auth=true
- spring.mail.properties.mail.smtp.starttls.enable=true
- spring.mail.properties.mail.smtp.starttls.required=true
- spring.mail.port=465
- spring.mail.properties.mail.smtp.socketFactory.port = 465
- spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
- spring.mail.properties.mail.smtp.socketFactory.fallback = false
- @Autowired
- JavaMailSender mailSender; //添加依赖后可以直接引用
- public void sendMail(String emailForm,String[] emailTo,String title,String context){
- MimeMessage mimeMessage = mailSender.createMimeMessage();
- MimeMessageHelper helper;
- try {
- helper = new MimeMessageHelper(mimeMessage, true);
- helper.setFrom(emailForm);
- helper.setTo(emailTo);
- helper.setSubject(title);//主题
- helper.setText(context);//正文
- mailSender.send(mimeMessage);
- } catch (MessagingException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
com.sun.mail.util.MailConnectException: Couldn't connect to host异常解决相关推荐
- 用MyEclipse测试发送email时报java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream的异常时, 是因为MyEclipse8.5自带的javamail版本较 ...
- MailConnectException: Couldn‘t connect to host
SpringBoot 使用JavaMail发送邮件报错MailConnectException: Couldn't connect to host, port: smtp.qq.com, 465; t ...
- javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决
javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决 参考文章: (1)javax.mail.MessagingExc ...
- cannot connect to host的解决办法
作者:朱金灿 来源:http://blog.csdn.net/clever101 下午更新源码,出现下面的错误: 通过ping来测试svn服务器的连接,发现可以连接得通,于是猜测可以服务器的svn服务 ...
- Linux环境邮件服务报错:Couldn‘t connect to host, port: smtp.qq.com, 25; timeout -1;
在Windows环境下邮件服务能够正常使用,但是在Linux环境下,邮件服务报错,无法发送邮件. 具体报错信息如下: com.sun.mail.util.MailConnectException: C ...
- php soap proxy host,php-PHP soap Could not connect to host
function getResult() { $url = "http://bcsz.xms.foxhis.com:8080/ItfServerWS/XmsWS?wsdl";//正 ...
- com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User
使用springboot集成的邮件发送功能,参考了文章:https://blog.csdn.net/gfd54gd5f46/article/details/77414560 无论是SimpleMail ...
- com.sun.mail.smtp.protected void rcptTo() 方法
RFC 822 RFC 822是电子邮件的标准格式,电子邮件除了是由一个Internet用户传递给另一个用户的信息之外,还必须包含附加的服务信息,这两个部分加在一起叫做电子邮件的标准格式,外文简称RF ...
- SpringBoot使用JavaMailSender发送邮件:com.sun.mail.smtp.SMTPSendFailedException: 451 MI:SFQ 163 smtp7
SpringBoot使用JavaMailSender发送邮件时,报错如下: 2020-08-08 07:32:21,237 ERROR --- [http-nio-8080-exec-1] cn.co ...
最新文章
- 湖南大学让晶体管小至3纳米,沟道长度仅一层原子 | Nature子刊
- NIO通信,仿QQ基本聊天业务,交流篇
- Redis 特殊数据类型 :Geospatial、Hyperloglog、Bitmap
- Leecode19. 删除链表的倒数第 N 个结点——Leecode大厂热题100道系列
- 给 EF Core 查询增加 With NoLock
- C语言数组查找(线性查找 折半查找)
- 计算机应用作业2,计算机应用2作业
- facebook开源的prophet时间序列预测工具---识别多种周期性、趋势性(线性,logistic)、节假日效应,以及部分异常值
- Java-封装继承多态
- matlab求条件概率密度_数值优化方法—迭代法amp;终止条件
- 777后无效 执行chmod_执行chmod -R 777 / 补救
- 公众号如何涨粉?做微信seo疯狂涨粉技巧
- Harmony OS har包生成和使用技巧
- 列主元消去法例题详解_列主元消去法
- 笑话大全API 实战项目 开心一笑app
- 打开keil提示未安装xx器件包,实际上已经安装
- 如何有效的清理c盘文件?真实有效
- Android接入穿山甲SDK并实现开屏广告
- 教师计算机培训心得博客,教师研修心得体会博客
- 由一位坛友的布局想到的定位问题:absolute和relative