发送 E-mail 程序问题? 谢谢! ( 积分: 100 )

  • 主题发起人 主题发起人 赛特
  • 开始时间 开始时间

赛特

Unregistered / Unconfirmed
GUEST, unregistred user!
代码是这样的:
memo1.Lines.Clear;

IdSMTP1.Host := combobox1.Text; //SMTP 服务器
IdSMTP1.Port := 25;

IdMessage1.From.Address := edit2.Text; //发件人地址
IdMessage1.Recipients.EMailAddresses := edit3.Text + ',' + combobox1.Text;

IdMessage1.Subject := edit4.Text; //邮件主题
IdMessage1.Body.Text := memo2.Text; //邮件内容

if fileexists(edit5.Text) then //粘贴附件
TIdAttachment.Create(IdMessage1.MessageParts , edit5.Text);

try
try
IdSMTP1.Connect(1000);
IdSMTP1.Send(IdMessage1);
except on E:Exception do
Memo1.Lines.Insert(0, 'ERROR: ' + E.Message);
end;
finally
if IdSMTP1.Connected then IdSMTP1.Disconnect;
end;

如果:“SMTP 服务器”写的是:SMTP.163.com (edit1.text)
“发件人”写的是:XXX@163.com (edit2.text)
“收件人”写的是:XXX@sina.com (edit3.text)
结果程序运行报错:ERROR: 用户被锁定?是什么原因呢?

如果:“SMTP 服务器”写的是:SMTP.sina.com (edit1.text)
“发件人”写的是:XXX@sina.com (edit2.text)
“收件人”写的是:XXX@163.com (edit3.text)
结果程序运行报错:ERROR: <unknown[222.180.166.17]>: Client host rejected: Access denied?是什么原因呢?

应该怎样改才能成功发送电子邮件呢?谢谢!
 
你这个邮箱在其它地方是否能用,比如,foxmail/outlook/oe
 
邮箱没问题,163 和 sina 的都能登陆
 
//------------------------

{DNASendEMail(SMTP服务器地址, 发送MAIL用户名, 发送MAIL的密码, 发送MAIL全名, 接收MAIL[可和发送MAIL相同],信笺主题, 信笺内容):Bool;
Send Email Unit One By 疯仔
Email:144423@qq.com
}
unit SendMail;

interface
uses windows, winsock;
function DNASendEMail(PSmtp,PUser,PPass,PGetMail,PTOMail,Subject,MailText:string):boolean;
implementation
var
SendBody:string;
const
CRLF=#13#10;
BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
function StrLen(const Str: PChar): Cardinal; assembler;
asm
MOV EDX,EDI
MOV EDI,EAX
MOV ECX,0FFFFFFFFH
XOR AL,AL
REPNE SCASB
MOV EAX,0FFFFFFFEH
SUB EAX,ECX
MOV EDI,EDX
end;
function StrCopy(Dest: PChar; const Source: PChar): PChar; assembler;
asm
PUSH EDI
PUSH ESI
MOV ESI,EAX
MOV EDI,EDX
MOV ECX,0FFFFFFFFH
XOR AL,AL
REPNE SCASB
NOT ECX
MOV EDI,ESI
MOV ESI,EDX
MOV EDX,ECX
MOV EAX,EDI
SHR ECX,2
REP MOVSD
MOV ECX,EDX
AND ECX,3
REP MOVSB
POP ESI
POP EDI
end;
function StrPas(const Str: PChar): string;
begin
Result := Str;
end;
function FindInTable(CSource:char):integer;
begin
result:=Pos(string(CSource),BaseTable)-1;
end;
function EncodeBase64(Source:string):string;
var
Times,LenSrc,i:integer;
x1,x2,x3,x4:char;
xt:byte;
begin
result:='';
LenSrc:=length(Source);
if LenSrc mod 3 =0 then Times:=LenSrc div 3
else Times:=LenSrc div 3 + 1;
for i:=0 to times-1 do
begin
if LenSrc >= (3+i*3) then
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
xt:=xt or (ord(Source[2+i*3]) shr 4);
x2:=BaseTable[xt+1];
xt:=(Ord(Source[2+i*3]) shl 2) and 60;
xt:=xt or (ord(Source[3+i*3]) shr 6);
x3:=BaseTable[xt+1];
xt:=(ord(Source[3+i*3]) and 63);
x4:=BaseTable[xt+1];
end
else if LenSrc>=(2+i*3) then
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
xt:=xt or (ord(Source[2+i*3]) shr 4);
x2:=BaseTable[xt+1];
xt:=(ord(Source[2+i*3]) shl 2) and 60;
x3:=BaseTable[xt+1];
x4:='=';
end else
begin
x1:=BaseTable[(ord(Source[1+i*3]) shr 2)+1];
xt:=(ord(Source[1+i*3]) shl 4) and 48;
x2:=BaseTable[xt+1];
x3:='=';
x4:='=';
end;
result:=result+x1+x2+x3+x4;
end;
end;
function LookupName(const Name: string): TInAddr;
var
HostEnt: PHostEnt;
InAddr: TInAddr;
begin
HostEnt := gethostbyname(PChar(Name));
FillChar(InAddr, SizeOf(InAddr), 0);
if HostEnt <> nil then
begin
with InAddr, HostEnt^ do
begin
S_un_b.s_b1 := h_addr^[0];
S_un_b.s_b2 := h_addr^[1];
S_un_b.s_b3 := h_addr^[2];
S_un_b.s_b4 := h_addr^[3];
end;
end;
Result := InAddr;
end;
function StartNet(host:string;port:integer;var sock:integer):Boolean;
var
wsadata:twsadata;
FSocket:integer;
SockAddrIn:TSockAddrIn;
err:integer;
begin
err:=WSAStartup($0101,WSAData);
FSocket:=socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
if FSocket=invalid_socket then begin
Result:=False;
Exit;
end;
SockAddrIn.sin_addr:=LookupName(host);
SockAddrIn.sin_family := PF_INET;
SockAddrIn.sin_port :=htons(port);
err:=connect(FSocket,SockAddrIn, SizeOf(SockAddrIn));
if err=0 then begin
sock:=FSocket;
Result:=True;
end
else
begin
Result:=False;
end;
end;
procedure StopNet(Fsocket:integer);
var
err:integer;
begin
err:=closesocket(FSocket);
err:=WSACleanup;
end;
function SendData(FSocket:integer;SendStr:string):integer;
var
DataBuf:array[0..4096] of char;
err:integer;
begin
strcopy(DataBuf,pchar(SendStr));
err:=send(FSocket,DataBuf,strlen(DataBuf),MSG_DONTROUTE);
Result:=err;
end;
function GetData(FSocket:integer):String;
const
MaxSize=1024;
var
DataBuf:array[0..MaxSize] of char;
err:integer;
begin
err:=recv(FSocket,DataBuf,MaxSize,0);
Result:=Strpas(DataBuf);
end;
function DNASendEMail(psmtp,puser,ppass,pgetmail,PTOMail,subject,mailtext:string):boolean;
var
FSocket,res:integer;
begin
Result:=false;
sendbody:='SendEmail Unit By Anskya ';
if StartNet(PSmtp, 25, FSocket) then
begin
SendData(FSocket, 'HELO ' +Puser+ CRLF);
getdata(FSocket);
SendData(FSocket, 'AUTH LOGIN' + CRLF);
getdata(FSocket);
SendData(FSocket, EncodeBase64(Puser) + CRLF);
getdata(FSocket);
SendData(FSocket, EncodeBase64(PPass) + CRLF);
getdata(FSocket);
SendData(FSocket, 'MAIL FROM: <' + PGetMail + '>' + CRLF);
getdata(FSocket);
SendData(FSocket, 'RCPT TO: <' + PTOMail + '>' + CRLF);
getdata(FSocket);
SendData(FSocket, 'DATA' + CRLF);
getdata(FSocket);
SendBody := 'From:信息 <' + PGetMail + '>' + CRLF
+ 'To: <' + PGetMail + '>' + CRLF
+ 'Subject: ' + Subject + CRLF
+ CRLF
+ MailText + CRLF
+ '.' + CRLF;
res := SendData(FSocket, SendBody);
getdata(FSocket);
SendData(FSocket, 'QUIT' + CRLF);
getdata(FSocket);
StopNet(Fsocket);
if res <> SOCKET_ERROR then
begin
Result:=true;
end;
end;
end;
end.
 
有这么复杂吗?我就需要用INDY控件发送简单邮件就行了
 
调用演示

新建一个工程.. 加上以上SendMail.pas...

一个 button, 七个 edit ...

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,sendmail;

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
smtp:string;
user:string;
pas:string;
alluser:string;
smail:string;
biaoti:string;
neirong:string;

begin
smtp:=edit1.Text;//SMTP服务器地址 smtp.qq.com
user:=edit2.text; // 发送MAIL用户名 144423
pas:=edit3.text; //发送MAIL的密码 123456
alluser:=edit4.text; // 发送MAIL全名 144423@qq.com
smail:=edit5.text; //接收MAIL全名 144423@qq.com
biaoti:=edit6.text; //信标题 能收到吗?
neirong:=edit7.text; //信内容 真的能啊!!呵呵!
DNASendEMail(pchar(smtp),pchar(user),pchar(pas),pchar(alluser),pchar(smail),pchar(biaoti),pchar(neirong))

end;

end.
 
都封装好了..
DNASendEMail(pchar(smtp),pchar(user),pchar(pas),pchar(alluser),pchar(smail),pchar(biaoti),pchar(neirong));
直接这样调用就好了..
还不简单啊.我刚测试能发送..
 
谢谢,我试试
 
我是这样发送的:
smtp:='smtp.sina.com';
user:='saite006';
pas:='XX';
alluser:='saite006@sina.com';
smail:='saite006@sina.com';
biaoti:='hello';
neirong:='您好!';

if DNASendEMail(pchar(smtp),pchar(user),pchar(pas),pchar(alluser),pchar(smail),pchar(biaoti),pchar(neirong)) then showmessage('ok');

执行后,能顺利showmessage('ok'),但信箱里面没收到呢?而且照你那样,用QQ邮箱也不能收到呢?你直接把源程序发给我吗?saite006@sina.com
 
谢谢,学习
 
接受答案了.
 
后退
顶部