2014-12-23 155 views
1

我想附帶的MemoryStream發送電子郵件,但我總是得到了在我收到的電子郵件,不帶附件此消息「這是MIME格式的多部分消息......」與MemoryStream的發送電子郵件附

代碼:

var 
    MemStrm : TMemoryStream; 
begin 
    MemStrm := TMemoryStream.Create; 
    Str1.SaveToStream(MemStrm); 
    MemStrm.Position := 0; 

    try 
    with IdSSLIOHandlerSocketOpenSSL1 do 
    begin 
    Destination   := 'smtp.mail.yahoo.com' + ':' + IntToStr(465); 
    Host     := 'smtp.mail.yahoo.com'; 
    MaxLineAction   := maException; 
    Port     := 465; 
    SSLOptions.Method  := sslvTLSv1; 
    SSLOptions.Mode  := sslmUnassigned; 
    SSLOptions.VerifyMode := []; 
    SSLOptions.VerifyDepth := 0; 
    end; 
    with IdSMTP1 do 
    begin 
    IOHandler   := IdSSLIOHandlerSocketOpenSSL1; 
    Host    := 'smtp.mail.yahoo.com'; 
    Port    := 465; 
    AuthType   := satDefault; 
    Password   := 'password'; 
    Username   := '[email protected]'; 
    UseTLS    := utUseImplicitTLS; 
    end; 
    with IdMessage1 do 
    begin 

    From.Address    := '[email protected]'; 

    List:= Recipients.Add; 
    List.Address:= '[email protected]'; 
    Recipients.EMailAddresses := '[email protected]'; 
    Subject     := 'test'; 
    Body.Text     := 'test'; 
    ContentType    := 'text/plain'; 

    IdAttachment := TIdAttachmentMemory.Create(IdMessage1.MessageParts,MemStrm); 
    IdAttachment.ContentType := 'text/plain'; 
    IdAttachment.FileName := 'attached.txt'; 
    end; 
    finally 
    idsmtp1.Connect; 
    idsmtp1.Send(idmessage1); 
    idsmtp1.Disconnect; 
    MemStrm.Free; 
    end; 
end 

我不想將其保存爲文件第一然後安裝它,我怎麼重視樣本的MemoryStream到我的郵箱?

編輯:它沒有與TIdAttachmentFile甚至工作,我使用Indy10版本10.6.0.5049德爾福7

+0

哪個版本的Indy是:請使用Recipients.Add.AddressRecipients.EMailAddresses,不都使用? (當前是10.6.1 iirc) – mjn

+0

delphi 7 indy10版本10.6.0.5049,只是更新了我的問題,即使使用TIdAttachmentFile也是如此,同樣的問題。 – user3533628

回答

2

要設置頂級TIdMessage.ContentTypetext/plain,它告訴讀者解釋整個電子郵件爲純文本。您需要在屬性設置爲multipart/mixed代替,那麼讀者解釋附件:

with IdMessage1 do 
begin 
    ... 
    ContentType := 'multipart/mixed'; 
end; 

我也建議你添加一個TIdText對象,讓讀者知道郵件是關於什麼的,例如:

IdText := TIdText.Create(IdMessage1.MessageParts, nil); 
IdText.ContentType := 'text/plain'; 
IdText.Body.Text := 'here is an attachment for you.'; 

IdAttachment := TIdAttachmentMemory.Create(IdMessage1.MessageParts,MemStrm); 
IdAttachment.ContentType := 'text/plain'; 
IdAttachment.FileName := 'attached.txt'; 

另外,您正在填寫Recipients屬性兩次。

with IdMessage1 do 
begin 
    ... 
    List:= Recipients.Add; 
    List.Address := '[email protected]'; 
    ... 
end; 

with IdMessage1 do 
begin 
    ... 
    Recipients.EMailAddresses := '[email protected]'; 
    ... 
end; 
-1

的問題是IdAttachment的ContentType的定義。
你必須將其設置爲'application/x-zip-compressed'這樣的:

IdAttachment := TIdAttachmentMemory.Create(IdMessage1.MessageParts,MemStrm); 
IdAttachment.ContentType := 'application/x-zip-compressed'; 
IdAttachment.FileName := 'attached.txt'; 
+0

這是錯誤的答案。您正在嘗試使用錯誤的值爲錯誤的屬性修復錯誤。 –

相關問題