From ff9697fa38c30a01be92220b4ca021b6c0e2dce2 Mon Sep 17 00:00:00 2001
From: liuhaikang <893370936@qq.com>
Date: Tue, 27 Jun 2023 21:12:34 +0800
Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0pop3=E8=B6=85=E6=97=B6?=
 =?UTF-8?q?=E6=9C=BA=E5=88=B6=202.=E6=96=B0=E5=A2=9EsyncExpunge=E6=8E=A5?=
 =?UTF-8?q?=E5=8F=A3,=E4=BF=AE=E6=94=B9expunge=E6=8E=A5=E5=8F=A3=203.?=
 =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96=E8=BD=AC=E5=8F=91=E7=9A=84?=
 =?UTF-8?q?=E9=82=AE=E4=BB=B6=E6=97=B6=E7=9A=84=E9=97=AA=E9=80=80=E9=97=AE?=
 =?UTF-8?q?=E9=A2=98=204.=E4=BF=AE=E6=94=B9Util=E7=9A=84decode64=E6=96=B9?=
 =?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: liuhaikang <893370936@qq.com>
---
 README.md                               |  10 +-
 mail/src/main/ets/Util.ts               |   1 +
 mail/src/main/ets/mail/Folder.ts        |  23 +-
 mail/src/main/ets/mail/Message.ts       |  85 ++++---
 mail/src/main/ets/mail/MimeMultipart.ts |  34 ++-
 mail/src/main/ets/mail/Store.ts         | 302 ++++++++++++++++++------
 6 files changed, 324 insertions(+), 131 deletions(-)

diff --git a/README.md b/README.md
index ae916a9..4166b5e 100644
--- a/README.md
+++ b/README.md
@@ -894,7 +894,7 @@ npm环境配置等更多内容,请参考 [如何安装OpenHarmony npm包](http
 
 * 获取邮件完整内容(promise形式)
 
-  `syncGetContent():Promise<MimeMultipart | Message`
+  `syncGetContent():Promise<MimeMultipart | Message>`
 
 * POP3同步获取邮件Header以及正文的前几行(promise形式)
 
@@ -990,9 +990,13 @@ npm环境配置等更多内容,请参考 [如何安装OpenHarmony npm包](http
 
   `exists(callback)`
 
-* 删除被标记位delete的邮件
+* 删除被标记为delete的邮件(callback形式)
 
-  `expunge(): Message[]`
+  `expunge(callback)`
+
+* 删除被标记为delete的邮件(promise形式)
+
+  `syncExpunge(): Promise<String> `
 
 * 获取所有邮件个数
 
diff --git a/mail/src/main/ets/Util.ts b/mail/src/main/ets/Util.ts
index 098e445..374e11f 100644
--- a/mail/src/main/ets/Util.ts
+++ b/mail/src/main/ets/Util.ts
@@ -132,6 +132,7 @@ export class Util {
 
     //将Base64编码字符串转换成Ansi编码的字符串
     static decode64(input: string) {
+        input = input.replace(/\s/g, '');
         var output = "";
         var chr1, chr2, chr3;
         var enc1, enc2, enc3, enc4;
diff --git a/mail/src/main/ets/mail/Folder.ts b/mail/src/main/ets/mail/Folder.ts
index 38b28ea..8cbed94 100644
--- a/mail/src/main/ets/mail/Folder.ts
+++ b/mail/src/main/ets/mail/Folder.ts
@@ -19,8 +19,6 @@ import { Message } from './Message'
 import { Constant } from '../Constant';
 
 
-
-
 /**
  * 邮箱管理类
  * @param folderName
@@ -209,11 +207,24 @@ export class Folder {
         this.store.exists(this.folderName, callback)
     }
 
-    public expunge(): Message[] {
+    public syncExpunge(): Promise<String> {
+        return new Promise((resolve, reject) => {
+            this.expunge((err) => {
+                if (!err) {
+                    resolve(err)
+                } else {
+                    reject(err)
+                }
+            })
+        })
+    }
+
+    public expunge(callback) {
         if (!!this.store) {
-            this.store.expunge()
+            this.store.expunge((err) => {
+                callback(err)
+            })
         }
-        return this.cacheMessages
     }
 
     public getFolder(folderName: string): Folder {
@@ -379,7 +390,7 @@ export class Folder {
                 if (!!!this.uidTable) {
                     this.uidTable = new Map()
                 }
-                if(!this.uidTable.has(uid)){
+                if (!this.uidTable.has(uid)) {
                     this.uidTable.set(uid, msg)
                 }
                 callback(true, msg)
diff --git a/mail/src/main/ets/mail/Message.ts b/mail/src/main/ets/mail/Message.ts
index cc5b7f1..bf4b1a2 100644
--- a/mail/src/main/ets/mail/Message.ts
+++ b/mail/src/main/ets/mail/Message.ts
@@ -52,6 +52,9 @@ export class Message {
     private htmlCharset = ""
     private htmlEncoding = ""
     private htmlSize = 0
+    private textIndex = "1"
+    private htmlIndex = "2"
+    private nowIndex = "1"
 
     constructor(folder?: Folder, msgnum?: number) {
         this.folder = folder
@@ -632,14 +635,14 @@ export class Message {
         let partCount = 0
 
         let regex = /{(\d+)}\r\n/; // 匹配 {数字} 的正则表达式
-        if(regex.test(bodyStr)){
+        if (regex.test(bodyStr)) {
             let match;
             while (match = regex.exec(bodyStr)) {
                 let strNum = parseInt(match[1]); // 将匹配到的数字字符串转换为整数
                 let index = match.index + match[0].length; // 计算右括号 } 的索引位置
                 let subStr = bodyStr.substr(index, strNum);
                 let result = Util.decodeMimeStr(subStr);
-                bodyStr = bodyStr.substring(0, match.index) + "\"" +  result + "\"" + bodyStr.substring(index + strNum); // 将子字符串替换为新字符串
+                bodyStr = bodyStr.substring(0, match.index) + "\"" + result + "\"" + bodyStr.substring(index + strNum); // 将子字符串替换为新字符串
             }
         }
         let body = bodyStr.replace(/\(/g, '[')
@@ -657,21 +660,10 @@ export class Message {
         }
         partCount = bodyArr.length - 5
         if (bodyArr[0] instanceof Array) {
-            for (let i = 0; i < bodyArr.length; i++) {
-                let bodyFirst = bodyArr[i]
-                if (bodyFirst instanceof Array) {
-                    let bodyFirst1 = bodyFirst[0]
-                    if (bodyFirst1 instanceof Array) {
-                        for (let i = 0; i < bodyFirst.length; i++) {
-                            let bodySecond = bodyFirst[i]
-                            if (!!bodySecond && bodySecond.length > 10) {
-                                this.buildContentBody(bodySecond)
-                            }
-                        }
-                    } else {
-                        this.buildAttachment(bodyFirst)
-                    }
-                }
+            for (let index = 0; index < bodyArr.length; index++) {
+                let bodyIndexPart = bodyArr[index]
+                this.nowIndex = (index + 1) + ""
+                this.buildWholeBody(bodyIndexPart)
             }
         } else {
             this.buildAttachment(bodyArr)
@@ -682,9 +674,11 @@ export class Message {
         mimeMultipart.setTextCharset(this.textCharset)
         mimeMultipart.setTextEncoding(this.textEncoding)
         mimeMultipart.setTextSize(this.textSize)
+        mimeMultipart.setTextIndex(this.textIndex)
         mimeMultipart.setHtmlCharset(this.htmlCharset)
         mimeMultipart.setHtmlEncoding(this.htmlEncoding)
         mimeMultipart.setHtmlSize(this.htmlSize)
+        mimeMultipart.setHtmlIndex(this.htmlIndex)
         mimeMultipart.setAttachmentFiles(this.getFiles())
 
         let bodyStructure = " textCharset " + this.textCharset
@@ -708,19 +702,48 @@ export class Message {
         return mimeMultipart
     }
 
-    private buildContentBody(bodyArr) {
-        if (!!bodyArr && bodyArr.length > 6) {
-            let type = bodyArr[1].toUpperCase()
-            if (type == "PLAIN") {
-                this.textCharset = bodyArr[2][1]
-                this.textEncoding = bodyArr[5]
-                this.textSize = bodyArr[6]
+    private buildWholeBody(bodyIndexPart) {
+        if (bodyIndexPart instanceof Array) {
+            let bodyIndexPartHead = bodyIndexPart[0]
+            if (bodyIndexPartHead instanceof Array) {
+                for (let i = 0; i < bodyIndexPart.length; i++) {
+                    let bodySecond = bodyIndexPart[i]
+                    if (!!bodySecond && bodySecond.length > 10) {
+                        this.buildContentBody(bodySecond, i, false)
+                        let index = this.nowIndex.lastIndexOf(".");
+                        if (index !== -1) {
+                            // 如果字符串中存在点号
+                            this.nowIndex = this.nowIndex.substring(0, index); // 取出点号前面的内容
+                        }
+                    }
+                }
+            } else {
+                this.buildAttachment(bodyIndexPart)
             }
+        }
+    }
 
-            if (type == "HTML") {
-                this.htmlCharset = bodyArr[2][1]
-                this.htmlEncoding = bodyArr[5]
-                this.htmlSize = bodyArr[6]
+    private buildContentBody(bodyArr, index, isFromBuildAttachment:boolean) {
+        if (!!bodyArr && bodyArr.length > 6) {
+            if(!isFromBuildAttachment){
+                index = index + 1;
+                this.nowIndex += "." + index
+            }
+            if (bodyArr[1] instanceof Array) {
+                this.buildWholeBody(bodyArr)
+            } else if(typeof bodyArr[1] === "string"){
+                let type = bodyArr[1].toUpperCase()
+                if (type == "PLAIN") {
+                    this.textCharset = bodyArr[2][1]
+                    this.textEncoding = bodyArr[5]
+                    this.textSize = bodyArr[6]
+                    this.textIndex = this.nowIndex
+                }else if (type == "HTML") {
+                    this.htmlCharset = bodyArr[2][1]
+                    this.htmlEncoding = bodyArr[5]
+                    this.htmlSize = bodyArr[6]
+                    this.htmlIndex = this.nowIndex
+                }
             }
         }
     }
@@ -734,7 +757,7 @@ export class Message {
             isAttachment = true
         } else {
             for (let i = 0; i < bodyArr.length; i++) {
-                if (bodyArr[i] instanceof Array) {
+                if (bodyArr[i] instanceof Array && typeof bodyArr[i][0] === "string") {
                     let attachment = bodyArr[i][0].toUpperCase()
                     if (attachment == "ATTACHMENT" || attachment == "INLINE") {
                         if (!!bodyArr[i][1]) {
@@ -752,14 +775,14 @@ export class Message {
             file.setMimeType(bodyArr[0] + "/" + bodyArr[1])
             file.setSize(bodyArr[6])
             if (bodyArr[2] instanceof Array) {
-                if (bodyArr[2][0].toUpperCase() == "CHARSET") {
+                if (typeof bodyArr[2][0] === "string" && bodyArr[2][0].toUpperCase() == "CHARSET") {
                     file.setCharSet(bodyArr[2][1])
                 }
             }
             file.setCid(bodyArr[3])
             this.addFiles(file)
         } else {
-            this.buildContentBody(bodyArr)
+            this.buildContentBody(bodyArr,this.nowIndex, true)
         }
     }
 }
\ No newline at end of file
diff --git a/mail/src/main/ets/mail/MimeMultipart.ts b/mail/src/main/ets/mail/MimeMultipart.ts
index f5e9b9d..71b693c 100644
--- a/mail/src/main/ets/mail/MimeMultipart.ts
+++ b/mail/src/main/ets/mail/MimeMultipart.ts
@@ -29,6 +29,8 @@ export class MimeMultipart {
     private htmlCharset = ""
     private htmlEncoding = ""
     private htmlSize = 0
+    private textIndex = "1"
+    private htmlIndex = "2"
     private files: Array<AttachmentBody>
 
     constructor(msgNum: number, store: Store) {
@@ -84,6 +86,14 @@ export class MimeMultipart {
         return this.textSize
     }
 
+    public setTextIndex(textIndex){
+        this.textIndex = textIndex
+    }
+
+    public getTextIndex(): string{
+        return this.textIndex
+    }
+
     public setHtmlCharset(htmlCharset) {
         this.htmlCharset = htmlCharset
     }
@@ -100,6 +110,15 @@ export class MimeMultipart {
         return this.htmlSize
     }
 
+
+    public setHtmlIndex(htmlIndex){
+        this.htmlIndex = htmlIndex
+    }
+
+    public gethtmlIndex(): string{
+        return this.htmlIndex
+    }
+
     public syncGetText(): Promise<MimeBodyPart> {
         return new Promise((resolve, reject) => {
             this.getPartText(this.textSize, (success, mimeBody) => {
@@ -128,12 +147,7 @@ export class MimeMultipart {
             return callback(false, "no text")
         }
         let section = ""
-        if (!!this.files) {
-            section = ' (BODY[1.1]<' + '0.' + number + '>)'
-        } else {
-            section = ' (BODY[' + 1 + ']<' + '0.' + number + '>)'
-        }
-
+        section = ' (BODY[' + this.textIndex + ']<' + '0.' + number + '>)'
         this.store.getBodyPart(this.msgNum, section, (success, result) => {
             if (success) {
                 let mimeBody = new MimeBodyPart("", "", "")
@@ -178,13 +192,7 @@ export class MimeMultipart {
             return callback(false, "no html")
         }
         let section = ""
-        if (this.textSize == 0) {
-            section = ' (BODY[' + 1 + ']<' + '0.' + number + '>)'
-        } else if (!!!this.files) {
-            section = ' (BODY[' + 2 + ']<' + '0.' + number + '>)'
-        } else {
-            section = ' (BODY[1.2]<' + '0.' + number + '>)'
-        }
+        section = ' (BODY[' + this.htmlIndex + ']<' + '0.' + number + '>)'
 
         this.store.getBodyPart(this.msgNum, section, (success, result) => {
             if (success) {
diff --git a/mail/src/main/ets/mail/Store.ts b/mail/src/main/ets/mail/Store.ts
index 02ad93c..668671a 100644
--- a/mail/src/main/ets/mail/Store.ts
+++ b/mail/src/main/ets/mail/Store.ts
@@ -23,8 +23,6 @@ import { Flag } from '../mail/Flag';
 import { SocketUtil } from './SocketUtil';
 import net_socket from '@ohos.net.socket';
 import { imapEncode } from '../Utf7Util'
-import util from '@ohos.util';
-
 
 /**
  * 接收邮件、邮箱管理
@@ -45,6 +43,7 @@ export class Store {
     private deleteMailBoxEvent: Function
     private renameMailBoxEvent: Function
     private deleteMsgCountEvent: Function
+    private expungeEvent: Function
     private existsEvent: Function
     private moveEvent: Function
     private copyEvent: Function
@@ -106,77 +105,105 @@ export class Store {
                 connectCallback(success, err)
             }
         }, (value) => {
-            if (this.properties.getProtocol() == "imap") {
-                if (this.timerId != -1) {
-                    MailLogger.info("ohos_mail on message: clear send connect server timeout")
-                    clearTimeout(this.timerId)
-                }
-                this.timerId = setTimeout(() => {
-                    MailLogger.info("ohos_mail timeout currentCmd-----" + this.currentCmd)
-                    switch (this.currentCmd) {
-                        case "A01":
-                            this.connectCallback(false, "connect server response timeout")
-                            break
-                        case "A02":
-                            this.listEvent(false, "get mail box response timeout")
-                            break
-                        case "A03":
-                            this.infoEvent(false, "select folder response timeout")
-                            break
-                        case "A07":
-                            this.uidEvent(false, "get mail uid response timeout")
-                            break
-                        case "A08":
-                            this.createMailBoxEvent(false, "create mailbox response timeout")
-                            break
-                        case "A09":
-                            this.deleteMailBoxEvent(false, "delete mailbox response timeout")
-                            break
-                        case "A10":
-                            this.renameMailBoxEvent(false, "rename mailBox response timeout")
-                            break
-                        case "A11":
-                            this.deleteMsgCountEvent(false, "get deleted message count response timeout")
-                            break
-                        case "A12":
-                            this.existsEvent(false, "exists response timeout")
-                            break
-                        case "A13":
-                            this.moveEvent(false, "move response timeout")
-                            break
-                        case "A14":
-                            this.headerEvent(false, "getHeader response timeout")
-                            break
-                        case "A15":
-                            this.allHeaderEvent(false, "geAllHeader response timeout")
-                            break
-                        case "A16":
-                            this.flagsEvent(false, "getFlags response timeout")
-                            break
-                        case "A17":
-                            this.setFlagEvent(false, "setFlags response timeout")
-                            break
-                        case "A18":
-                            this.sizeEvent(false, "getSize response timeout")
-                            break
-                        case "A19":
-                            this.contentEvent(false, "getContent response timeout")
-                            break
-                        case "A21":
-                            this.connectCallback(false, "id response timeout")
-                            break
-                        case "A22":
-                            this.contentEvent(false, "getBodyPart response timeout")
-                            break
-                        case "A23":
-                            this.copyEvent(false, "copy response timeout")
-                            break
-                        case "A24":
-                            this.msgNumEvent(false, "get msgNum response timeout")
-                            break
-                    }
-                }, this.timeOutMillisecond)
+            if (this.timerId != -1) {
+                MailLogger.info("ohos_mail on message: clear request timeout")
+                clearTimeout(this.timerId)
             }
+            this.timerId = setTimeout(() => {
+                MailLogger.info("ohos_mail timeout currentCmd-----" + this.currentCmd)
+                switch (this.currentCmd) {
+                    case "A01":
+                        this.connectCallback(false, "connect server response timeout")
+                        break
+                    case "A02":
+                        this.listEvent(false, "get mail box response timeout")
+                        break
+                    case "A03":
+                        this.infoEvent(false, "select folder response timeout")
+                    case "A04":
+                        this.expungeEvent(false, "expunge timeout")
+                        break
+                    case "A07":
+                        this.uidEvent(false, "get mail uid response timeout")
+                        break
+                    case "A08":
+                        this.createMailBoxEvent(false, "create mailbox response timeout")
+                        break
+                    case "A09":
+                        this.deleteMailBoxEvent(false, "delete mailbox response timeout")
+                        break
+                    case "A10":
+                        this.renameMailBoxEvent(false, "rename mailBox response timeout")
+                        break
+                    case "A11":
+                        this.deleteMsgCountEvent(false, "get deleted message count response timeout")
+                        break
+                    case "A12":
+                        this.existsEvent(false, "exists response timeout")
+                        break
+                    case "A13":
+                        this.moveEvent(false, "move response timeout")
+                        break
+                    case "A14":
+                        this.headerEvent(false, "getHeader response timeout")
+                        break
+                    case "A15":
+                        this.allHeaderEvent(false, "geAllHeader response timeout")
+                        break
+                    case "A16":
+                        this.flagsEvent(false, "getFlags response timeout")
+                        break
+                    case "A17":
+                        this.setFlagEvent(false, "setFlags response timeout")
+                        break
+                    case "A18":
+                        this.sizeEvent(false, "getSize response timeout")
+                        break
+                    case "A19":
+                        this.contentEvent(false, "getContent response timeout")
+                        break
+                    case "A21":
+                        this.connectCallback(false, "id response timeout")
+                        break
+                    case "A22":
+                        this.contentEvent(false, "getBodyPart response timeout")
+                        break
+                    case "A23":
+                        this.copyEvent(false, "copy response timeout")
+                        break
+                    case "A24":
+                        this.msgNumEvent(false, "get msgNum response timeout")
+                        break
+                    case "USER":
+                    case "PASS":
+                        this.pop3Event(false, "pop3 auth response timeout")
+                        break
+                    case "STAT":
+                        this.infoEvent(false, "pop3 STAT response timeout")
+                        break
+                    case "UIDL":
+                        this.uidEvent(false, "pop3 uidl response timeout")
+                        break
+                    case "TOP":
+                        this.allHeaderEvent(false, "pop3 top response timeout")
+                        break
+                    case "DELE":
+                        this.setFlagEvent(false, "pop3 DELE response timeout")
+                        break
+                    case "LIST":
+                        this.sizeEvent(false, "pop3 LIST response timeout")
+                        break
+                    case "TOPLINE":
+                        this.topLinesContent(false, "pop3 TOPLINE response timeout")
+                        break
+                    case "RETR":
+                        this.contentEvent(false, "pop3 RETR response timeout")
+                        break
+                    case "QUIT":
+                        this.quitEvent(false, "pop3 QUIT response timeout")
+                        break
+                }
+            }, this.timeOutMillisecond)
 
             //服务端响应监听
             let decodeNum = new Uint8Array(value.message)
@@ -221,6 +248,10 @@ export class Store {
 
                 if (this.currentCmd == "RETR") {
                     if (!this.isStartWrite) {
+                        if (this.timerId != -1) {
+                            clearTimeout(this.timerId)
+                            MailLogger.info("ohos_mail RETR timeout clear")
+                        }
                         this.contentEvent(true, this.responseDecode)
                         this.responseDecode = ""
                     }
@@ -229,6 +260,10 @@ export class Store {
 
                 if (this.currentCmd == "TOP") {
                     if (asciiToStr.includes(".\r\n")) {
+                        if (this.timerId != -1) {
+                            clearTimeout(this.timerId)
+                            MailLogger.info("ohos_mail TOP timeout clear")
+                        }
                         this.isStartWrite = false
                         this.allHeaderEvent(true, this.responseDecode)
                         this.responseDecode = ""
@@ -238,6 +273,10 @@ export class Store {
 
                 if (this.currentCmd == "TOPLINE") {
                     if (asciiToStr.includes(".\r\n")) {
+                        if (this.timerId != -1) {
+                            clearTimeout(this.timerId)
+                            MailLogger.info("ohos_mail TOPLINE timeout clear")
+                        }
                         this.isStartWrite = false
                         this.topLinesContent(true, this.responseDecode)
                         this.responseDecode = ""
@@ -246,6 +285,11 @@ export class Store {
                 }
 
                 if (asciiToStr.startsWith("+OK")) {
+                    if (this.timerId != -1) {
+                        clearTimeout(this.timerId)
+                        MailLogger.info("ohos_mail pop3 timeout clear, currentCmd-----" + this.currentCmd)
+                    }
+
                     if (this.currentCmd == "PASS") {
                         connectCallback(true, asciiToStr)
                         return
@@ -291,6 +335,11 @@ export class Store {
                 }
 
                 if (asciiToStr.startsWith("-ERR")) {
+                    if (this.timerId != -1) {
+                        clearTimeout(this.timerId)
+                        MailLogger.info("ohos_mail pop3 timeout clear, currentCmd-----" + this.currentCmd)
+                    }
+
                     if (this.currentCmd == "USER" || this.currentCmd == "PASS") {
                         connectCallback(false, asciiToStr)
                         this.pop3Event(asciiToStr)
@@ -334,7 +383,7 @@ export class Store {
             if (lastLine.startsWith("A01")) {
                 if (this.timerId != -1) {
                     clearTimeout(this.timerId)
-                    MailLogger.info("ohos_mail on message: connect server clear")
+                    MailLogger.info("ohos_mail connect imap server clear")
                 }
                 if (lastLine.startsWith("A01 OK")) {
                     if (this.idMap) {
@@ -383,6 +432,20 @@ export class Store {
                 return
             }
 
+            //获取邮箱信息
+            if (lastLine.startsWith("A04")) {
+                if (this.timerId != -1) {
+                    clearTimeout(this.timerId)
+                    MailLogger.info("ohos_mail expunge timeout clear")
+                }
+                if (lastLine.startsWith("A04 OK")) {
+                    this.expungeEvent(null)
+                } else {
+                    this.expungeEvent(asciiToStr)
+                }
+                return
+            }
+
             //获取邮件UID
             if (lastLine.startsWith("A07")) {
                 if (this.timerId != -1) {
@@ -736,7 +799,14 @@ export class Store {
             { data: "USER " + this.properties.getFrom() + Constant.LINEFEED,
                 encoding: "UTF-8" }
 
+        this.currentCmd = "USER"
         this.socket.send(cmd).then(() => {
+            if (this.timerId != -1) {
+                clearTimeout(this.timerId)
+            }
+            this.timerId = setTimeout(() => {
+                this.pop3Event(false, "send USER timeout")
+            }, this.timeOutMillisecond)
             MailLogger.info('ohos_mail-- send user success');
         }).catch(err => {
             MailLogger.info('ohos_mail-- send suer fail --' + JSON.stringify(err));
@@ -753,20 +823,35 @@ export class Store {
             { data: "PASS " + this.properties.getAuthorizationCode() + Constant.LINEFEED,
                 encoding: "UTF-8" }
 
+        this.currentCmd = "PASS"
         this.socket.send(cmd).then(() => {
+            if (this.timerId != -1) {
+                clearTimeout(this.timerId)
+            }
+            this.timerId = setTimeout(() => {
+                this.pop3Event(false, "send PASS timeout")
+            }, this.timeOutMillisecond)
             MailLogger.info('ohos_mail-- send pass success');
         }).catch(err => {
             MailLogger.info('ohos_mail-- send pass fail --' + JSON.stringify(err));
         })
     }
 
-    private sendStat() {
+    private sendStat(infoEvent) {
         this.currentCmd = "STAT"
         let cmd = this.isSSL ? "STAT" + Constant.LINEFEED :
             { data: "STAT" + Constant.LINEFEED,
                 encoding: "UTF-8" }
 
+        this.infoEvent = infoEvent
+
         this.socket.send(cmd).then(() => {
+            if (this.timerId != -1) {
+                clearTimeout(this.timerId)
+            }
+            this.timerId = setTimeout(() => {
+                this.infoEvent(false, "send STAT timeout")
+            }, this.timeOutMillisecond)
             MailLogger.info('ohos_mail-- send stat success');
         }).catch(err => {
             MailLogger.info('ohos_mail-- send stat fail--' + JSON.stringify(err));
@@ -904,7 +989,7 @@ export class Store {
             if (name != "INBOX") {
                 return new Error("folder is not INBOX")
             } else {
-                this.sendStat()
+                this.sendStat(event)
             }
             return
         }
@@ -944,6 +1029,12 @@ export class Store {
                     encoding: "UTF-8" }
 
             this.socket.send(cmd).then(() => {
+                if (this.timerId != -1) {
+                    clearTimeout(this.timerId)
+                }
+                this.timerId = setTimeout(() => {
+                    this.uidEvent(false, "send UIDL timeout")
+                }, this.timeOutMillisecond)
                 MailLogger.info('ohos_mail send get mail uid success ');
             }).catch(err => {
                 MailLogger.error('ohos_mail send get mail uid fail :' + JSON.stringify(err));
@@ -972,6 +1063,10 @@ export class Store {
     }
 
     public getMsgNumByUid(uid, callback) {
+        if (this.properties.getProtocol() == "pop3") {
+            return callback(false, "pop3 not support")
+        }
+
         this.currentCmd = "A24"
         this.msgNumEvent = callback
         let cmd = this.isSSL ? "A24 UID Fetch " + uid + " (UID)" + Constant.LINEFEED :
@@ -1256,6 +1351,12 @@ export class Store {
                     encoding: "UTF-8" }
 
             this.socket.send(cmd).then(() => {
+                if (this.timerId != -1) {
+                    clearTimeout(this.timerId)
+                }
+                this.timerId = setTimeout(() => {
+                    this.allHeaderEvent(false, "send TOP timeout")
+                }, this.timeOutMillisecond)
                 this.isStartWrite = true
                 MailLogger.info('ohos_mail send TOP success');
             }).catch(err => {
@@ -1324,6 +1425,12 @@ export class Store {
                         encoding: "UTF-8" }
 
                 this.socket.send(cmd).then(() => {
+                    if (this.timerId != -1) {
+                        clearTimeout(this.timerId)
+                    }
+                    this.timerId = setTimeout(() => {
+                        this.setFlagEvent(false, "send DELE timeout")
+                    }, this.timeOutMillisecond)
                     MailLogger.info('ohos_mail send setFlag success');
                 }).catch(err => {
                     MailLogger.error('ohos_mail send setFlag fail :' + JSON.stringify(err));
@@ -1376,6 +1483,12 @@ export class Store {
                     encoding: "UTF-8" }
 
             this.socket.send(cmd).then(() => {
+                if (this.timerId != -1) {
+                    clearTimeout(this.timerId)
+                }
+                this.timerId = setTimeout(() => {
+                    this.sizeEvent(false, "send LIST timeout")
+                }, this.timeOutMillisecond)
                 MailLogger.info('ohos_mail send getSize success');
             }).catch(err => {
                 MailLogger.error('ohos_mail send getSize fail :' + JSON.stringify(err));
@@ -1403,6 +1516,10 @@ export class Store {
     }
 
     public getBodyPart(msgNum: number, section: string, callback) {
+        if (this.properties.getProtocol() == "pop3") {
+            return callback(false, "pop3 not support")
+        }
+
         this.currentCmd = "A22"
         this.contentEvent = callback
         let cmd = this.isSSL ? 'A22 Fetch ' + msgNum + section + Constant.LINEFEED :
@@ -1424,6 +1541,10 @@ export class Store {
     }
 
     public getLineText(msgNum, linesCount, callback) {
+        if (this.properties.getProtocol() == "imap") {
+            return callback(false, "imap not support")
+        }
+
         this.topLinesContent = callback
         this.currentCmd = "TOPLINE"
 
@@ -1433,6 +1554,12 @@ export class Store {
 
 
         this.socket.send(cmd).then(() => {
+            if (this.timerId != -1) {
+                clearTimeout(this.timerId)
+            }
+            this.timerId = setTimeout(() => {
+                this.topLinesContent(false, "send TOPLINE timeout")
+            }, this.timeOutMillisecond)
             this.isStartWrite = true
             MailLogger.info('ohos_mail send TOPLINE success');
         }).catch(err => {
@@ -1452,6 +1579,12 @@ export class Store {
                     encoding: "UTF-8" }
 
             this.socket.send(cmd).then(() => {
+                if (this.timerId != -1) {
+                    clearTimeout(this.timerId)
+                }
+                this.timerId = setTimeout(() => {
+                    this.contentEvent(false, "send RETR timeout")
+                }, this.timeOutMillisecond)
                 this.isStartWrite = true
                 MailLogger.info('ohos_mail send RETR success');
             }).catch(err => {
@@ -1481,7 +1614,8 @@ export class Store {
     }
 
     //删除所有 标记位delete的邮件
-    public expunge() {
+    public expunge(callback: (err) => void) {
+        this.expungeEvent = callback
         if (this.properties.getProtocol() == "pop3") {
             this.quit()
             return
@@ -1492,9 +1626,15 @@ export class Store {
                 encoding: "UTF-8" }
 
         this.socket.send(cmd).then(() => {
-            MailLogger.info('ohos_mail send delete mail success');
+            if (this.timerId != -1) {
+                clearTimeout(this.timerId)
+            }
+            this.timerId = setTimeout(() => {
+                this.expungeEvent("send expunge timeout")
+            }, this.timeOutMillisecond)
+            MailLogger.info('ohos_mail send expunge success');
         }).catch(err => {
-            MailLogger.error('ohos_mail send delete mail fail :' + JSON.stringify(err));
+            MailLogger.error('ohos_mail send expunge fail :' + JSON.stringify(err));
         })
     }
 
@@ -1555,6 +1695,12 @@ export class Store {
                 encoding: "UTF-8" }
 
         this.socket.send(cmd).then(() => {
+            if (this.timerId != -1) {
+                clearTimeout(this.timerId)
+            }
+            this.timerId = setTimeout(() => {
+                this.quitEvent(false, "send QUIT timeout")
+            }, this.timeOutMillisecond)
             MailLogger.info('ohos_mail send quit success');
         }).catch(err => {
             MailLogger.error('ohos_mail send quit fail :' + JSON.stringify(err));
-- 
Gitee