最近在做一个接口项目,需要捕获所有的异常信息,并发送邮件给管理员。使用了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 host25。首先想到的是linux的端口25没有开通,遂联系相关人员开通,但是可能出于安全考虑没有同意开通。 
没办法,只能更换端口了,需要对配置进行修改 
原始配置

  1. spring.mail.host=220.181.15.111 #smtp.126.com 的ip地址
  2. spring.mail.username=邮箱
  3. spring.mail.password=权限密码
  4. spring.mail.default-encoding=UTF-8

修改为

  1. spring.mail.host=220.181.15.111
  2. spring.mail.username=邮箱
  3. spring.mail.password=权限密码
  4. spring.mail.default-encoding=UTF-8
  5. spring.mail.properties.mail.smtp.auth=true
  6. spring.mail.properties.mail.smtp.starttls.enable=true
  7. spring.mail.properties.mail.smtp.starttls.required=true
  8. spring.mail.port=465
  9. spring.mail.properties.mail.smtp.socketFactory.port = 465
  10. spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
  11. spring.mail.properties.mail.smtp.socketFactory.fallback = false

之后就可以正常发送邮件了。 
而且在linux中可能会出现无法解析域名的问题,这时候可以将smtp主机域名修改为ip地址。ip地址可以使用命令行的ping命令来获取,如下: 


顺便讲下spring boot 使用JavaMail发送邮件的方式吧。 
首先添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-mail</artifactId>
  4. </dependency>

然后在配置文件(application.properties)中添加mail配置

  1. spring.mail.host=220.181.15.111
  2. spring.mail.username=邮箱
  3. spring.mail.password=权限密码
  4. spring.mail.default-encoding=UTF-8
  5. spring.mail.properties.mail.smtp.auth=true
  6. spring.mail.properties.mail.smtp.starttls.enable=true
  7. spring.mail.properties.mail.smtp.starttls.required=true
  8. spring.mail.port=465
  9. spring.mail.properties.mail.smtp.socketFactory.port = 465
  10. spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
  11. spring.mail.properties.mail.smtp.socketFactory.fallback = false
  1. @Autowired
  2. JavaMailSender mailSender; //添加依赖后可以直接引用
  3. public void sendMail(String emailForm,String[] emailTo,String title,String context){
  4. MimeMessage mimeMessage = mailSender.createMimeMessage();
  5. MimeMessageHelper helper;
  6. try {
  7. helper = new MimeMessageHelper(mimeMessage, true);
  8. helper.setFrom(emailForm);
  9. helper.setTo(emailTo);
  10. helper.setSubject(title);//主题
  11. helper.setText(context);//正文
  12. mailSender.send(mimeMessage);
  13. } catch (MessagingException e1) {
  14. // TODO Auto-generated catch block
  15. e1.printStackTrace();
  16. }
  17. }

com.sun.mail.util.MailConnectException: Couldn't connect to host异常解决相关推荐

  1. 用MyEclipse测试发送email时报java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

    java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream的异常时, 是因为MyEclipse8.5自带的javamail版本较 ...

  2. MailConnectException: Couldn‘t connect to host

    SpringBoot 使用JavaMail发送邮件报错MailConnectException: Couldn't connect to host, port: smtp.qq.com, 465; t ...

  3. javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决

    javax.mail.MessagingException: 501 Syntax: HELO hostname Linux端异常解决 参考文章: (1)javax.mail.MessagingExc ...

  4. cannot connect to host的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 下午更新源码,出现下面的错误: 通过ping来测试svn服务器的连接,发现可以连接得通,于是猜测可以服务器的svn服务 ...

  5. Linux环境邮件服务报错:Couldn‘t connect to host, port: smtp.qq.com, 25; timeout -1;

    在Windows环境下邮件服务能够正常使用,但是在Linux环境下,邮件服务报错,无法发送邮件. 具体报错信息如下: com.sun.mail.util.MailConnectException: C ...

  6. php soap proxy host,php-PHP soap Could not connect to host

    function getResult() { $url = "http://bcsz.xms.foxhis.com:8080/ItfServerWS/XmsWS?wsdl";//正 ...

  7. com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User

    使用springboot集成的邮件发送功能,参考了文章:https://blog.csdn.net/gfd54gd5f46/article/details/77414560 无论是SimpleMail ...

  8. com.sun.mail.smtp.protected void rcptTo() 方法

    RFC 822 RFC 822是电子邮件的标准格式,电子邮件除了是由一个Internet用户传递给另一个用户的信息之外,还必须包含附加的服务信息,这两个部分加在一起叫做电子邮件的标准格式,外文简称RF ...

  9. 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 ...

最新文章

  1. 湖南大学让晶体管小至3纳米,沟道长度仅一层原子 | Nature子刊
  2. NIO通信,仿QQ基本聊天业务,交流篇
  3. Redis 特殊数据类型 :Geospatial、Hyperloglog、Bitmap
  4. Leecode19. 删除链表的倒数第 N 个结点——Leecode大厂热题100道系列
  5. 给 EF Core 查询增加 With NoLock
  6. C语言数组查找(线性查找 折半查找)
  7. 计算机应用作业2,计算机应用2作业
  8. facebook开源的prophet时间序列预测工具---识别多种周期性、趋势性(线性,logistic)、节假日效应,以及部分异常值
  9. Java-封装继承多态
  10. matlab求条件概率密度_数值优化方法—迭代法amp;终止条件
  11. 777后无效 执行chmod_执行chmod -R 777 / 补救
  12. 公众号如何涨粉?做微信seo疯狂涨粉技巧
  13. Harmony OS har包生成和使用技巧
  14. 列主元消去法例题详解_列主元消去法
  15. 笑话大全API 实战项目 开心一笑app
  16. 打开keil提示未安装xx器件包,实际上已经安装
  17. 如何有效的清理c盘文件?真实有效
  18. Android接入穿山甲SDK并实现开屏广告
  19. 教师计算机培训心得博客,教师研修心得体会博客
  20. 由一位坛友的布局想到的定位问题:absolute和relative

热门文章

  1. android fragment加载布局的方式,Android中Fragment的加载方式与数据通信详解
  2. 凯诺克斯品牌百科介绍
  3. 5款强大的开源报表工具!
  4. python 舍去小数_python小数的进位与舍去
  5. java swing 获当前日期_Java图形界面Swing下日期控件
  6. 07---布儒斯特角
  7. 蝉游记创业一周年流水帐
  8. uniapp 中点击某个按钮关注成功 操作
  9. 选修课论文(蓝宝石)
  10. ARM Cortex-M系列之中断向量表