在Email的登录界面,需要配置各种的邮箱。其通信是使用的socket方式进行,具体代码可以参考MailTransport的open方法。
转载请注明http://ishelf.iteye.com/blog/1022652
/** * Attempts to open a connection using the Uri supplied for connection parameters. Will attempt * an SSL connection if indicated. */ public void open() throws MessagingException, CertificateValidationException { if (Config.LOGD && Email.DEBUG) { Log.d(Email.LOG_TAG, "*** " + mDebugLabel + " open " + getHost() + ":" + String.valueOf(getPort())); } try { SocketAddress socketAddress = new InetSocketAddress(getHost(), getPort()); if (canTrySslSecurity()) { mSocket = SSLUtils.getSSLSocketFactory(canTrustAllCertificates()).createSocket(); } else { mSocket = new Socket(); } mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); mIn = new BufferedInputStream(mSocket.getInputStream(), 1024); mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512); } catch (SSLException e) { if (Config.LOGD && Email.DEBUG) { Log.d(Email.LOG_TAG, e.toString()); } throw new CertificateValidationException(e.getMessage(), e); } catch (IOException ioe) { if (Config.LOGD && Email.DEBUG) { Log.d(Email.LOG_TAG, ioe.toString()); } throw new MessagingException(MessagingException.IOERROR, ioe.toString()); } }
这是一个很典型的java语言的socket通信,不过android的Email通信和gmail通信还有尚邮(第三方应用)有一些区别,表现主要在两个地方:1.数据加载方式,email的邮件提取是通过两次通信实现的,而gmail和尚邮的方式应该是类似的,是通过一次通信实现的,两次加载的优缺点主要就是表现在大附件显示(音乐,视频等)这里,大家可以用尚邮试试。
下面给出一个socket通信的test类
import android.app.Activity; import android.os.Bundle; import android.util.Log; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; public class SocketTest extends Activity { /** Called when the activity is first created. */ public static String LOG_TAG = "SocketTest"; private Socket mSocket; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SocketAddress socketAddress = new InetSocketAddress("imap.gmail.com", 995); Log.d(LOG_TAG, "socketAddress :" + socketAddress.toString()); mSocket = new Socket(); Log.d(LOG_TAG, "" + mSocket.toString()); Log.d(LOG_TAG, "mSocket conneted"); try { mSocket.connect(socketAddress, 10000); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.d(LOG_TAG,e.getMessage()); } // After the socket connects to an SSL server, confirm that the hostname // is as expected Log.d(LOG_TAG, "mSocket conneted ..........."); } }
下次分析一下Email的附件提取方式。android2.2和1.6两个版本的最大区别在于附件的提取,不过同名附件无法同时收取的问题还是没有解决。