1. 程式人生 > >詳解Address和RFC 822

詳解Address和RFC 822

                               詳解Address和RFC 822

 

  1. 名稱解釋

  RFC 822

電子郵件的標準格式 (RFC 822)    除了由一個Internet使用者傳遞給另一個使用者的資訊之外,電子郵件中還必須包含附加的服務資訊。SMTP伺服器利用這些資訊來傳遞郵件,而客戶端的郵件接收軟體則利用這些資訊來對郵件進行分類。

每封郵件都有兩個部分:信頭和主體。

信頭部分的欄位可分為兩類。一類是由你的電子郵件程式產生的,另一類是郵件通過SMTP伺服器時被加上的。在所有被SMTP伺服器加上的欄位中,對我們而言最重要的是Message-Id欄位。這個欄位是由你傳向的SMTP伺服器加上的。這是一個唯一的ID號。你可用這個號碼作為郵件的編號。   

下表列出了可由使用者的郵件程式控制的信頭欄位。這並不意味著所有的欄位都是必須的。實際上可以忽略形成信頭這一步驟而只發送正文。讓你的SMTP伺服器為你加上最起碼的必需欄位。   

信頭欄位 目的   
From 郵件作者   
Sender 發信人   
Reply-To 回郵地址   
To 收信人地址    
CC 抄送:另一個收信人地址    
BCC 密送:收信人地址,但其它收信人看不到這個收信人的地址。   
Subject 主題   
Comments 備註   
Keywords 關鍵字,用來進一步搜尋郵件    
In-Reply-To 被當前郵件回覆的郵件的ID    
References 幾乎同In-Reply-To一樣   
Encrypted 加密郵件的加密型別   
Date 發信日期   

 

Address 

Javamail中關於Address的類共有3個類。分別是:
Javax.mail.Address (抽象基礎類) 
javax.mail.internet.InternetAddress (繼承於Address)
javax.mail.internet.NewsAddress  

1、Javax.mail.Address

抽象方法 
abstract String getType() 
Return a type string that identifies this address type. 
返回Address的型別。若InternetAddress則返回rfc822。
abstract String toString() 
Return a String representation of this address object. 
返回表述Address物件的String。
這個String只能包含US_ASCII碼。
static String toString(Address[] addresses, int used) 
Convert the given array of InternetAddress objects into a comma separated sequence of address strings. The resulting string contains only US-ASCII characters, and hence is mail-safe.  The 'used' parameter specifies the number of character positions already taken up in the field into which the resulting address sequence string is to be inserted. It is used to determine the line-break positions in the resulting address sequence string.
used引數用於指定要被插入字串的字元位置數。

2.解析  static InternetAddress[] parse(String addresslist)  

Parse the given comma separated sequence of addresses into InternetAddress objects.
把一個由逗號分隔的地址字串系列解析成InternetAddress陣列。地址必須遵循RFC822中的語法。 
static InternetAddress[] parse(String addresslist, boolean strict)  
Parse the given sequence of addresses into InternetAddress objects. If strict is false, simple  email addresses separated by spaces are also allowed. If strict is true, many (but not all) of the RFC822 syntax rules are enforced. In particular, even if strict is true, addresses composed of simple names (with no "@domain" part) are allowed. Such "illegal" addresses are not uncommon in real messages. 
把一個地址字串系列解析成InternetAddress陣列。若strict是false,由空格分隔的簡單郵件地址也是允許的。若strict是true,許多(但不是全部)RFC822語法規則將被執行。即使strict是true,若地址不包含(@域名)部分也是允許的。
沒有strict引數的通常用於解析使用者輸入的郵件地址字串 static InternetAddress[] parseHeader(String addresslist, boolean strict)  
Parse the given sequence of addresses into InternetAddress objects. 
把一個地址字串系列解析成InternetAddress陣列。若strict是false,個人地址的完整語法規則都不用執行。若strict是true,許多(但不是全部)RFC822語法規則將被執行。
為了更好的支援訊息中的無效地址。若strict是false,此方法執行比parse(“xx”,false)還要少的語法規則。若strict是true,此方法執行比parse(“xx”,true)更多的語法規則。

3.獲取當前使用者地址 

static InternetAddress getLocalAddress(Session session)  
Return an InternetAddress object representing the current user.