对不起,我有一个问题。 在JavaMail(1.4.7)的Android邮件发送编程开发中,MessagingException与SMTPTransport连接,程序异常终止。 在使用3G和LTE时我没有成功,但在使用Wi-Fi超过十秒后失败了。
源代码如下。
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "false");
Session session = Session.getInstance(props);
session.setDebug(true);
SMTPTransport transport = new SMTPTransport(session, null);
transport.connect("smtp.gmail.com", 587, userEmail, null);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail, oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);
return transport;
错误如下
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
nested exception is:
java.net.ConnectException: failed to connect to smtp.gmail.com/2404:6800:4008:c01::6c (port 587): connect failed: ECONNREFUSED (Connection refused)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:288)