我正在尝试使用端口587和我的Gmail帐户发送电子邮件,下面是代码。如果我在我的公司网络之外,我的代码工作正常。当我在防火墙/代理后面时,它无法正常工作。我也无法ping到smtp.gmail.com。我也尝试使用我的公司电子邮件ID,这也是同样的问题。
当587被阻止时,有没有办法从防火墙后面发送电子邮件?
package otel;
import java.io.IOException;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) throws IOException {
int MINUTES = 2; // The delay in minutes
Timer timer = new Timer();
timer.schedule(new TimerTask(){
@Override
public void run() { // Function runs every MINUTES minutes.
// Run the code you want here
// CLASSB.funcb(); // If the function you wanted was static
// System.out.println("This is a timer test");
// 1000 milliseconds in a second * 60 per minute * the MINUTES variable.
System.setProperty("http.proxyHost", "xxxxxxxxxxxxx");
System.setProperty("http.proxyPort", "80");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");
// settings proxy credentials
System.setProperty("http.proxyUser", "xxxxxx");
System.setProperty("http.proxyPassword", "xxxxxxxx");
// HTTPS
System.setProperty("https.proxyHost", "xxxxxxxxx");
System.setProperty("https.proxyPort", "443");
System.setProperty("https.proxyUser", "xxxxxxx");
System.setProperty("https.proxyPassword", "xxxxxsxxx");
final String username = "gmailuserid";
final String password = "gmailpassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("userid@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("userid@gmail.com"));
message.setSubject("test mail");
// otelInfo otelInfo = new otelInfo();
String result2 = otel.otelInfo.readResults();
// System.out.println(result2);
message.setText("Dear Mail Crawler,"+ result2);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException | IOException e) {
throw new RuntimeException(e);
}
}
}, 0, 1000 * 60 * MINUTES);
}}
以下是错误日志:
Exception in thread "Timer-0" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at otel.SendMailTLS$1.run(SendMailTLS.java:76)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2118)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:712)
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 javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at otel.SendMailTLS$1.run(SendMailTLS.java:71)
... 2 more
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
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:2084)
... 9 more