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

xe6 使用gmail的問題

答題得分者是:GrandRURU
linwinshin
一般會員


發表:7
回覆:13
積分:4
註冊:2015-03-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2015-03-05 16:47:58 IP:220.132.xxx.xxx 訂閱
拜讀soredtown前輩的大作 用"Delphi XE5 Android APP EMail至GMail" 使用前輩提供的code, 在xe6上 可以compile,但是一直都沒法傳送 ,出現'EMail Error'
煩請前輩幫忙看看下面的code有甚麼問題? 謝謝
procedure TForm1.Button1Click(Sender: TObject); begin with IdMessage1 do begin Recipients.EMailAddresses := 'test@gmail.com';// From.Address := 'test@gmail.com'; From.Name := 'Self'; CCList.EMailAddresses := ''; BccList.EMailAddresses := ''; Priority := mpNormal; Subject := 'Test Mail Subject'; Body.Add('Test Mail Body'); end; with IdSMTP1 do begin Host := 'smtp.gmail.com'; Username := 'xxxxxxx';//使用我的帳戶名稱.不含"@gmail.com" Password := 'yyyyyyy'; IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Self); Port := 465; UseTLS := utUseImplicitTLS; try Connect; except end; if not Connected then begin Showmessage('EMail Error'); Exit; end; try Send(IdMessage1); finally Disconnect; end; end; end; //在project1.exe的目錄夾有裝openssl.exe ,libeay32.dll, ssleay32.dll
 


------
林文信
pcplayer99
尊榮會員


發表:142
回覆:740
積分:591
註冊:2003-01-21

發送簡訊給我
#2 引用回覆 回覆 發表時間:2015-03-05 18:18:37 IP:120.236.xxx.xxx 訂閱
如果你用 TIdSMTP 和 TIdPOP3 的话,记得要加 SSL。
GrandRURU
站務副站長


發表:234
回覆:1654
積分:1752
註冊:2005-06-21

發送簡訊給我
#3 引用回覆 回覆 發表時間:2015-03-06 08:16:35 IP:221.120.xxx.xxx 未訂閱
 排版很亂,預計又是射後不理的


Sending email with attachments using Delphi, Indy 10.5.5 and GMail
http://www.lastaddress.net/2013/05/sending-email-with-attachments-using.html?m=1
linwinshin
一般會員


發表:7
回覆:13
積分:4
註冊:2015-03-05

發送簡訊給我
#4 引用回覆 回覆 發表時間:2015-03-06 15:13:50 IP:220.132.xxx.xxx 訂閱
謝謝2位前輩的回答 我正努力測試 
因為第一次使用新版 不知道為何排版會出現不好看的畫面 前輩多包含

原來應該是這樣
procedure TForm1.Button1Click(Sender: TObject);
begin
with IdMessage1 do
begin
Recipients.EMailAddresses :=
'test@gmail.com';//
From.Address := 'test@gmail.com';
From.Name := 'Self';
CCList.EMailAddresses := '';
BccList.EMailAddresses := '';
Priority := mpNormal;
Subject := 'Test Mail Subject';
Body.Add('Test Mail Body');
end;

with IdSMTP1 do
begin
Host := 'smtp.gmail.com';
Username := 'xxxxxxx';//
使用我的帳戶名稱.不含"@gmail.com"
Password := 'yyyyyyy';
IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
Port := 465;
UseTLS := utUseImplicitTLS;

try
Connect;
except
end;

if not Connected then
begin
Showmessage('EMail Error');
Exit;
end;

try
Send(IdMessage1);
finally
Disconnect;
end;
end;
end;


//
project1.exe的目錄夾有裝openssl.exe ,libeay32.dll, ssleay32.dll
------
林文信
aftcast
站務副站長


發表:81
回覆:1482
積分:1762
註冊:2002-11-21

發送簡訊給我
#5 引用回覆 回覆 發表時間:2015-03-06 15:41:08 IP:114.32.xxx.xxx 訂閱


try
Connect;
except

改成

Connect; // 就這一行就好


於是你可能就會跳出錯誤訊息… 接著把那錯誤的內容或畫面 po 上來。才會知道是發生什麼事。
------



蕭沖
--All ideas are worthless unless implemented--

C++ Builder Delphi Taiwan G+ 社群
http://bit.ly/cbtaiwan
GrandRURU
站務副站長


發表:234
回覆:1654
積分:1752
註冊:2005-06-21

發送簡訊給我
#6 引用回覆 回覆 發表時間:2015-03-06 17:55:32 IP:59.120.xxx.xxx 未訂閱
 Username := 'xxxxxxx';//使用我的帳戶名稱.不含"@gmail.com"

含@gmail.com也不行嗎?
看起來只有這個地方有差異
pcplayer99
尊榮會員


發表:142
回覆:740
積分:591
註冊:2003-01-21

發送簡訊給我
#7 引用回覆 回覆 發表時間:2015-03-07 18:43:06 IP:120.236.xxx.xxx 訂閱
我在 D7 INDY9 底下连接 Gmail 成功过。代码找不到了。否则我就直接贴出来了。
linwinshin
一般會員


發表:7
回覆:13
積分:4
註冊:2015-03-05

發送簡訊給我
#8 引用回覆 回覆 發表時間:2015-03-09 20:28:03 IP:220.132.xxx.xxx 訂閱
謝謝2位版主及前輩的答覆.  試了很久還是試不出來.  上傳我的檔案.  想請前輩們幫忙看看哪裡出錯.   我在gmail臨時建了一個  帳號 .例題有說明.    .謝謝


unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdMessage, Vcl.StdCtrls,
IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
IdMessageClient, IdSMTPBase, IdSMTP, IdIOHandler, IdIOHandlerSocket,
IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdAntiFreezeBase, Vcl.IdAntiFreeze;
type
TForm1 = class(TForm)
Button1: TButton;
IdMessage1: TIdMessage;
IdSMTP1: TIdSMTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
IdAntiFreeze1: TIdAntiFreeze;
ename: TEdit;
epass: TEdit;
EPORT: TEdit;
towho: TEdit;
fromwho: TEdit;
Label1: TLabel;
Label2: TLabel;
fromname: TEdit;
procedure Button1Click(Sender: TObject);
// procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
with IdMessage1 do
begin
Recipients.EMailAddresses :=towho.Text;// 'gmrlinwinshin@gmail.com';
From.Address :=fromwho.Text ;//'gmrlinwinshin@gmail.com';
From.Name :=fromname.Text ;//'Self';
CCList.EMailAddresses := '';
BccList.EMailAddresses := '';
Priority := mpNormal;
Subject := 'Test Mail Subject';
Body.Add('Test Mail Body');
end;
with IdSMTP1 do
begin
Host := 'smtp.gmail.com';
Username := ename.text; // 'gmrlinwinshin@gmail.com';
Password := epass.text; //'abc77777';
IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(Self);
Port :=STRTOINT(EPORT.Text) ;//465;
UseTLS := utUseImplicitTLS;
try
Connect;
except
end;
if not Connected then
begin
Showmessage('EMail Error');
Exit;
end;
try
Send(IdMessage1);
finally
Disconnect;
end;
end;
end;


end.
------
林文信
GrandRURU
站務副站長


發表:234
回覆:1654
積分:1752
註冊:2005-06-21

發送簡訊給我
#9 引用回覆 回覆 發表時間:2015-03-10 13:54:16 IP:59.120.xxx.xxx 未訂閱
我沒有XE7
而且我照著我提供的參考網址做是沒有問題的

要不要考慮換一下寫法?


[code delphi]
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 }
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',
'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.
[/code]
GrandRURU
站務副站長


發表:234
回覆:1654
積分:1752
註冊:2005-06-21

發送簡訊給我
#10 引用回覆 回覆 發表時間:2015-03-10 14:08:23 IP:59.120.xxx.xxx 未訂閱
因為程式碼貼上有問題,而且不能修改,重新回文

如果還是看不到,可以到這裡看。

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 }
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',
'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.
linwinshin
一般會員


發表:7
回覆:13
積分:4
註冊:2015-03-05

發送簡訊給我
#11 引用回覆 回覆 發表時間:2015-03-12 19:02:32 IP:220.132.xxx.xxx 訂閱
謝謝副站長的例題
這2天試了多次 也換了幾台電腦都不行. 我想我一定有那裡弄錯了.程度真是不夠. 想請副站長把整個DEMO 含執行檔 .DLL一起POST來. 不知道副站長方不方便 ? 謝謝
------
林文信
系統時間:2017-11-22 11:34:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!