2013-02-03 83 views
3

我使用以下代碼來評估消息。一個E郵件MSG的含量(體/線)與INDY接收10個部件使用Indy 10和DELPHI評估電子郵件

function LinesFromMsg(aMsg: TIdMessage): TStrings; 
var 
    i: Integer; 
begin 
    for i := 0 to aMsg.MessageParts.AttachmentCount-1 do 
    begin 
    if (amsg.MessageParts.Items[i].ContentType ='HTML') then 
    begin 
     if (amsg.MessageParts.Items[i] is Tidtext) then 
     Result := TidText(amsg.MessageParts.Items[i]).body; 
    end; 
    end; 
end; 

關於這個代碼我有2個問題:

一個),這是在尋找Tlines部分的正確方式仲裁郵件信息? (考慮INDY 10 EMAIL MSG PARTS所示的建議)

b)我可以在哪裏找到所有不同Contenttype字符串值的教程?

回答

6

要查找的正確ContentType值是text/html。使用Indy的IsHeaderMediaType()函數進行檢查,因爲ContentType值可能具有與其相關的附加屬性,您的比較需要忽略。

您還需要考慮TIdMessage.ContentType,因爲HTML電子郵件可能不是MIME編碼的,因此根本不使用TIdMessage.MessageParts集合。

最後,循環需要使用MessageParts.Count屬性,而不是MessageParts.AttachmentsCount屬性。

試試這個:

function HTMLFromMsg(aMsg: TIdMessage): TStrings; 
var 
    i: Integer; 
    Part: TIdMessagePart; 
begin 
    Result := nil; 
    if IsHeaderMediaType(aMsg.ContentType, 'text/html') then 
    begin 
    Result := aMsg.Body; 
    Exit; 
    end; 
    for i := 0 to aMsg.MessageParts.Count-1 do 
    begin 
    Part := aMsg.MessageParts.Items[i]; 
    if (Part is TIdText) and IsHeaderMediaType(Part.ContentType, 'text/html') then 
    begin 
     Result := TIdText(Part).Body; 
     Exit; 
    end; 
    end; 
end; 

隨着中說,這在技術上是不處理MIME的正確方法。正式的,一個符合要求的讀者應該通過MIME部分向後循環,因爲它們是從最簡單的形式向下到最複雜的形式。因此,您向後循環,將MIME嵌套考慮在內,尋找您支持的最複雜的表單。更多類似的東西(未經測試):

procedure DisplayPlainText(Body: TStrings); 
begin 
    // display plain text as needed... 
end; 

procedure DisplayHTML(Body: TStrings); 
begin 
    // display html as needed... 
end; 

procedure DisplayMultiPartAlternative(aMsg: TIdMessage; aParentIndex, aLastIndex: Integer); 
var 
    Part: TIdMessagePart; 
    i: Integer: 
begin 
    for i := aLastIndex-1 downto aParentIndex+1 do 
    begin 
    Part := aMsg.MessageParts.Items[i]; 
    if (Part.ParentPart = aParentIndex) and (Part is TIdText) then 
    begin 
     if IsHeaderMediaType(Part.ContentType, 'text/html') then 
     begin 
     DisplayHTML(TIdText(Part).Body); 
     Exit; 
     end; 
     if IsHeaderMediaType(Part.ContentType, 'text/plain') then 
     begin 
     DisplayPlain(TIdText(Part).Body); 
     Exit; 
     end; 
    end; 
    end; 
    // nothing supported to display... 
end; 

procedure DisplayMultiPartMixed(aMsg: TIdMessage; aParentIndex, aLastIndex: Integer); 
var 
    Part: TIdMessagePart; 
    i: Integer; 
begin 
    for i := aLastIndex-1 downto aParentIndex+1 do 
    begin 
    Part := aMsg.MessageParts.Items[i]; 
    if (Part.ParentPart = aParentIndex) and (Part is TIdText) then 
    begin 
     if IsHeaderMediaType(Part.ContentType, 'multipart/alternative') then 
     begin 
     DisplayMultiPartAlternative(aMsg, ParentPart.Index, aLastIndex); 
     Exit; 
     end; 
     if IsHeaderMediaType(ParentPart.ContentType, 'text/html') then 
     begin 
     DisplayHTML(TIdText(Part).Body); 
     Exit; 
     end; 
     if IsHeaderMediaType(Part.ContentType, 'text/plain') then 
     begin 
     DisplayHTML(TIdText(Part).Body); 
     Exit; 
     end; 
     aLastIndex := i; 
    end; 
    // nothing supported to display... 
    end; 


procedure DisplayMsg(aMsg: TIdMessage); 
var 
    ContentType: string; 
begin 
    ContentType := ExtractHeaderMediaType(aMsg.ContentType); 
    case PosInStrArray(ContentType, ['multipart/mixed', 'multipart/alternative', 'text/html', 'text/plain'], False) of 
    0: begin 
     DisplayMultiPartAlternative(aMsg, -1, aMsg.MessageParts.Count); 
     Exit; 
    end; 
    1: begin 
     DisplayMultiPartMixed(aMsg, -1, aMsg.MessageParts.Count); 
     Exit; 
    end; 
    2: begin 
     DisplayHTML(aMsg.Body); 
     Exit; 
    end; 
    3: begin 
     DisplayPlainText(aMsg.Body); 
     Exit; 
    end; 
    else 
    // nothing supported to display... 
    end; 
end; 
+1

其他用戶的信息:include unit IdGlobalProtocols – user1769184