2015-11-04 44 views
0

我正在创建电子邮件客户端应用程序。 在登录我试图通过与IMAPHOST一个字符串(例如imap.gmail.com),但每当我试图服务器回答我,这个错误:Java电子邮件客户端应用程序android:“javax.mail.MessagingException:主机未解析: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"); 
    } 

    } 
+0

btw:你的参数构造函数代码没什么意义 –

回答

0

我在我的应用程序中遇到了同样的错误。我发现错误是由于我以前运行应用程序时商店连接未关闭而导致的。

解决方案,你应该尝试

请杀死你的手机任务管理器应用程序并再次运行。我也建议你调用store.close()方法。

相关问题