全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:9034
推到 Plurk!
推到 Facebook!

用Gmail寄信 (Delphi 7 + Delphi 2009)

 
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-05-01 15:40:48 IP:61.220.xxx.xxx 訂閱
用 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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-05-02 00:08:44 IP:203.73.xxx.xxx 訂閱
用 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]
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
編輯記錄
pcboy 重新編輯於 2009-05-18 20:11:46, 註解 無‧
pcboy 重新編輯於 2009-05-18 20:41:59, 註解 無‧
pcernet
初階會員


發表:69
回覆:113
積分:41
註冊:2002-11-29

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-07-17 07:49:42 IP:61.218.xxx.xxx 未訂閱
head   的部分(主旨和寄件人等)中文的部分出現的都是?號

加上
idMessage1.OnInitializeISO:=InitializeISO;
procedure TForm1.InitializeISO(var VHeaderEncoding: Char; var VCharSet:string);
begin
//VTransferHeader := bit8;
VHeaderEncoding := 'B';
VCharSet := 'big5';
end;

還是不行,謝謝!
mybanksoft
初階會員


發表:8
回覆:37
積分:29
註冊:2007-12-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-08-04 20:35:23 IP:118.231.xxx.xxx 訂閱
  1. 在delphi 2009所附的indy是10.2.5版,我將他換成indy 10.5.5版後就沒有亂碼的問題。
  2. 但是indy 的idmessage的主旨,顯示不出unicode,而內容(unicode的字體預設是細明體)及附加檔案都是ok的
  3. 程式及元件任何字型都不用設定,只要用預設即可,因為delphi 2009是真正的unocode
  4. 建議用svn升級,這樣的話随身可以更新,最新的版本(亂碼是因為indy尚未完全跟上delphi 2009或 C 2009的關係。
------
努力學習Rave
小萝卜头
一般會員


發表:7
回覆:5
積分:2
註冊:2009-08-30

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-08-30 03:56:41 IP:61.136.xxx.xxx 訂閱
DELPHI7可以吗?
------
初学者
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-08-31 11:44:47 IP:122.116.xxx.xxx 訂閱
你沒看到第一行就有寫
用 Gmail 寄信, Delphi 7 版, 測試可用

===================引 用 小萝卜头 文 章===================
DELPHI7可以吗?
TWY
高階會員


發表:2
回覆:133
積分:152
註冊:2009-09-02

發送簡訊給我
#7 引用回覆 回覆 發表時間:2009-09-02 09:51:01 IP:211.21.xxx.xxx 訂閱
前些日子類似寄件需求用了 AFQuickMail 元件來寫,看了這篇文章後打算仿此改成自動寄件。
此篇文章非常實用且貼心,謝謝 pcboy 板主的分享。


powerhowardchen
初階會員


發表:15
回覆:28
積分:28
註冊:2004-04-19

發送簡訊給我
#8 引用回覆 回覆 發表時間:2010-02-22 02:53:39 IP:118.165.xxx.xxx 訂閱
樓主寫的可能在當時可以用,但是在現今2010(Use Delphi 2010)時,卻不能用了。
我嘗試使用摟主的方式,是可以Connect,可是在Send時會失敗(完全當住,無法得到主機回應)。

經我測試可行的方案如下:
1.Port使用465
2.UseTLS改為utUseImplicitTLS
其他則照樓主的方法,即可發送郵件。
系統時間:2024-05-08 11:36:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!