JavaMail API to iMail -- java.net.SocketException: Permission denied: connect
我按照上面的链接建议进行了更改我仍然面临同样的问题我使用的是jdk1.8,但尝试使用JDK 6并将-Djava.net.preferIPv4Stack = true添加到VM选项中
Properties props = System.getProperties();
props.put("mail.host",smtpHostServer );
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smptp.ssl.enable", true);
props.put("mail.smtp.ssl.trust", smtpHostServer);
props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "25");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(props, null);
session.setDebug(true);
EmailUtil.sendEmail(session, emailID,"SimpleEmail Testing Subject", "SimpleEmail Testing Body");
我得到以下日志,不确定它是否与帐户权限有关,因为我使用公司帐户MS交换服务器,例如:abc@xyz.com
`DEBUG: setDebug: JavaMail version 1.5.5
Message is ready
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xxxxx.xxxxx.com", port 25, isSSL false
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: xxxx.xxxxxx.com, 25; timeout -1;
nested exception is:
java.net.SocketException: Permission denied: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:267)
at com.test.spring.mail.EmailUtil.sendEmail(EmailUtil.java:71)
at com.test.spring.mail.MailServiceTest.main(MailServiceTest.java:44)
Caused by: java.net.SocketException: Permission denied: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
... 6 more`