[英]Failed to send Mail with Javax.mail
我正在嘗試使用javax.mail API在帶有Java 1.8的Windows 8.1上發送電子郵件,但我一直收到錯誤消息。
這是我寫的方法:
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");
還嘗試使用以下命令在run命令中進行設置:
-Djava.net.preferIPv4stack = true
我可以使用Python發送郵件,但我想使用Java發送郵件。
試圖使用Wireshark捕獲流量,但是服務器之間甚至沒有TCP握手。
還有其他想法嗎?
謝謝
看來您的SMTP服務器可能由於身份驗證問題而拒絕了您的連接。 要對此進行測試,請嘗試從同一台計算機進行手動SMTP會話 ,然后查看結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.