簡體   English   中英

MimeMessageParser無法從地址獲取

[英]MimeMessageParser unable to fetch from address

我們已經在這個問題上停留了很長時間了。在我們的項目中,我們試圖解析寫入文件的電子郵件並將數據放入pojo。 它適用於大多數情況,但是當電子郵件ID太長時,電子郵件ID會轉到下一行,由於未提取發件人地址,而是提取了名稱。我們使用commons-email-1.4

包含emailmessage的輸入文件具有

情況1:

From: "def, abc [CCC-OT]" <abc.def@test.com> //here it fetches the mail id properly

如果郵件ID較長,則該文件具有

情況2:

From: "defxacdhf, abc [CCC-OT]" 
<abc.defxacdhf@test.com>// here the mail id jumps to the next line so the from address fetched contains the name

這是示例代碼

ByteArrayInputStream byteArrayStream = new ByteArrayInputStream(FileUtils.getStreamAsByteArray(buffInStream,
                lengthOfFile));
        // MimeMessage message = new MimeMessage(mailSession, byteArrayStream);
        MimeMessageParser mimeParser = new MimeMessageParser(MimeMessageUtils.createMimeMessage(mailSession,
                byteArrayStream));
        MimeMessageParser parsedMessage = mimeParser.parse();

當我們嘗試獲取發件人地址時

emailData.setFromAddress(parsedMessage.getFrom());

abc.def@test.com ,它返回abc.def@test.com ,在case2中,它返回"defxacdhf, abc [CCC-OT]" 感謝您的幫助。

編輯腳本文件的讀寫方式如下。

while read line
        do
            echo "$line" >> /directory/$FILE_NAME
        done

如前所述:

這在使用的任何庫中都不是錯誤,而是輸入不符合RFC的錯誤。

引用RFC-822

3.1.1。 長頭字段

  Each header field can be viewed as a single, logical line of ASCII characters, comprising a field-name and a field-body. For convenience, the field-body portion of this conceptual entity can be split into a multiple-line representation; this is called "folding". The general rule is that wherever there may be linear-white-space (NOT simply LWSP-chars), a CRLF immediately followed by AT LEAST one LWSP-char may instead be inserted. 

我不明白為什么您使用shell while循環讀取數據而不是僅僅使用cat或類似的東西,但是問題在於您使用的是“ read”。 默認情況下,read將輸入行拆分為多個字段,並由shell IFS環境變量指定的字段分隔符分隔。 前導字段分隔符將被忽略,因此當您讀取以空白開頭的行時,空白將被忽略。

將循環更改為:

    while IFS= read -r line
    do
        echo "$line" >> /directory/$FILE_NAME
    done

這會將IFS設置為每次讀取前的空字符串,並指定“原始”讀取,以使反斜杠字符不特殊。

但是,除非您在該讀取循環中執行其他操作,否則要做的只是簡單得多

    cat > /directory/$FILE_NAME

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM