0

I have written this code to send mail from java application--

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");

String host = "smtp.gmail.com";
int port = 587;
String userName = "xxx";
String password = "xxx";

String mailTo = "xx@gmail.com";
String subject = "mail content";

JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setJavaMailProperties(props);
sender.setHost(host);
sender.setPort(port);
sender.setUsername(userName);
sender.setPassword(password);

MimeMessage message = sender.createMimeMessage();
MimeMessageHelper helper;
try {
    helper = new MimeMessageHelper(message, true);
    helper.setTo(mailTo);
    helper.setSubject(subject);
    helper.setText("test test");
} catch (MessagingException e) {
    throw new RuntimeException(e);
}

sender.send(message);

When I am running this program from one laptop , it is working fine .But when I try running it from another laptop , it is giving following errors -

Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect; message exception details (1) are:
Failed message 1:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722)
    at javax.mail.Service.connect(Service.java:342)
    at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
    at com.dailycodebuffer.SpringEmailClient.SpringEmailClientApplication.main(SpringEmailClientApplication.java:50)
Caused by: java.net.SocketException: Permission denied: connect
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:576)
    at java.base/sun.nio.ch.Net.connect(Net.java:565)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333)
    at java.base/java.net.Socket.connect(Socket.java:645)
    at java.base/java.net.Socket.connect(Socket.java:595)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:335)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:214)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2160)
    ... 7 more

How to overcome these errors on another laptop ?

Locke
  • 7,626
  • 2
  • 21
  • 41
  • You could try using port 465. According to [Google's Documentation](https://developers.google.com/gmail/imap/imap-smtp), port 465 is the recommended port if you are not sending a plain text message before TLS begins. That being said, I doubt this is the cause of the issue. – Locke Nov 30 '21 at 18:01
  • According to [this question](https://stackoverflow.com/q/12901475/5987669), you may be able to fix this issue by adding this property (`System.setProperty("java.net.preferIPv4Stack", "true")`) or the equivalent command line argument. – Locke Nov 30 '21 at 18:06
  • Are you using google workspace, or "plain" gmail? Workspace provides some flexibility in the requirements that are imposed on servers sending SMTP: https://support.google.com/a/answer/2520500 – Gus Nov 30 '21 at 18:18
  • Switching to port 465 and adding following line of code solved the problem .Properties props = new Properties(); props.put("mail.smtp.ssl.enable", "true"); – Hrishikesh Shinde Nov 30 '21 at 18:19
  • Permission denied should lead you to suspect the second laptop is configured to disallow such a connection, maybe a port based restriction – erik258 Dec 01 '21 at 02:37

0 Answers0