邮件接收POP3协议简单介绍

常用的电子邮件协议有SMTP、POP3、IMAP这三种
其中电子邮件的发送协议是SMTP, 邮件的接收协议主要有2种 IMAP和POP3,本文主要介绍POP3协议
POP3(Post Office Protocol 3)邮局协议的第3个版本,是因特网电子邮件的第一个离线协议标准。
POP3 协议的命令
比如我们想要去连接163邮箱服务器
在电脑终端cmd 输入 telnet pop.163.com 110(pop3的端口号)
连接上之后输入
user (邮箱名)
pass (密码)
登陆上之后可以查看邮件总数
STAT (查看邮件总数)
list (msg)(默认列出所有邮件信息)
uidl 这是邮件的唯一标识符
retr (msg)列出第msg个邮件的都有内容
dele (msg)用于在邮件msg上设置删除标志
top (msg) (n) 返回由邮件msg前n行内容,n必须是正整数
上面是POP3的一些获取服务器信息的命令

目前在看一个问题就是在Android中使用POP3同步服务器上的邮件的问题
看通过看Android Email的源码
在Pop3Service.java类中,在这里截取一部分代码进行解释

 public static int synchronizeMailboxSynchronous(Context context, final Account account,
            final Mailbox folder, final int deltaMessageCount) throws MessagingException {
        TrafficStats.setThreadStatsTag(TrafficFlags.getSyncFlags(context, account));
        try {
        //这里是开始同步邮件
            synchronizePop3Mailbox(context, account, folder, deltaMessageCount);

            if (nc != null) {
                nc.cancelLoginFailedNotification(account.mId);
            }
        } catch (MessagingException e) {
        return EmailServiceStatus.SUCCESS;
    }


上面讲的是从synchronizePop3Mailbox()方法开始同步邮件,看一下synchronizePop3Mailbox里面做了写什么

//显示查询本地客户端上的邮件,把本地邮件放在localMessageMap 中,其中serverId最为key。

 Cursor localUidCursor = null;
        HashMap<String, LocalMessageInfo> localMessageMap = new HashMap<String, LocalMessageInfo>();

        try {
            localUidCursor = resolver.query(
                    EmailContent.Message.CONTENT_URI,
                    LocalMessageInfo.PROJECTION,
                    MessageColumns.MAILBOX_KEY + "=?",
                    new String[] {
                            String.valueOf(mailbox.mId)
                    },
                    null);
            while (localUidCursor.moveToNext()) {
                LocalMessageInfo info = new LocalMessageInfo(localUidCursor);
                localMessageMap.put(info.mServerId, info);
            }
 //获取服务器上面的邮件数量
 final int remoteMessageCount = remoteFolder.getMessageCount();
 //如果大于0的话就获取remoteMessages 信息
 if (remoteMessageCount > 0) {
            remoteMessages = remoteFolder.getMessages(remoteMessageCount, remoteMessageCount);
 //这里的将服务器上面的message的uid和Message放在remoteUidMap里面
 for (final Pop3Message message : remoteMessages) {
                final String uid = message.getUid();
                remoteUidMap.put(uid, message);
            }
 //现在就是开始要去匹配哪些邮件是要从服务器上面同步下来的,在刷新是肯定是要将客户端没有但是在服务器上面的最新邮件同步下来的
  for (final Pop3Message message : remoteMessages) {
                final String uid = message.getUid();
                //在这里通过唯一标识符uid去localMessageMap里面去匹配
                final LocalMessageInfo localMessage = localMessageMap.get(uid);
                //如果localMessage为null则去同步
                if (localMessage == null) {
                    count++;
                } else {
                //否则认为本地已经存在了,不用再去服务器上同步
                    LogUtils.d(Logging.LOG_TAG, "found a local message, need " +
                            deltaMessageCount + " more remote messages");
                    countNeeded = deltaMessageCount;
                    count = 0;
               }
               //
                if (localMessage == null ||
                        (localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE &&
                                localMessage.mFlagLoaded != Message.FLAG_LOADED_PARTIAL)) {
                    LogUtils.d(Logging.LOG_TAG, "need to sync " + uid);

                    unsyncedMessages.add(message);

“`

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值