Skip to content Skip to sidebar Skip to footer

Can't Receive Mail From Gmail

I have been trying to receive mail from a gmail account. But I could not do that. I read all questions in Stack Overflow and tried all answers but it didn't work. Yesterday I found

Solution 1:

You didn't say whether you had found the code on the JavaMail FAQ. There's a lot of old and bad code floating around out there on the internet but the code on the FAQ is known to work. Still, it sounds like you've got code that works on the console so maybe this is an Android specific problem. It's always hard to know what could be going wrong there since Android isn't Java. The JavaMail FAQ also has debugging tips, in case you haven't found them. In particular, a protocol trace might provide the information necessary for others to help you debug the problem.

Solution 2:

StringSSL_FACTORY="javax.net.ssl.SSLSocketFactory";
Propertiespr=newProperties();
pr.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
pr.setProperty("mail.pop3.socketFactory.fallback", "false");
pr.setProperty("mail.pop3.port",  "995");
pr.setProperty("mail.pop3.socketFactory.port", "995");
pr.put("mail.pop3.host", "pop.gmail.com");
pr.setProperty("mail.store.protocol", "pop3");

            try {
                Sessionsession= Session.getDefaultInstance(pr, null);
                Storestore= session.getStore();
                store.connect("username", "password");
                if(store.isConnected()==true)
                {
                System.out.println("Connection is OK");
                }
//              Folder inbox = store.getFolder("INBOX");Folderinbox= store.getDefaultFolder().getFolder("INBOX");
                inbox.open(Folder.READ_ONLY);
                Message messages[] = inbox.getMessages();
                for(Message message:messages) 
                {
//              System.out.println(message.getSentDate());
                message.writeTo(System.out);
                }

With this code, I can only move to "store.connect" line. On this line, application stop unexpectedly. So, I couldnt connect even. Using with mail.jar on Eclipse,Android Project

Solution 3:

I had problems reading some gmail messages, so I asked a question regarding that here. The code in the question can be a good working example for you.

And yes, you can read mails from any server that supports pop3 or imap access. Example for hotmail, just change the host to pop3.live.com and protocol to pop3s. Hope that helps!!

Solution 4:

try this one code

Mail m = new Mail(your email_id), your id password ));
String[] toArr = { send email id };
m.setTo(toArr);
m.setFrom(your email_id);
m.setSubject("Out of subject");
m.setBody("Out Of msg ");
try {
  m.send();
// Log.v("TAg", "send Mail");
} catch (Exception e) {
// Log.e("MailApp", "Could not send email", e);
}

mail.java

publicclassMailextendsjavax.mail.Authenticator {
    private String _user;
    private String _pass;

    private String[] _to;
    private String _from;

    private String _port;
    private String _sport;

    private String _host;

    private String _subject;
    private String _body;

    privateboolean _auth;

    privateboolean _debuggable;

    private Multipart _multipart;

    publicMail() {
        _host = "smtp.gmail.com"; // default smtp server
        _port = "465"; // default smtp port
        _sport = "465"; // default socketfactory port

        _user = ""; // username
        _pass = ""; // password
        _from = ""; // email sent from
        _subject = ""; // email subject
        _body = ""; // email body

        _debuggable = false; // debug mode on or off - default off
        _auth = true; // smtp authentication - default on

        _multipart = newMimeMultipart();

        // There is something wrong with MailCap, javamail can not find a// handler for the multipart/mixed part, so this bit needs to be added.MailcapCommandMapmc= (MailcapCommandMap) CommandMap
                .getDefaultCommandMap();
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
        mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
        CommandMap.setDefaultCommandMap(mc);
    }

    publicMail(String user, String pass) {
        this();

        _user = user;
        _pass = pass;
    }

    publicvoidsetTo(String[] toArr) {
        _to = toArr;
    }

    publicvoidsetFrom(String from) {
        _from = from;
    }

    publicvoidsetSubject(String subject) {
        _subject = subject;
    }

    /** method will be check email validation **/publicbooleansend()throws Exception {
        Propertiesprops= _setProperties();

        if (!_user.equals("") && !_pass.equals("") && _to.length > 0
                && !_from.equals("") && !_subject.equals("")
                && !_body.equals("")) {
            Sessionsession= Session.getInstance(props, this);

            MimeMessagemsg=newMimeMessage(session);

            msg.setFrom(newInternetAddress(_from));

            InternetAddress[] addressTo = newInternetAddress[_to.length];
            for (inti=0; i < _to.length; i++) {
                addressTo[i] = newInternetAddress(_to[i]);
            }
            msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);

            msg.setSubject(_subject);
            msg.setSentDate(newDate());

            // setup message bodyBodyPartmessageBodyPart=newMimeBodyPart();
            messageBodyPart.setText(_body);
            _multipart.addBodyPart(messageBodyPart);

            // Put parts in message
            msg.setContent(_multipart);

            // send email
            Transport.send(msg);

            returntrue;
        } else {
            returnfalse;
        }
    }

    /** This method(addAttachment) will allow attach the file in mail **/publicvoidaddAttachment(String filename)throws Exception {
        BodyPartmessageBodyPart=newMimeBodyPart();
        DataSourcesource=newFileDataSource(filename);
        messageBodyPart.setDataHandler(newDataHandler(source));
        messageBodyPart.setFileName(filename);

        _multipart.addBodyPart(messageBodyPart);
    }

    public PasswordAuthentication getPasswordAuthentication() {
        returnnewPasswordAuthentication(_user, _pass);
    }

    private Properties _setProperties() {
        Propertiesprops=newProperties();

        props.put("mail.smtp.host", _host);

        if (_debuggable) {
            props.put("mail.debug", "true");
        }

        if (_auth) {
            props.put("mail.smtp.auth", "true");
        }

        props.put("mail.smtp.port", _port);
        props.put("mail.smtp.socketFactory.port", _sport);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        return props;
    }

    // the getters and setterspublic String getBody() {
        return _body;
    }

    publicvoidsetBody(String _body) {
        this._body = _body;
    }

    // more of the getters and setters …..
}

and add to

  1. mail.jar
  2. activation.jar

Solution 5:

http://blogs.wrox.com/files/2013/05/Chapter-8-9781118087299-2.pdf

You will also learn how to use the HTTP protocol to talk to web servers so that you can download text and binary data. The last part of this chapter shows you how to parse XML files to extract the relevant parts of an XML file — a technique that is useful if you are accessing Web services.

Post a Comment for "Can't Receive Mail From Gmail"