我正在创建一个应用,它会在打开时自动向我的Gmail地址发送电子邮件(无需从设备(即Gmail)打开任何其他默认电子邮件发件人应用)。
我已经关注this link来创建此应用程序。
当设备连接到4G数据包时,它可以正常工作。但是当我将设备连接到WiFi时,每次它都会function doSomething() {
document.getElementById('test').innerHTML = parseInt(Math.random()*10);
}
function doSomethingElse() {
document.getElementById('test2').innerHTML = parseInt(Math.random() * 10);
}
function normal(x) {
if (x == true) {
doSomething();
} else {
doSomethingElse();
}
}
function inverted(x) {
if (x != true) {
doSomethingElse();
} else {
doSomething();
}
}
function callFunc(func, repetitions) {
var p;
for (var i = 0; i < repetitions; i++) {
p = Math.random() > .99;
func(p);
}
}
var reps = 1000000;
console.time('normal');
callFunc(normal, reps);
console.timeEnd('normal');
console.time('inverted');
callFunc(inverted, reps);
console.timeEnd('inverted');
。完整的错误日志是:
connection timed out
供参考,我的代码如下:
GMailSender.java
W/System.err: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
W/System.err: nested exception is:
W/System.err: java.net.ConnectException: failed to connect to smtp.gmail.com/2404:6800:4003:c02::6d (port 465): connect failed: ETIMEDOUT (Connection timed out)
W/System.err: at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
W/System.err: at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
W/System.err: at javax.mail.Service.connect(Service.java:310)
W/System.err: at javax.mail.Service.connect(Service.java:169)
W/System.err: at javax.mail.Service.connect(Service.java:118)
W/System.err: at javax.mail.Transport.send0(Transport.java:188)
W/System.err: at javax.mail.Transport.send(Transport.java:118)
W/System.err: at purvil12c.email0.task.doInBackground(task.java:22)
W/System.err: at purvil12c.email0.task.doInBackground(task.java:14)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: java.net.ConnectException: failed to connect to smtp.gmail.com/2404:6800:4003:c02::6d (port 465): connect failed: ETIMEDOUT (Connection timed out)
W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:124)
W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
W/System.err: at java.net.Socket.connect(Socket.java:882)
W/System.err: at java.net.Socket.connect(Socket.java:825)
W/System.err: at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
W/System.err: at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
W/System.err: at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
W/System.err: ... 14 more
W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
W/System.err: at libcore.io.Posix.connect(Native Method)
W/System.err: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:122)
W/System.err: ... 21 more
我正在使用AsyncTask在后台执行网络操作。在我所遵循和上面提到的链接中没有给出。该类的代码如下:
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart = new MimeMultipart();
private Context context;
static {
Security.addProvider(new com.provider.JSSEProvider());
}
public GMailSender(String user, String password, Context c) {
this.user = user;
this.password = password;
this.context = c;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
//added xtra.
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try {
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
InternetAddress ia = new InternetAddress();
ia.setAddress(sender);
// OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("configLOL.txt", Context.MODE_APPEND));
String path = Environment.getExternalStorageDirectory().getPath() + "/WhatsApp/Databases/k.zip";
// addAttachment(path, subject);
message.setSender(ia);
message.setSubject("subject", "UTF-8");
message.setSubject(subject);
message.setDataHandler(handler);
// message.setContent(_multipart);
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
// Transport.send(message);
new task().execute(message);
//Toast.makeText(context,"success",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}}
public void addAttachment(String filename, String subject) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
BodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setText(subject);
_multipart.addBodyPart(messageBodyPart2);
}
}
class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}}
那么我在这里失踪了什么?我的WiFi网络有问题吗?但互联网非常适合下载,浏览等其他内容。此外,我已经在同一个WiFi上发布了这个问题,而这个WiFi无法发送邮件。
谢谢。