我尝试使用javax.mail API在Windows 8.1上使用java 1.8发送电子邮件,但我一直收到错误消息。
这是我写的方法:
public void sendAlertMail(String from, String to, String header, String msgBody){
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", smptHost);
props.put("mail.smtp.port", smptPort);
props.put("mail.smtp.starttls","true");
Session mailSession;
if (authenticate) {
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
mailSession = Session.getInstance(props, auth);
}
else{
mailSession = Session.getInstance(props);
}
// uncomment for debugging infos to stdout
// mailSession.setDebug(true);
Transport transport = null;
try {
transport = mailSession.getTransport();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
MimeMessage message = new MimeMessage(mailSession);
try {
message.addHeaderLine(header);
message.setContent(msgBody, "text/plain");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
} catch (MessagingException e) {
}
try {
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
我收到的错误是:
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:2100)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at tapperSolution.mail.MailSender.sendAlertMail(MailSender.java:64)
at tapperSolution.mail.MailSender.sendAlertMail(MailSender.java:74)
at tapperSolution.mail.MailSender.main(MailSender.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.net.SocketException: Permission denied: connect\at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
... 12 more
我关闭了防火墙和防病毒软件,并尝试使用以下代码在我的代码中设置VM选项:
System.setProperty("java.net.preferIPv4Stack", "true");
还尝试使用以下命令在运行命令中设置它:
-Djava.net.preferIPv4stack =真
我能够使用Python发送邮件,但我想使用Java来发送邮件。
尝试使用Wireshark捕获流量,但服务器甚至没有TCP握手。
还有其他想法吗?
由于