緣起
我在KTOP中回答「xe6 使用gmail的問題」這個主題,因為程式碼編排有問題,故在這裡另外轉貼。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdExplicitTLSClientServerBase, IdSMTP, IdSSLOpenSSL, IdMessage, IdAttachmentFile, Vcl.StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } /// <summary> /// Sends an email using the specified SMTP server. /// </summary> /// <param name="sendTo">The email address of the recipient.</param> /// <param name="subject">The subject of the email.</param> /// <param name="body">The body of the email.</param> /// <param name="attachFiles">A TStringList containing file paths to be attached to the email. Can be nil if no attachments are required.</param> /// <param name="smtpHost">The hostname or IP address of the SMTP server.</param> /// <param name="smtpPort">The port number of the SMTP server.</param> /// <param name="smtpUser">The username to authenticate with the SMTP server.</param> /// <param name="smtpPass">The password to authenticate with the SMTP server.</param> /// <param name="tls">The type of TLS encryption to use. Can be utNoTLSSupport if TLS is not supported by the SMTP server.</param> /// <returns>True if the email was sent successfully, false otherwise.</returns> function SendEmail(sendTo: string; subject: string; body: string; attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser: string; smtpPass: string; tls: TIdUseTLS): boolean; end; var Form1: TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin SendEmail( 'mybuddy@example.com', // 目的地E-Mail 'This is the subject', 'This is the body of the email....', nil, 'smtp.gmail.com', 587, 'myusername@gmail.com', // 登入帳號 'mypassword'{ 登入密碼 }, utUseExplicitTLS); end; function TForm1.SendEmail(sendTo, subject, body: string; attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser, smtpPass: string; tls: TIdUseTLS): boolean; var smtp: TIdSmtp; ssl: TIdSSLIOHandlerSocketOpenSSL; msg: TIdMessage; i: Integer; begin smtp:=TIdSmtp.Create(nil); ssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil); msg:=TIdMessage.Create(nil); try try smtp.Host:=smtpHost; smtp.Port:=smtpPort; smtp.Username:=smtpUser; smtp.Password:=smtpPass; //smtp.OnConnected :=IdSMTP1Connected; //smtp.OnDisconnected :=IdSMTP1Disconnected; //smtp.OnFailedRecipient :=IdSMTP1FailedRecipient; //smtp.OnStatus :=IdSMTP1Status; //smtp.OnTLSNotAvailable :=IdSMTP1TLSNotAvailable; //smtp.OnWork :=IdSMTP1Work; if not (tls=utNoTLSSupport) then begin ssl.Destination:=smtpHost + ':' + IntToStr(smtpPort); ssl.Host:=smtpHost; ssl.Port:=smtpPort; ssl.SSLOptions.Method:=sslvTLSv1; //ssl.OnStatusInfo:=IdSSLIOHandlerSocketOpenSSL1StatusInfo; //ssl.OnGetPassword:=IdSSLIOHandlerSocketOpenSSL1GetPassword; //ssl.OnStatus:=IdSSLIOHandlerSocketOpenSSL1Status; smtp.IOHandler:=ssl; smtp.UseTLS:=tls; end; msg.Recipients.EMailAddresses := sendTo; msg.Subject:=subject; msg.Body.Text:=body; if(Assigned(attachFiles)) then begin for i := 0 to attachFiles.Count - 1 do begin if FileExists(attachFiles[i]) then TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]); end; end; smtp.Connect; smtp.Send(msg); smtp.Disconnect; result:=true; finally msg.Free; ssl.Free; smtp.Free; end; except result:=false; end; end; end. |
結論:
使用 Indy 10.6.1 連接 Gmail SMTP 並成功發送和接收電子郵件。它在 Delphi XE7 中運作正常,可以作為開發人員實現電子郵件發送功能的參考。
和你分享。😉