Java电子邮件客户端应用程序android:" javax.mail.MessagingException:主机未解析:imap.gmail.com"

时间:2015-11-04 11:16:58

标签: java android email

我正在创建一个电子邮件客户端应用程序。 在登录期间,我尝试使用IMAPHOST(示例imap.gmail.com)传递字符串,但每当我尝试服务器回答我时,都会返回此错误:

javax.mail.MessagingException: Host is unresolved: imap.gmail.com ;
有人可以帮帮我吗?这是代码的一部分:

  public class Mail extends javax.mail.Authenticator implements Parcelable {
    private String user;
    private String pass;

    private String port;
    private String sport;

    private String smtpHost;
    private String imapHost;

    private boolean auth;

    private boolean debuggable;

    private Multipart multipart;


    private String _ID;

    public Mail() {

        smtpHost = "";
        imapHost = "";

        port = "465"; // default smtp port
        sport = "465"; // default socketfactory port

        user = ""; // username
        pass = ""; // password

        ID = "";

        debuggable = false; // debug mode on or off - default off
        auth = true; // smtp authentication - default on

       multipart = new MimeMultipart();


    public Mail(String smtpHost,String imapHost,String user, String pass) {
        this();
        smtpHost = smtpHost;
        imapHost = imapHost;
        user = user;
        pass = pass;
    }

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

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

        props.put("mail.trasport.protocol", "smtp");
        props.put("mail.smtp.host", smtpHost);

        props.put("mail.store.protocol", "imaps");
        props.put("mail.imaps.host", imapHost);

        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;
    }


    public boolean loginIsValid(){

        Store store = null;

        try {
            store = this.getStore();
            store.connect();

            // No Exception thrown, Successful login.
            // Close service connection until actual use
            store.close();

        } catch (AuthenticationFailedException e) {
            return false;

        } catch (MessagingException e) {
            //TODO Handle correctly
            e.printStackTrace();
            System.exit(1);
        }

        return true;
    }


    private Store getStore() throws NoSuchProviderException {
        Properties props = this._setProperties();

        Session session = Session.getInstance(props, this);
        return session.getStore("imaps");
    }

  }

1 个答案:

答案 0 :(得分:0)

我在我的应用程序中遇到了同样的错误。我看到错误是由于我以前运行应用程序时没有关闭存储连接。

你应该尝试的解决方案

请在移动设备的任务管理器中终止该应用程序并再次运行。我还建议你调用store.close()方法。