之前做了一个kafka发送短信邮件的项目,用spring来发送邮件遇到好多奇葩的问题。纪录一下,方便以后学习。

必须知道的配置:host地址是smtp地址,邮箱用户名,邮箱密码,这里配置的邮箱用户名必须和后面的发送者一直,不然要报错。报错如图:

Exception in thread "pool-10-thread-6" org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
	com.sun.mail.smtp.SMTPAddressFailedException: 501 5.1.3 Invalid address
; message exception details (1) are:
Failed message 1:
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
	com.sun.mail.smtp.SMTPAddressFailedException: 501 5.1.3 Invalid address

	at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java


或者

Exception in thread "pool-10-thread-5" org.springframework.mail.MailSendException: Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 440 mail from account doesn't conform with authentication (Auth Account:zhaoyong@ukelink.com|Mail Account:fangpeng@ukelink.com)
; message exception details (1) are:
Failed message 1:
com.sun.mail.smtp.SMTPSendFailedException: 440 mail from account doesn't conform with authentication (Auth Account:zhaoyong@ukelink.com|Mail Account:fangpeng@ukelink.com)

	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
	at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:424)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:346)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:341)
	at com.ucloudlink.bss.ucp.service.consumer.EmailsMessageService.sendMailMethod(EmailsMessageService.java:100)
	at com.ucloudlink.bss.ucp.service.consumer.EmailsMessageService.consumerEmail(EmailsMessageService.java:53)
	at com.ucloudlink.bss.ucp.kafka.KafkaProxy.proxyService(KafkaProxy.java:31)
	at com.ucloudlink.bss.ucp.kafka.EmailSmsConsumer$1.run(EmailSmsConsumer.java:73)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)

 遇到这种问题,有可能一下几种可能:

1.可能是网络问题,像我们公司严格限制网络

2.或者是smtp地址不存在

Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
	java.net.SocketException: Software caused connection abort: recv failed. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
	java.net.SocketException: Software caused connection abort: recv failed; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Exception reading response;
  nested exception is:
	java.net.SocketException: Software caused connection abort: recv failed
	at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2202)
	at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2072)
	at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1899)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
	at javax.mail.Service.connect(Service.java:295)
	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:397)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:346)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:341)
	at com.cloulink.test.ReadEmail.send(ReadEmail.java:36)
	at com.cloulink.test.ReadEmail.main(ReadEmail.java:16)
Caused by: java.net.SocketException: Software caused connection abort: recv failed
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:168)
	at java.net.SocketInputStream.read(SocketInputStream.java:121)
	at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
	at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
	at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2182)
	... 9 more
       记得要配置javaMailProperties.put("mail.smtp.auth", true);
javaMailProperties.put("mail.smtp.starttls.enable", true);

(spring mail 具体用法参照官网:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mail-usage)

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐