我用IdSMTP开发邮件发送的程序,但是当收件人的邮箱提供者和发件人的邮箱提供者相同是,就会报错,如************@tom.com(收件人邮箱),!!!!!!!!@tom.com(发件人邮箱),就会报错,代码如下:
var
MxList: TStringList;
i: Integer;
QName, ThoughAddress: string;
begin
if trim(edit5.Text)='' then
begin
showmessage('请输入收件人邮箱');
edit5.SetFocus;
exit;
end;
if trim(edit6.Text)='' then
begin
showmessage('请输入邮件主题');
edit6.SetFocus;
exit;
end;
if trim(edit7.Text)='' then
begin
showmessage('请输入发件人邮箱');
edit7.SetFocus;
exit;
end;
//设置邮件内容
IdMsgSend.From.Address:=trim(edit7.Text);
IdMsgSend.Recipients.EMailAddresses:=trim(edit5.Text);
IdMsgSend.Subject:=trim(edit6.Text);
IdMsgSend.Body.Text:=memo2.Text;
if FileExists(trim(edit8.Text)) then
TIdAttachment.Create(idmsgsend.MessageParts,trim(edit8.Text));
QName:=TrimRight(copy(edit5.Text, Pos('@', trim(edit5.Text)) + 1, Length(trim(edit5.Text))));
MxList:=TStringList.Create;
try
GetMxList(MxList, QName);
ThoughAddress := MxList.Names[0];
finally
MxList.Free;
end;
//发送邮件
try
try
IdSMTP1.Host:=ThoughAddress; //具体使用的SMTP,可以到你申请的邮箱所在的网站中去找
IdSMTP1.Port := 25;
IdSMTP1.Connect;
IdSMTP1.Send(IdMsgSend);
showmessage('邮件发送成功');
edit5.Text:='';
edit6.Text:='';
edit7.Text:='';
edit8.Text:='';
memo2.Text:='';
except
on E:Exception do
showmessage('错误: ' + E.Message);
end;
finally
if IdSMTP1.Connected then
IdSMTP1.Disconnect;
end;
var
MxList: TStringList;
i: Integer;
QName, ThoughAddress: string;
begin
if trim(edit5.Text)='' then
begin
showmessage('请输入收件人邮箱');
edit5.SetFocus;
exit;
end;
if trim(edit6.Text)='' then
begin
showmessage('请输入邮件主题');
edit6.SetFocus;
exit;
end;
if trim(edit7.Text)='' then
begin
showmessage('请输入发件人邮箱');
edit7.SetFocus;
exit;
end;
//设置邮件内容
IdMsgSend.From.Address:=trim(edit7.Text);
IdMsgSend.Recipients.EMailAddresses:=trim(edit5.Text);
IdMsgSend.Subject:=trim(edit6.Text);
IdMsgSend.Body.Text:=memo2.Text;
if FileExists(trim(edit8.Text)) then
TIdAttachment.Create(idmsgsend.MessageParts,trim(edit8.Text));
QName:=TrimRight(copy(edit5.Text, Pos('@', trim(edit5.Text)) + 1, Length(trim(edit5.Text))));
MxList:=TStringList.Create;
try
GetMxList(MxList, QName);
ThoughAddress := MxList.Names[0];
finally
MxList.Free;
end;
//发送邮件
try
try
IdSMTP1.Host:=ThoughAddress; //具体使用的SMTP,可以到你申请的邮箱所在的网站中去找
IdSMTP1.Port := 25;
IdSMTP1.Connect;
IdSMTP1.Send(IdMsgSend);
showmessage('邮件发送成功');
edit5.Text:='';
edit6.Text:='';
edit7.Text:='';
edit8.Text:='';
memo2.Text:='';
except
on E:Exception do
showmessage('错误: ' + E.Message);
end;
finally
if IdSMTP1.Connected then
IdSMTP1.Disconnect;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货