java - Javax.mail : Connection timed out only when connected to WiFi -
i'm creating app, sends email gmail address automatically when opened (no need open other default email sender app device(i.e gmail)).
i've followed this link creating application.
it works when device connected 4g data pack. when connect device wifi, every time gives connection timed out
. complete error log is:
w/system.err: javax.mail.messagingexception: not connect smtp host: smtp.gmail.com, port: 465; w/system.err: nested exception is: w/system.err: java.net.connectexception: failed connect smtp.gmail.com/2404:6800:4003:c02::6d (port 465): connect failed: etimedout (connection timed out) w/system.err: @ com.sun.mail.smtp.smtptransport.openserver(smtptransport.java:1391) w/system.err: @ com.sun.mail.smtp.smtptransport.protocolconnect(smtptransport.java:412) w/system.err: @ javax.mail.service.connect(service.java:310) w/system.err: @ javax.mail.service.connect(service.java:169) w/system.err: @ javax.mail.service.connect(service.java:118) w/system.err: @ javax.mail.transport.send0(transport.java:188) w/system.err: @ javax.mail.transport.send(transport.java:118) w/system.err: @ purvil12c.email0.task.doinbackground(task.java:22) w/system.err: @ purvil12c.email0.task.doinbackground(task.java:14) w/system.err: @ android.os.asynctask$2.call(asynctask.java:292) w/system.err: @ java.util.concurrent.futuretask.run(futuretask.java:237) w/system.err: @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) w/system.err: @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) w/system.err: @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) w/system.err: @ java.lang.thread.run(thread.java:818) w/system.err: caused by: java.net.connectexception: failed connect smtp.gmail.com/2404:6800:4003:c02::6d (port 465): connect failed: etimedout (connection timed out) w/system.err: @ libcore.io.iobridge.connect(iobridge.java:124) w/system.err: @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183) w/system.err: @ java.net.plainsocketimpl.connect(plainsocketimpl.java:456) w/system.err: @ java.net.socket.connect(socket.java:882) w/system.err: @ java.net.socket.connect(socket.java:825) w/system.err: @ com.sun.mail.util.socketfetcher.createsocket(socketfetcher.java:233) w/system.err: @ com.sun.mail.util.socketfetcher.getsocket(socketfetcher.java:163) w/system.err: @ 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: @ libcore.io.posix.connect(native method) w/system.err: @ libcore.io.blockguardos.connect(blockguardos.java:111) w/system.err: @ libcore.io.iobridge.connecterrno(iobridge.java:137) w/system.err: @ libcore.io.iobridge.connect(iobridge.java:122) w/system.err: ... 21 more
and reference, code following:
gmailsender.java
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"); }}
and i'm performing network operation in background using asynctask. not given in link followed , mentioned above. code of class following:
public class task extends asynctask<mimemessage, integer,long>{ @override protected long doinbackground(mimemessage... strings) { try{ system.out.println("in background"); transport.send(strings[0]); } catch (exception e){ // toast.maketext(c,e.getmessage(),toast.length_long).show(); e.printstacktrace(); } return null; }}
so i'm missing here? there problem in wifi network? internet works other stuff downloading, surfing, etc. i've posted question on same wifi not working sending mail.
thank you.
Comments
Post a Comment