0

I use smtp.mail.ru as smtp server and this server corresponds to multiple IP addresses(based on my experiments different amount of servers from differernt networks) and one of those servers is ipv6. I manually iterate over those servers and when I try to send email using ipv6 - it leads to exception:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 2a00:1148:db00:0:0:0:0:8, 465; timeout -1;
  nested exception is:
    java.net.SocketException: Network unreachable. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 2a00:1148:db00:0:0:0:0:8, 465; timeout -1;
  nested exception is:
    java.net.SocketException: Network unreachable

My code for taking ip addressse:

InetAddress.getAllByName(hostName); // host = "smtp.mail.ru: here
log.warn("Read addresses {}", Arrays.asList(addresses));

result is:

2023-06-22 16:37:09.216  WARN 1 --- [           main] r.v.u.e.s.MyClasss            : Read addresses [smtp.mail.ru/94.100.180.160, smtp.mail.ru/217.69.139.160, smtp.mail.ru/2a00:1148:db00:0:0:0:0:8]

And code for mail send:

@Autowired
private JavaMailSenderImp sender;
...
public void foo() {
     ...
     sender.setHost(getNextAddress());
     sender.send(message);
     ...
 }

...

private String getNextAddress(){
   InetAddress[] all = InetAddress.getAllByName(hostName); //hostName= "smtp.mail.ru: here
   //logic for index calcualtion
   return all[index].getHostAddress()
}

How can I avoid it ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

0 Answers0