大家好,欢迎来到IT知识分享网。用DELPHI发邮件
控件
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
代码
IdSMTP1.Host:=’smtp.163.com’;
IdSMTP1.Port:=25;
IdSMTP1.Username:=’发信人名称;
IdSMTP1.Password:=’发信人邮箱密码’;
IdSMTP1.Connect();
IdMessage1.Body.Clear;
IdMessage1.Body.Add(信的内容);
IdMessage1.From.Text:=’从哪发的,应该可以邮箱欺骗’;
IdMessage1.Recipients.EMailAddresses:=’发到哪个邮箱’;
IdMessage1.Subject:=’邮件题目’;
IdSMTP1.AuthenticationType := atLogin;
IdSMTP1.Authenticate;
IdSMTP1.Send(IdMessage1);
IdSMTP1.Disconnect;
end;
重点来了,有朋友试验这个代码说不好用,刚开始我测试也不行,在csdn上用了几百分也没弄明白是怎么回事,
后来一个偶然的机会发现,用新注册的邮箱用这个代码是发不了了,用用过一段时间的邮箱就可以发,新注册用户只能通过web方式发邮件,
估计与在论坛注册的新用户在某个设定时间内发不了帖子是一个道理。
或者:
我写了一个发邮件的函数,包你满意
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes,
Graphics, Controls, Forms, Dialogs, StdCtrls, IdComponent,
IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP, IdBaseComponent,
IdMessage;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TLoginEmailServer=record
SMTPHost:string;
SMTPPort:integer;
Username:string;
Password:string;
SmtpAuthType:integer;
end;
function SendEmail(poSMTPServer:TLoginEmailServer;poBody:Tstrings;psFromEmial,
psToEmail,psSubject:string;psContentType:string;
CCToEmail:string;poAttachmentPath:TStrings):integer;
var
loIdMsgSend: TIdMessage;
loSMTP: TIdSMTP;
i:integer;
begin
Result:=3;
loIdMsgSend:=nil;
loSMTP:=nil;
try
loIdMsgSend:=TIdMessage.Create(nil);
loSMTP:=TIdSMTP.Create(nil);
with loIdMsgSend do
begin
ContentType:=psContentType;
From.Text := psFromEmial;
ReplyTo.EMailAddresses := psFromEmial;
Recipients.EMailAddresses := psToEmail;
CCList.EMailAddresses:=CCToEmail;
Subject := psSubject;
Priority := mpHigh;
ReceiptRecipient.Text := ”;
Body.Assign(poBody);
if Assigned(poAttachmentPath) then
begin
for i := 0 to poAttachmentPath.Count-1 do
begin
TIdAttachment.Create(loIdMsgSend.MessageParts,poAttachmentPath.Strings[i]);
end;
end;
end;
with loSMTP do
begin
Host :=poSMTPServer.SMTPHost;
Port := poSMTPServer.SMTPPort;
if poSMTPServer.SmtpAuthType=1 then
AuthenticationType:=atLogin
else
AuthenticationType:=atNone;
Username := poSMTPServer.Username;
Password := poSMTPServer.Password;
try
Connect;
Send(loIdMsgSend);
except
result:=2;
exit;
end;
Result:=0;
end;
finally
loIdMsgSend.Free;
loSMTP.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SendEmail(………);
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
begin
try
IdSMTP1.AuthenticationType:=atLogin; //设置登陆类型
IdSMTP1.Username:=Edit1.Text; //设置登陆帐号
IdSMTP1.Password:=Edit2.Text; //设置登陆密码
IdSMTP1.Host:=Edit3.Text; //设置SMTP地址
IdSMTP1.Port:=strtoint(Edit4.Text); //设置端口 必须转化为整型
IdSMTP1.Connect; //开始连接服务器
except
Showmessage(‘连接失败,请重试!’);
Exit; //连接失败 的话 退出该执行过程
end;
IdMessage1.Body.Clear; //先清空上次发送的内容
IdMessage1.Subject:=Edit5.Text; //设置邮件发送的标题
IdMessage1.Body.Assign(Memo1.Lines); //设置邮件发送的主体
IdMessage1.From.Address:=Edit6.Text; //设置邮件的发件人 也就是说该邮件来自什么地方
IdMessage1.Recipients.EMailAddresses:=Edit7.Text; //收件人的地址
try
idSMTP1.Send(IdMessage1);
Showmessage(‘邮件发送成功!’);
except
Showmessage(‘邮件发送失败!’);
end;
end;
–咳,又忘了出处。。老早以前保存的。。。抱歉抱歉
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/26073.html