android用java邮件api读取交换电子邮件

时间:2011-03-07 11:57:23

标签: android email exchange-server javamail

我想从ms交换机上阅读我的电子邮件,我在这里使用java邮件api http://code.google.com/p/javamail-android/并且我已成功从我的Google帐户中提取电子邮件...交换的问题是我在本地拥有它我没有公共证书,android给了我03-07 13:12:25.708: WARN/System.err(3886): javax.mail.MessagingException: Not trusted server certificate;

我如何通过证书检查?

我从这里看到了Vynayak的例子:Sending Email in Android using JavaMail API without using the default/built-in app

他使用了一些TrustManager类,我也尝试过这样做,但我不知道如何将trustFactory类链接到imap属性。

到目前为止,我的活动中有这个:

Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");

    props.setProperty("mail.store.socketFactory.class",
                "com.imap.DummySSLSocketFactory");


    // Prevents to fall into NOT-secure connection
    props.setProperty("mail.pop3.socketFactory.fallback", "false");

    try {
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect("mail.domain.com", "user", "pass");

        System.out.println(store);

        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        Message messages[] = inbox.getMessages();
        for (Message message : messages) {
            System.out.println(message);
        }
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (MessagingException e) {
        e.printStackTrace();
        System.exit(2);
    }

我知道props.setProperty("mail.store.socketFactory.class", "com.imap.DummySSLSocketFactory");行没有正确实现,因为我不知道怎么能用imaps来使用它..

和DummyTrustManager:

package com.imap;

import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class DummyTrustManager implements X509TrustManager {

  public void checkClientTrusted(X509Certificate[] cert, String authType) throws CertificateException {

      // everything is trusted

  }

  public void checkServerTrusted(X509Certificate[] cert, String authType) throws CertificateException {

      // everything is trusted

  }

  public X509Certificate[] getAcceptedIssuers() {

      return new X509Certificate[0];

  }

} 

和DummySoketSSLFactory:

  package com.imap;

  import java.io.IOException;
  import java.net.InetAddress;
  import java.net.Socket;
  import javax.net.SocketFactory;
  import javax.net.ssl.*;

  public class DummySSLSocketFactory extends SSLSocketFactory {

      private SSLSocketFactory factory;

      public DummySSLSocketFactory() {

          try {

              SSLContext sslcontext = SSLContext.getInstance("TLS");

              sslcontext.init(null,

                                   new TrustManager[] { new DummyTrustManager() },

                                   null);

              factory = (SSLSocketFactory)sslcontext.getSocketFactory();

          } catch(Exception ex) {

          }

      }

      public static SocketFactory getDefault() {

          return new DummySSLSocketFactory();

      }

      public Socket createSocket() throws IOException {

          return factory.createSocket();

      }

      public Socket createSocket(Socket socket, String s, int i, boolean flag)

                                  throws IOException {

          return factory.createSocket(socket, s, i, flag);

      }

      public Socket createSocket(InetAddress inaddr, int i,

                                  InetAddress inaddr1, int j) throws IOException {

          return factory.createSocket(inaddr, i, inaddr1, j);

      }

      public Socket createSocket(InetAddress inaddr, int i)

                                  throws IOException {

          return factory.createSocket(inaddr, i);
}

      public Socket createSocket(String s, int i, InetAddress inaddr, int j)

                                  throws IOException {

          return factory.createSocket(s, i, inaddr, j);

      }

      public Socket createSocket(String s, int i) throws IOException {

          return factory.createSocket(s, i);

      }

      public String[] getDefaultCipherSuites() {

          return factory.getDefaultCipherSuites();

      }

      public String[] getSupportedCipherSuites() {

          return factory.getSupportedCipherSuites();

      }

  }

虚拟类来自http://www.anddev.org/advanced-tutorials-f21/javamail-and-android-little-excursus-t3093.html

请帮助我完成这项工作

1 个答案:

答案 0 :(得分:1)

最简单的解决方案是创建自签名证书。 Example here.