2016-02-25 58 views
0

我有一個存儲原始電子郵件內容的數據庫。我的要求是從數據庫獲取單個郵件並處理該數據以獲取該特定電子郵件的基本信息(例如FROM,TO,SUBJECT等),並使用Core將所有附件保存到文件系統中Java的。目前我能夠以字符串的形式從數據庫獲取原始電子郵件數據,但無法處理該數據。使用Java處理原始電子郵件數據

如何使用Java處理此原始電子郵件數據(字符串數據類型)?

編輯: 在數據庫級別,數據存儲爲NCLOB。從數據庫中獲取數據後,將其存儲爲Java String數據類型。

樣本電子郵件數據是:

Return-Path: <[email protected]> 
Delivered-To: [email protected] 
Received: from naplmailer2.com (unknown [172.25.3.5]) 
    by mail3.mydomain.com (Postfix) with ESMTP id 46E6572049B 
    for <[email protected]>; Tue, 23 Feb 2016 15:16:43 +0530 (IST) 
DKIM-Signature: v=1; a=rsa-sha256; d=mydomain; s=sms2; c=relaxed/simple; 
    q=dns/txt; [email protected]; t=1456220806; x=1458812806; 
    h=From:Sender:Reply-To:Subject:Date:Message-ID:To:Cc:MIME-Version:Content-Type: 
    Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: 
    Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: 
    List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; 
    bh=K7Tc1XHEFN5ey8WU6/HXHF9XYDMLCiIsVdU7DloptqI=; 
    b=CEnhtyGSQi+08wghYzKjW61JpO/IqOCgjopdCaesEfRgdeu86BWTQ9ZV0G7mCkDz 
    XChXBhzNsj+uST6yiu7ivYsCBqKvBAnyaoUvLSUw5rWAuCNlg1gdP1ilEzFnZZBB 
    6U25CK64N81I5cKCdltgmUe5B97XueIV8M8LjhyemxM=; 
X-AuditID: 7370fb5c-f79a16d000001484-b0-56cc2a86383c 
Received: from CHNMURROOTCAS2.murugappa.com ([172.25.1.14]) 
    by naplmailer2.com (Symantec Messaging Gateway) with SMTP id 8B.42.05252.68A2CC65; Tue, 23 Feb 2016 15:16:46 +0530 (IST) 
Received: from CHNMURROOTMBX2.murugappa.com ([fe80::a141:6b81:60c9:125c]) by 
CHNMURROOTCAS2.murugappa.com ([fe80::fc6b:b33c:6d4f:fadd%12]) with mapi id 
14.03.0210.002; Tue, 23 Feb 2016 15:16:40 +0530 
From: Support-BPM-CholaMS <[email protected]> 
To: "[email protected]" <[email protected]> 
Subject: Test From Mail 
Thread-Topic: Test From Mail 
Thread-Index: AdFuHx8uv6VR8hDtQvKILSCahVrrMg== 
Date: Tue, 23 Feb 2016 09:46:39 +0000 
Message-ID: <[email protected]a.com> 
Accept-Language: en-US 
Content-Language: en-US 
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-originating-ip: [10.111.10.60] 
Content-Type: multipart/alternative; 
    boundary="_000_B8C5C607CDD50E4D84DACA129D4CFD64C7299C49CHNMURROOTMBX2m_" 
MIME-Version: 1.0 
X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprMKsWRmVeSWpSXmKPExsWyRpKRT7dN60yYwe2HihYvDps7MHqs73jD 
    GsAY1cBok5iXl1+SWJKqkJJanGyr5JJZnJyTmJmbWqSQll+k4JyRn5Oo4BuspJCZYqtkqqRQ 
    kJOYnJqbmldiq5RYUJCal6Jkx6WAAWyAyjLzFFLzkvNTMvPSbZU8g/11LSxMLXUNlexcPIOd 
    fRw9fV2DFPz8E7ayZjx+spe54LdqxeLPS9kbGBcodzFyckgImEicOvSNFcIWk7hwbz1bFyMX 
    h5DAdkaJdcd3QjmnGSU+z17PCFLFJmArseJgM5gtIuAocezPNxYQW1hAXGLdxFesEHEZieWH 
    l0DZehLnzl5lA7FZBFQljhzoZQaxeQWCJW7seAZWwwi0+fupNUwgNjPQnFtP5jNBXCQgsWTP 
    eWYIW1Ti5eN/UJcqSLR+PwUU5wCqz5fY8cEYYqSgxMmZT1gmMArNQjJpFkLVLCRVECU6Egt2 
    f2KDsLUlli18zQxjnznwmAlZfAEj+ypG/rzEgpzcxMyc1CIjveT83E2MwJgvLvgds4Px00+n 
    Q4wCHIxKPLzLG06HCbEmlhVX5h5ilOBgVhLhdeA7EybEm5JYWZValB9fVJqTWnyI0QcYIhOZ 
    pUST84HpKK8k3tDI3MzQzMTY0NDc2BKHsJI4b6v84TAhgXRgaspOTS1ILYIZx8TBKdXAWDgr 
    40nv+6kRyxcq/0qx//f+zokw3qrXR/M3XLflqeaaHnpi6YXDN39mzZhiMLv6DceSuWerT1xS 
    SrXbcnaX/LOcj/pu9XFreqSf3lJ9lfYpY/3x2BW/+wofCb7749Fzfv3j/emHsy6/eO+X4LGs 
    /4fGYpbrB0733TjNmyKzQWnjBP93PfbzFnEqsRRnJBpqMRcVJwIArc+Y8CYDAAA= 

--_000_B8C5C607CDD50E4D84DACA129D4CFD64C7299C49CHNMURROOTMBX2m_ 
Content-Type: text/plain; charset="us-ascii" 
content-transfer-encoding: quoted-printable 

Testing for from mail fetch 

--_000_B8C5C607CDD50E4D84DACA129D4CFD64C7299C49CHNMURROOTMBX2m_ 
Content-Type: text/html; charset="us-ascii" 
content-transfer-encoding: quoted-printable 
--_000_B8C5C607CDD50E4D84DACA129D4CFD64C7299C49CHNMURROOTMBX2m_-- 
+0

什麼樣的原始數據的烏爾談論..字符串不是原生..是一種叫做它進來字節的形式。所以請具體說明你問什麼 –

+0

在DB級數據被存儲爲NCLOB。從數據庫中獲取數據後,將其存儲爲Java String數據類型。 – user5979396

+0

您可以發佈您正在提取的那個String的內容示例嗎? – Nadir

回答

0

那麼,根據你的回答做一些研究&評論,我得到了我所需要的。謝謝大家的努力。

只是在這裏共享相同。下面的Java方法將從數據庫獲取電子郵件原始數據,查找並保存電子郵件數據中包含的所有附件到文件系統,最後返回成功或失敗消息。

public static String saveAttachments(String EMAIL_ID) 
{ 
    try 
    { 
     String saveDirectory = "C:\\Email\\Attachements\\"; 

     //Get email record from DB 
     EMAIL newEMAILObj = EMAIL.getEmailDetailsForEmailId(EMAIL_ID); 

     //Get email raw data into a String variable 
     String emailRawData = newEMAILObj.getCONTENT(); 

     Session newSession = Session.getDefaultInstance(new Properties()); 
     InputStream inputStreamObj = new ByteArrayInputStream(emailRawData.getBytes()); 
     MimeMessage mimeMessageObj = new MimeMessage(newSession, inputStreamObj); 
     String contentType = mimeMessageObj.getContentType(); 

     if (contentType.contains("multipart")) //Content may contain attachments 
     { 
      Multipart multiPart = (Multipart) mimeMessageObj.getContent(); 
      int numberOfParts = multiPart.getCount(); 
      for (int partCount = 0; partCount < numberOfParts; partCount++) 
      { 
       MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount); 
       if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) //This part is an attachment 
       { 
        File file = new File(saveDirectory+part.getFileName()); 
        part.saveFile(file); 
       } 
      } 
     } 
    } 
    catch (MessagingException ex) 
    { 
     return "FAILED: "+ex.getLocalizedMessage(); 
    } 
    catch (IOException ex) 
    { 
     return "FAILED: "+ex.getLocalizedMessage(); 
    } 
    return "SUCCESS"; 
} 
0

假設你是取字符串中包含新行分隔符

String rawEmail = "YOUR EMAIL CONTENTS"; 
String [] lines = rawEmail.split("\\r?\\n"); 
Map<String, String> attributes = new HashMap<>(); 
for(String line : lines) 
{ 
    String [] tokens = line.split(":"); 
    if(!tokens[0].isEmpty()) 
    { 
     attributes.put(tokens[0].trim(), tokens[1].isEmpty()? null : tokens[1].trim()); 
    } 
} 

嵌套屬性的進一步處理將做同樣的方式

+0

而不是使用這個地圖,HashMap等..有什麼辦法可以使用JavaMail方法(這裏給出:http:///docs.oracle.com/javaee/1.4/api/javax/mail/Message.html)的字符串數據? – user5979396

+0

您將不得不使用MimeMessage類,或者實現自己的擴展Message類。有一個名爲setContent(Object content,String type)的方法,我猜是你會得到什麼。另一種選擇是在令牌[0](屬性名稱)之間切換,並使用類中的相應方法來設置值。 – Nadir

0

那麼如果你想解析一封電子郵件,你只需要知道電子郵件的格式。這曾經在RFC822中定義,由RFC2822廢棄,由RFC5322廢棄。你應該先閱讀這些文件,然後選擇你想要處理的部分。

在最高級別,由行組成的消息。這些行應該用\r\n(CrLf)來終止,但是你不應該依賴它,因爲你從DB獲取消息而不知道是否發生了預處理。首先出現一個標題(包含標題行),另一個選項是一個由空行與標題分開的主體。

標題行或形式爲HEADER_NAME:HEADER_VALUE的標題名稱不得以空格開頭。在標題部分,任何以空格開頭的行都是一個連續行,並且必須連接到前一行的值。

有關更多詳細信息,請參閱RFC 5322