2011-08-12 280 views
13

我正在使用INDY在端口25上使用SMTP客戶端發送電子郵件,沒有任何問題。使用Gmail從DELPHI(Indy)使用TLS發送SMTP

現在我需要使用Gmail帳戶發送電子郵件,並且我需要使用TLS。

任何人都可以提供一個簡單的示例如何做到這一點。

感謝

+1

頂部結果上谷歌 「印+ SMTP +的Gmail」 的揭示:[發送郵件使用Gmail印至](HTTP://www.marcocantu.c om/tips/oct06_gmail.html) –

+1

我過去曾使用Marco Cantu的指南:http://www.marcocantu.com/tips/oct06_gmail.html – berndvf

回答

6

此代碼的工作對於Gmail:

begin 
    IDSMTP1 := TIDSMTP.Create; 
    IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create; 
    try 

    with IDSMTP1 do 
    begin 
     Host := srvr.Host; 
     Port := srvr.Port; 
     if (srvr.needAuthentication = 'Y') then 
     AuthType := satDefault 
     else 
     AuthType := satNone; 
     IOHandler := IdSSLIOHandlerSocketOpenSSL1; 

     if (srvr.secureMode = 'Y') then 
     UseTLS := utUseRequireTLS 
     else 
     UseTLS := utNoTLSSupport; 

     Username := srvr.Username; 
     Password := srvr.Password; 
    end; 

    idMBHTML := TIdMessageBuilderHTML.Create; 
    Idmessage1 := TIDMessage.Create; 

    try 
     with idMBHTML do 
     begin 
     enc := TEncoding.Unicode; 
     HTML.LoadFromStream(FEmlMsg.MsgBody, enc); 
     for c := 0 to FEmlMsg.Attachmnts.Count - 1 do 
      Attachments.Add(FEmlMsg.Attachmnts[c]); 
     FillMessage(IDMessage1); 
     end; 

     with Idmessage1 do 
     begin 
     Subject := FEmlMsg.MsgSubject; 
     From.Address := FEmlMsg.FromAddress; 
     From.Name := FEmlMsg.FromName; 
     Recipients.EMailAddresses := FEmlMsg.RecipientAddress; 
     if FEmlMsg.ReceiptRecipientAddress <> '' then 
      ReceiptRecipient.Address := FEmlMsg.ReceiptRecipientAddress; 
     if FEmlMsg.ReceiptRecipientName <> '' then 
      ReceiptRecipient.Name := FEmlMsg.ReceiptRecipientName; 
     end; 

     with IDSMTP1 do 
     begin 
     if not Connected then 
      Connect; 
     Send(IdMessage1); 
     end; 

    finally 
     Idmessage1.Free; 
     idMBHTML.Free; 
    end; 
    finally 
    IDSMTP1.Free; 
    IdSSLIOHandlerSocketOpenSSL1.Free; 
    end; 
end; 
+0

已測試過你的自我?我在連接時遇到錯誤:EIdOSSLCouldNotLoadSSLLibrary帶有'無法加載SSL庫'消息。 – DRokie

+0

確保您爲您的Indy版本使用了正確版本的OpenSSL DLL。使用Indy的'WhichFailedToLoad()'函數來確定OpenSSL的哪些部分未能加載。 –

+0

正如Remy上面提到的,您必須將OpenSSL DLL文件與您的應用程序一起包含在內。它們應該與應用程序可執行文件位於同一個文件夾中,或者應位於系統路徑中(例如System32文件夾)。您可以從Indy網站下載最新的文件。 –

1

你需要SSL DLL使IdSSLIOHandler工作。

有一個在Indy SSL Website 信息這您重定向到Fulgan Download Site

您可以下載適合您的平臺一個包,然後包括2個DLL文件與應用程序。我自己正在使用2個DLL與indy組件通過電子郵件發送郵件超過2年。唯一的問題是發送速度很慢。

0

你不說你正在使用什麼版本的Indy。 要使用TSL(Hotmail/GMaill中的最後一次安全更改),您需要使用Indy 10.

請參閱description here

其實y使用此configuraction來使用Gmail發送郵件,它正常工作:

  • 郵件服務器= smtp.gmail.com
  • 的PortServer = 465
  • [email protected](同郵件)
  • SSLMethod = sslvTLSv1