无法从Gmail接收邮件

时间:2012-03-22 11:32:38

标签: android gmail javamail

我一直在尝试从Gmail帐户接收邮件。但我不能这样做。我阅读了Stack Overflow中的所有问题并尝试了所有答案,但它没有用。

昨天我找到了这个网站:http://groups.google.com/a/googleproductforums.com/forum/#!category-topic/gmail/reading-and-receiving-messages/XD0C4sw9K7U

根据网站;在Gmail中接收邮件已经没有办法了。

我的第一个问题是;用于从Gmail帐户接收邮件的Java代码太多。使用这些代码,我可以在控制台上看到我的邮件内容。为什么我们无法在Android应用程序中接收邮件?或者如果我们可以在Android应用程序中接收邮件。我们怎么做到这一点?

我的第二个问题是:如果我无法从Gmail帐户接收邮件,我可以从Hotmail帐户或其他邮件帐户接收邮件吗?

5 个答案:

答案 0 :(得分:0)

尝试这一个代码

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

public class Mail extends javax.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;

    private boolean _auth;

    private boolean _debuggable;

    private Multipart _multipart;

    public Mail() {
        _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 = new MimeMultipart();

        // There is something wrong with MailCap, javamail can not find a
        // handler for the multipart/mixed part, so this bit needs to be added.
        MailcapCommandMap mc = (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);
    }

    public Mail(String user, String pass) {
        this();

        _user = user;
        _pass = pass;
    }

    public void setTo(String[] toArr) {
        _to = toArr;
    }

    public void setFrom(String from) {
        _from = from;
    }

    public void setSubject(String subject) {
        _subject = subject;
    }

    /** method will be check email validation **/
    public boolean send() throws Exception {
        Properties props = _setProperties();

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

            MimeMessage msg = new MimeMessage(session);

            msg.setFrom(new InternetAddress(_from));

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

            msg.setSubject(_subject);
            msg.setSentDate(new Date());

            // setup message body
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(_body);
            _multipart.addBodyPart(messageBodyPart);

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

            // send email
            Transport.send(msg);

            return true;
        } else {
            return false;
        }
    }

    /** This method(addAttachment) will allow attach the file in mail **/
    public void addAttachment(String filename) throws Exception {
        BodyPart messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);

        _multipart.addBodyPart(messageBodyPart);
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(_user, _pass);
    }

    private Properties _setProperties() {
        Properties props = new Properties();

        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 setters
    public String getBody() {
        return _body;
    }

    public void setBody(String _body) {
        this._body = _body;
    }

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

并添加到

  1. mail.jar
  2. activation.jar

答案 1 :(得分:0)

您没有说您是否在JavaMail FAQ上找到了代码。互联网上有很多旧的和坏的代码,但常见问题解答中的代码已知有效。尽管如此,听起来你已经有了在控制台上运行的代码,所以这可能是Android特有的问题。由于Android不是Java,因此总是很难知道可能出现的问题。 JavaMail FAQ还有调试提示,以防您没有找到它们。特别是,协议跟踪可能会提供其他人帮助您调试问题所需的信息。

答案 2 :(得分:0)

String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties pr =  new Properties();
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 {
                Session session = Session.getDefaultInstance(pr, null);
                Store store = session.getStore();
                store.connect("username", "password");
                if(store.isConnected()==true)
                {
                System.out.println("Connection is OK");
                }
//              Folder inbox = store.getFolder("INBOX");
                Folder inbox = 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);
                }

使用此代码,我只能转到“store.connect”行。在这一行,应用程序意外停止。所以,我甚至无法连接。 在Eclipse上使用mail.jar,Android Project

答案 3 :(得分:0)

我在阅读一些gmail消息时遇到了问题,所以我问了一个关于here的问题。问题中的代码可以为您提供一个很好的工作示例。

是的,您可以从任何支持pop3或imap访问的服务器上阅读邮件。 hotmail的示例,只需将主机更改为pop3.live.com,将协议更改为pop3s。 希望有所帮助!!

答案 4 :(得分:0)

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

您还将学习如何使用HTTP协议与Web服务器通信,以便下载文本和二进制数据。本章的最后一部分将向您展示如何解析XML文件以提取XML文件的相关部分 - 这种技术在您访问Web服务时非常有用。