用Gmail寄信 (Delphi 7 + Delphi 2009) |
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
用 Gmail 寄信, Delphi 7 版, 測試可用
[code delphi] // 用 Gmail 寄信 // Delphi 7 Test OK // 請 Google 找尋 OpenSSL file for Delphi : libeay32.dll , ssleay32.dll // 放在程式所在的目錄 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, XPMan, StdCtrls, IdMessage, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdBaseComponent, IdComponent, IdIOHandler, IdIOHandlerSocket, IdSSLOpenSSL; type TForm1 = class(TForm) Button1: TButton; // Standard Button2: TButton; // Standard edtRecipients: TEdit; // Standard edtCC: TEdit; // Standard edtBCC: TEdit; // Standard edtSubject: TEdit; // Standard cbxConfirmReading: TCheckBox; // Standard ListBox1: TListBox; // Standard cbxPriority: TComboBox; // Standard mmMessage: TMemo; // Standard XPManifest1: TXPManifest; // Win32 odAttachments: TOpenDialog; // Dialogs IdSMTP1: TIdSMTP; // Indy Clients IdMessage1: TIdMessage; // Indy Misc IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocket; // Indy I/O Handlers procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin mmMessage.Text := ''; Button1.Caption := 'Attach File'; Button2.Caption := 'Send mail'; cbxPriority.Items.Add('0'); cbxPriority.Items.Add('1'); cbxPriority.Items.Add('2'); end; procedure TForm1.Button1Click(Sender: TObject); begin if odAttachments.Execute then ListBox1.Items.Add(odAttachments.FileName); end; procedure TForm1.Button2Click(Sender: TObject); var xAnexo: Integer; begin // IdMessage1.Recipients.EMailAddresses := edtRecipients.Text; IdMessage1.Recipients.EMailAddresses := '收件者Email'; IdMessage1.From.Address := '帳號@gmail.com'; // IdMessage1.CCList.EMailAddresses := edtCC.Text; IdMessage1.CCList.EMailAddresses := ''; // IdMessage1.BccList.EMailAddresses := edtBCC.Text; IdMessage1.BccList.EMailAddresses := ''; IdSMTP1.AuthenticationType := atLogin; IdSMTP1.Host := 'smtp.gmail.com'; IdSMTP1.Username := '帳號'; // 不含 @gmail.com IdSMTP1.Password := '密碼'; IdSMTP1.Port := 465; IdSMTP1.IOHandler := IdSSLIOHandlerSocket1; IdSSLIOHandlerSocket1.SSLOptions.Method := sslvSSLv2; IdSSLIOHandlerSocket1.SSLOptions.Mode := sslmClient; case cbxPriority.ItemIndex of 0: IdMessage1.Priority := mpLow; 1: IdMessage1.Priority := mpNormal; 2: IdMessage1.Priority := mpHigh; end; IdMessage1.Subject := edtSubject.Text; IdMessage1.Body := mmMessage.Lines; if cbxConfirmReading.Checked then IdMessage1.ReceiptRecipient.Text := IdMessage1.From.Text; {Auto answer} for xAnexo := 0 to ListBox1.Items.Count - 1 do TIdAttachment.Create(idmessage1.MessageParts,TFileName(ListBox1.Items[xAnexo])); // TIdAttachment.Create(idmessage1.MessageParts,TFileName(ListBox1.Items.String[xAnexo])); Button2.Caption := 'Connecting...'; IdSMTP1.Connect(6000); if IdSMTP1.Connected then begin Button2.Caption := 'Connected'; try Button2.Caption := 'Sending Message...'; IdSMTP1.Send(IdMessage1); finally IdSMTP1.Disconnect; end; Button2.Caption := 'Message sent successfully!'; end else MessageDlg('Error in Connection!',mtConfirmation,[mbOK],0); IdSMTP1.Disconnect; Sleep(1000); Button2.Caption := '&Send'; end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! 編輯記錄
pcboy 重新編輯於 2009-05-18 20:11:15, 註解 無‧
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
用 Gmail 寄信 , Delphi 2009 版, 測試可用
[code delphi] // Sendmail by Gmail // Delphi 2009 測試OK // 需要 OpenSSL for Delphi 2009 // 下載網址 http://indy.fulgan.com/SSL/ // 把 openssl-0.9.8k-i386-win32.zip 解壓縮, 兩個 dll 放到程式目錄 // Component // Button1: TButton; // IdSMTP1: TIdSMTP; // IdMessage1: TIdMessage; // IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; // Uses // IdAttachmentFile; //----------------------------------------------------------------- unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP, StdCtrls, IdAttachmentFile; type TForm1 = class(TForm) Button1: TButton; IdSMTP1: TIdSMTP; IdMessage1: TIdMessage; IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure SendGmailEmail(const toAddress, subject, body: String; const attachment: String = ''); var IdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL; IdSMTP: TIdSMTP; IdMsg: TIdMessage; begin IdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil); try IdSSLIOHandlerSocketOpenSSL.Destination := 'smtp.gmail.com:587'; IdSSLIOHandlerSocketOpenSSL.Host := 'smtp.gmail.com'; //IdSSLIOHandlerSocketOpenSSL.MaxLineAction := maException; IdSSLIOHandlerSocketOpenSSL.Port := 587; IdSSLIOHandlerSocketOpenSSL.DefaultPort := 0; IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := sslvTLSv1; IdSSLIOHandlerSocketOpenSSL.SSLOptions.Mode := sslmUnassigned; IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyMode := []; IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyDepth := 0; IdSMTP := TIdSMTP.Create(nil); try IdSMTP.IOHandler := IdSSLIOHandlerSocketOpenSSL; IdSMTP.Host := 'smtp.gmail.com'; IdSMTP.Port := 587; IdSMTP.UseTLS := utUseExplicitTLS; //IdSMTP.Username := GMailUserName; IdSMTP.Username := '帳戶@gmail.com'; IdSMTP.Password := '密碼'; IdSMTP.Connect; try IdMsg := TIdMessage.Create; try IdMsg.From.Address := '帳戶@gmail.com'; IdMsg.Recipients.EMailAddresses := toAddress; IdMsg.Subject := subject; IdMsg.Body.Text := body; if attachment <> '' then begin if FileExists(attachment) then TIdAttachmentFile.Create(IdMsg.MessageParts, attachment) else raise Exception.Create('"' attachment '" not found.'); end; IdSMTP.Send(IdMsg); finally IdMsg.Free; end; finally IdSMTP.Disconnect; end; finally IdSMTP.Free; end; finally IdSSLIOHandlerSocketOpenSSL.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin SendGmailEmail('收件者@Email.com', 'TestMail', 'TestBody', ''); Button1.Caption :='Send OK'; end; end. [/code]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
pcernet
初階會員 發表:69 回覆:113 積分:41 註冊:2002-11-29 發送簡訊給我 |
|
mybanksoft
初階會員 發表:8 回覆:37 積分:29 註冊:2007-12-25 發送簡訊給我 |
------
努力學習Rave |
小萝卜头
一般會員 發表:7 回覆:5 積分:2 註冊:2009-08-30 發送簡訊給我 |
|
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
TWY
高階會員 發表:2 回覆:133 積分:152 註冊:2009-09-02 發送簡訊給我 |
|
powerhowardchen
初階會員 發表:15 回覆:28 積分:28 註冊:2004-04-19 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |