0

I dont know y i am unable to send mail from tab. It gives exception saying cud not communicate, network unreachable. 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();
static {
    Security.addProvider(new JSSEProvider());
}
public GMailSender(String user, String password) {
    this.user = user;
    this.password = password;
    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");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");
    props.setProperty("mail.smtp.ssl.enable", "true");

    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"));
        message.setSender(new InternetAddress(sender));
        message.setSubject(subject);
        message.setDataHandler(handler);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(body);
        _multipart.addBodyPart(messageBodyPart);
        // Put parts in message
        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);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

 public 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");
    }
}
}

I called this from activity as below.

GMailSender gMailSender=new GMailSender("user@abc.com","passwd");
gMailSender.sendMail(subject,message,"user@abc.com","to@abc.com");

Any suggestion wil be very helpful.

AndroidNwB
  • 5
  • 1
  • 5
  • java.net.ConnectException: failed to connect to smtp.gmail.com/2404:6800:4003:c00::6d (port 587) after 90000ms: isConnected failed: ENETUNREACH (Network is unreachable) 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:310) – AndroidNwB May 01 '17 at 09:57
  • at javax.mail.Service.connect(Service.java:169) at javax.mail.Service.connect(Service.java:118) at javax.mail.Transport.send(Transport.java:118) at com.example.dell.notificationtest.GMailSender.sendMail(GMailSender.java:88) at com.example.dell.notificationtest.MainActivity$SendEmail.doInBackground(MainActivity.java:160) at com.example.dell.notificationtest.MainActivity$SendEmail.doInBackground(MainActivity.java:145) at android.os.AsyncTask$2.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) – AndroidNwB May 01 '17 at 09:58
  • at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.net.ConnectException: failed to connect to smtp.gmail.com/2404:6800:4003:c00::6d (port 587) after 90000ms: isConnected failed: ENETUNREACH (Network is unreachable) at libcore.io.IoBridge.isConnected(IoBridge.java:262) at libcore.io.IoBridge.connectErrno(IoBridge.java:186) – AndroidNwB May 01 '17 at 09:58
  • Have you tried using IPv4 only? – Stefan Freitag May 01 '17 at 10:10
  • how to find if it is ipv4 or ipv6?.. i ahve just used code from java mail api... only i called the send method passing my user id and pwd subject etc... – AndroidNwB May 01 '17 at 10:27
  • Does this help: http://stackoverflow.com/questions/19062836/android-network-is-unreachable-enetunreach? – Stefan Freitag May 01 '17 at 18:47
  • no.. as i have wifi working in other devices... – AndroidNwB May 02 '17 at 08:30
  • with the same wifi connection, only my mobiule cud send mail. but other devices cud not send mail... – AndroidNwB May 02 '17 at 08:31

0 Answers0