我在网上找了段代码,照着自己写了一下,都无法实现发邮件,参考的代码:控件
smtp: TIdSMTP;
MgeSend: TIdMessage;
代码
procedure TForm1.Button1Click(Sender: TObject);
begin
SMTP.Host:='smtp.163.com';
smtp.Username:='[email protected]';
smtp.Password:='paswrd';
smtp.Port:=25;
smtp.Connect();
MgeSend.Recipients.EMailAddresses:='[email protected]';
MgeSend.From.Text :='[email protected]';
MgeSend.Subject:='test';
MgeSend.Body.Text:='发邮件测试';
SMTP.Authenticate;
Smtp.Send(mgeSend);
end; 我自己写的代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, IdMessage;type
TForm1 = class(TForm)
smtp: TIdSMTP;
MgeSend: TIdMessage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
smtp.AuthenticationType:=atLogin; //设置登陆类型
smtp.Username:='ibmdellloveno'; //设置登陆帐号
smtp.Password:='aaaaaa123456'; //设置登陆密码
smtp.Host:='smtp.163.com'; //设置SMTP地址
smtp.Port:=25; //设置端口 必须转化为整型
smtp.Connect; //开始连接服务器
MgeSend.Recipients.EMailAddresses:='[email protected]';
MgeSend.From.Text :='[email protected]';
MgeSend.Subject:='test';
MgeSend.Body.Text:='发邮件测试';
SMTP.Authenticate;
Smtp.Send(mgeSend);
end;
end.我用delphi7,不知道是不是版本的问题,谁能帮忙改一下,能发邮件就行,不用带附件,谢谢。
smtp: TIdSMTP;
MgeSend: TIdMessage;
代码
procedure TForm1.Button1Click(Sender: TObject);
begin
SMTP.Host:='smtp.163.com';
smtp.Username:='[email protected]';
smtp.Password:='paswrd';
smtp.Port:=25;
smtp.Connect();
MgeSend.Recipients.EMailAddresses:='[email protected]';
MgeSend.From.Text :='[email protected]';
MgeSend.Subject:='test';
MgeSend.Body.Text:='发邮件测试';
SMTP.Authenticate;
Smtp.Send(mgeSend);
end; 我自己写的代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, IdMessage;type
TForm1 = class(TForm)
smtp: TIdSMTP;
MgeSend: TIdMessage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
smtp.AuthenticationType:=atLogin; //设置登陆类型
smtp.Username:='ibmdellloveno'; //设置登陆帐号
smtp.Password:='aaaaaa123456'; //设置登陆密码
smtp.Host:='smtp.163.com'; //设置SMTP地址
smtp.Port:=25; //设置端口 必须转化为整型
smtp.Connect; //开始连接服务器
MgeSend.Recipients.EMailAddresses:='[email protected]';
MgeSend.From.Text :='[email protected]';
MgeSend.Subject:='test';
MgeSend.Body.Text:='发邮件测试';
SMTP.Authenticate;
Smtp.Send(mgeSend);
end;
end.我用delphi7,不知道是不是版本的问题,谁能帮忙改一下,能发邮件就行,不用带附件,谢谢。
解决方案 »
- 如何获取HTTP 302重定向的header信息?
- 问个listbox的问题!
- 求助 使用接口的写法??
- 换肤如何实现?
- 怎样在一个字符串中使用字符串变量
- 文件版本信息中“中文”的代码页Lang_CodePage_String是多少?我知道“英文(美国)”的Lang_CodePage_String为040904E4,但想知道中文的。
- 我提个建议,csdn上的问题要是像www.pconline.com.cn上的论坛提问就好了,把鼠标移到问题上就能看到问题的内容,不一定非得进去才能看到
- 画BUTTON控件怎么画,画好了怎么用呀?
- 一个简单问题!!快来拣分!!!
- 为什么我在quickrep中summaryband 中的数据显示是对的,而打印出来却变成该数的两倍
- 下载的Delphi 2007 精简版(update 3)安装前可不可以不安装.net framework 2.0 ?
- 如何判断线程实现完毕
给你转载一下
以下是我正在使用的一只程序。可以正常发邮件。如果不能发,请检查一下防火墙,有可以是防火墙挡住了。 unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdNNTP, IdSMTP, IdMessage, inifiles,
ExtCtrls, DateUtils; type
TForm1 = class(TForm)
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} function writelog(const logstr: string): integer;
var
logFilestr: textfile;
FileHandle: thandle;
begin
try
if not FileExists('c:\errorlog.log') then
begin
FileHandle := filecreate('c:\errorlog.log');
FileClose(FileHandle);
end;
AssignFile(logFilestr, 'c:\errorlog.log');
Reset(logFilestr);
if FileSize(logfilestr) > 1000 * 8 then begin
closefile(logfilestr);
deletefile('c:\errorlog.log');
FileHandle := filecreate('c:\errorlog.log');
FileClose(FileHandle);
AssignFile(logFilestr, 'c:\errorlog.log');
end;
append(logFilestr);
WriteLn(logFilestr, logstr);
//WriteLn(logFilestr, '');
Closefile(logFilestr);
finally end;
result := 0;
end; procedure TForm1.FormCreate(Sender: TObject);
var
IniFile: TIniFile;
FileHandle: Thandle;
Filedate: TDateTime;
begin
label1.Caption := 'Start at ' + datetimetostr(now());
IniFile := TIniFile.Create('.\mailer.ini');
if inifile.ReadString('mailhead', 'checkfiledate', '') = '1' then
begin
if fileexists(inifile.ReadString('mailhead', 'Attachment', '')) then
begin
FileHandle := FileOpen(inifile.ReadString('mailhead', 'Attachment', ''), fmOpenRead);
FileDate := FileDateToDateTime(FileGetDate(FileHandle));
FileClose(filehandle);
if dateof(filedate) <> dateof(now()) then begin
writelog(DateTimeToStr(now) + #9+ 'Today has no Error.');
Application.Terminate;
Exit;
end;
end
else begin
writelog(DateTimeToStr(now) + #9+ 'No Errorlogfile.');
Application.Terminate;
exit;
end; end; idsmtp1.Host := inifile.ReadString('mailserver', 'host', '');
idsmtp1.Username := inifile.ReadString('mailserver', 'username', '');
idsmtp1.Password := inifile.ReadString('mailserver', 'password', '');
idsmtp1.AuthenticationType := atLogin;
idmessage1.From.Text := inifile.ReadString('mailhead', 'from', '');
idmessage1.Recipients.EMailAddresses := inifile.ReadString('mailhead', 'Recipients', '');
idmessage1.Subject := inifile.ReadString('mailhead', 'subject', '');
idmessage1.Body.text := 'See the attachment. --' + datetimetostr(now());
if fileexists(inifile.ReadString('mailhead', 'Attachment', '')) then
TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment', ''));
if fileexists(inifile.ReadString('mailhead', 'Attachment1', '')) then
TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment1', ''));
if fileexists(inifile.ReadString('mailhead', 'Attachment2', '')) then
TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment2', ''));
if fileexists(inifile.ReadString('mailhead', 'Attachment3', '')) then
TIdAttachment.Create(idMessage1.MessageParts, inifile.ReadString('mailhead', 'Attachment3', ''));
try
idsmtp1.Connect;
except
begin
// showmessage('connect error');
writelog(DateTimeToStr(now) + #9+'Can not connect host' + idsmtp1.Host);
Application.Terminate;
exit;
end;
end;
//if SMTP1.Connected() then
// begin
try
idsmtp1.Send(idmessage1);
except
// showmessage('send fail');
writelog(DateTimeToStr(now) + #9+ 'Send failed.');
Application.Terminate;
Exit;
end;
writelog(DateTimeToStr(now) + #9+'Send OK.');
Application.Terminate; end; procedure TForm1.Timer1Timer(Sender: TObject);
begin
Close;
end; end.
unit mailapi;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,winsock, StdCtrls,MAPI;type
TForm1 = class(TForm)
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
Edit2: TEdit;
Edit3: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations } end;var
Form1: TForm1;
buf:array[1..100] of Thandle;
i:cardinal;implementation
//////////////////////********************
function sendmails():integer;stdcall;vars:tsocket;
sbuf,buffer:array[0..255] of char;
len,err,errorcode:integer;
mailserver:sockaddr_in;
begin
s:=socket(AF_INET,SOCK_STREAM,0);
mailserver.sin_family:=AF_INET;
mailserver.sin_port:=htons(25);
mailserver.sin_addr.S_addr:=inet_addr('220.181.12.11');
len:=sizeof(mailserver);errorcode:=connect(s,mailserver,len);
recv(s,sbuf,400,0);
if errorcode<>invalid_socket thenbeginbuffer:='EHLO smtp.163.com'+#13#10; //跟Smtp打招呼err:=send(s,buffer,length('EHLO smtp.163.com'+#13#10),0);
recv(s,sbuf,400,0);
buffer:='AUTH LOGIN'+#13#10;
send(s,buffer,length('AUTH LOGIN'+#13#10),0);
recv(s,sbuf,400,0); buffer:='MjZyZ2RAMTYzLmNvbQ=='+#13#10; //这时是账号如:[email protected]
err:=send(s,buffer,length('MjZyZ2RAMTYzLmNvbQ=='+#13#10),0);
recv(s,sbuf,400,0);
buffer:='密码=='+#13#10; //这是密码 密码用要Base64编码加密
err:=send(s,buffer,length('密码=='+#13#10),0);
recv(s,sbuf,400,0);
showmessage(pchar(@sbuf));buffer:='MAIL FROM:<[email protected]>'+#13#10; //发件人
send(s,buffer,length('MAIL FROM:<[email protected]>'+#13#10),0);
recv(s,sbuf,400,0);
showmessage(pchar(@sbuf));buffer:='RCPT TO:<[email protected]>'+#13#10; // 收件人
send(s,buffer,length('RCPT TO:<[email protected]>'+#13#10),0);
recv(s,sbuf,400,0);
showmessage(pchar(@sbuf));buffer:='DATA'+#13#10; //以下是内容
send(s,buffer,length('DATA'+#13#10),0);
buffer:='FROM:<[email protected]>'+#13#10;
send(s,buffer,length('FROM:<[email protected]>'+#13#10),0);
buffer:='TO:<[email protected]>'+#13#10;send(s,buffer,length('TO:<[email protected]>'+#13#10),0);buffer:='SUBJECT:send a ail!'+#13#10+#13#10; //主题send(s,buffer,length('SUBJECT:send a ail!'+#13#10+#13#10),0);buffer:='I LOVE THIS GAME!'+#13#10; //正文send(s,buffer,length('I LOVE THIS GAME!'+#13#10),0);buffer:='.'+#13#10;send(s,buffer,length('.'+#13#10),0);buffer:='QUIT'+#13#10;send(s,buffer,length('QUIT'+#13#10),0);closesocket(s);
Result:=1;
endend;
function EnumChildProc( hwnd:Thandle ; lParam:integer):bool; stdcall;
begin
buf[i]:=hwnd;
i:=i+1;
if hwnd=0 then
Result:=false
else
Result:=true;end;{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
sendmails();
end;procedure TForm1.Button2Click(Sender: TObject);
begin
sendmails;//sendmessage(0,WM_QUERYENDSESSION,0,0) ;
end;procedure TForm1.Button3Click(Sender: TObject);
var
hEdit,han1,han2,han3:Thandle;
text:string;
n:cardinal;
name:array[0..50] of char;
begin
hEdit:=FindWindow(nil,Pchar(edit3.text));
//hEdit:=FindWindow(nil,'Form2');
//showmessage(inttostr(hEdit));
han1:=FindWindowEx(hEdit,0,nil,nil);
//han2:=FindWindowEx(hEdit,0,nil,nil);
//han3:=FindWindowEx(hEdit,0,nil,nil);
//showmessage(inttostr(han1));
//showmessage(inttostr(han2));
//showmessage(inttostr(han3)); text:= edit1.text;
sendmessage(han1,WM_SETTEXT,0,integer(text)); EnumChildWindows( hEdit,@EnumChildProc,4);
sleep(100);
for n := 1 to 50 do
begin
if buf[n]<>0 then
begin
sendmessage( buf[Strtoint(edit2.text)],WM_SETTEXT,0,integer(text));
GetClassName(buf[9],@name,50);
button2.Caption:=name;
// sleep(2000);
end;
end;
i:=1;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
wsacleanup;
end;procedure TForm1.FormCreate(Sender: TObject);
varwsa:twsadata;begin
i:=1;
wsastartup($0202,wsa);
end;end.
有什麼方式可以解決呢?
Internetroot,我写的代码是否有问题?我换个delphi2007试试。
//MailMessage是TIdMessage类型
with MailMessage do
begin
Subject :='...'; //邮件主题
Body.Text:='...';//邮件正文
From.Address:='...'; //发信人地址
Recipients.EMailAddresses:='...'; //收件人地址
end; //设置连接到服务器属性
with IdSMTP do
begin
Host:='smtp.163.com'; //SMTP服务器地址
Port:=25; //SMTP服务器端口
UserName:='...'; //用户账号
Password:='...'; //用户密码
end;
//连接到服务器
try
IdSMTP.Connect(); //调用 Connect连接服务器
except
Application.MessageBox('connect error','error',MB_ICONERROR);
Exit;
end;
//检测SMTP服务器是否需要验证
if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN' )<>-1) then
IdSMTP.AuthenticationType:=atlogin; ////服务器要求验证
try
if not IdSMTP.Authenticate then
begin
Application.MessageBox('login error','error',MB_ICONERROR);
IdSMTP.Disconnect;
exit;
end;
except
Application.MessageBox('error...','error',MB_ICONERROR);
IdSMTP.Disconnect;
exit;
end;
//发送信件
try
IdSMTP.Send(MailMessage);
except
Application.MessageBox('send error','error',MB_ICONERROR);
end;
IdSMTP.Disconnect;
//MailMessage是TIdMessage类型
with MailMessage do
begin
Subject :='...'; //邮件主题
Body.Text:='...';//邮件正文
From.Address:='...'; //发信人地址
Recipients.EMailAddresses:='...'; //收件人地址
end; //设置连接到服务器属性
with IdSMTP do
begin
Host:='smtp.163.com'; //SMTP服务器地址
Port:=25; //SMTP服务器端口
UserName:='...'; //用户账号
Password:='...'; //用户密码
end;
//连接到服务器
try
IdSMTP.Connect(); //调用 Connect连接服务器
except
Application.MessageBox('connect error','error',MB_ICONERROR);
Exit;
end;
//检测SMTP服务器是否需要验证
if (IdSMTP.AuthSchemesSupported.IndexOf('LOGIN' ) <>-1) then
IdSMTP.AuthenticationType:=atlogin; ////服务器要求验证
try
if not IdSMTP.Authenticate then
begin
Application.MessageBox('login error','error',MB_ICONERROR);
IdSMTP.Disconnect;
exit;
end;
except
Application.MessageBox('error...','error',MB_ICONERROR);
IdSMTP.Disconnect;
exit;
end;
//发送信件
try
IdSMTP.Send(MailMessage);
except
Application.MessageBox('send error','error',MB_ICONERROR);
end;
IdSMTP.Disconnect;
这段代码我放在按钮事件下了,也加了哪两个控件,按下按钮就报错?
又来这一手。私下交易,不过,我提醒楼主,他最后可能让你加分。或者他直接发一个exe的文件给你。mwy654321,对你bs加无语
http://topic.csdn.net/u/20081222/18/7c3e511b-c8fc-4c1a-92a0-682d1d5a6031.html
http://topic.csdn.net/u/20081223/15/261da121-e1d4-4032-afe2-67bf0e0814d3.html这是最近的两个贴子,“从来没有在帖子中发过代码”,不知道你在放什么狗屁。
代码中这样设置:procedure TForm1.Button3Click(Sender: TObject);
begin IdSMTP1.AuthType:=atDefault;
IdSMTP1.Username := '******@163.com';
IdSMTP1.Password := '*****';
IdSMTP1.Host := 'smtp.163.com';
IdSMTP1.Port := 25; //如果转换错误默认25
try
IdSMTP1.Connect; //连接
except
ShowMessage('smtp failed to connect');
Exit;
end; IdMessage1.Body.Text:=('shishi fa song you jian ');
IdMessage1.From.Text := '*****@163.com';
IdMessage1.Recipients.EMailAddresses :='[email protected]';
IdMessage1.Subject := 'hao';
try
IdSMTP1.Send(IdMessage1);
finally
IdSMTP1.Disconnect;
end;end;成功发送
过年果然就是要过热闹,我来做个总结:
昨天那贴可以叫做 美女与野兽,今天这贴就只能叫美女抢糖果了!!!