Indy 邮件发送的例子
Indy 邮件发送的例子 By Shaoyun 2010-3-22 1:15:29
代码实测使用163邮箱向QQ邮箱发送消息,信息多次成功发送!代码用到了Indy的两个组件IdSMTP和IdMessage,代码如下:
procedure TForm2.btn1Click(Sender: TObject);
begin
idsmtp1.Host:='smtp.163.com';
idsmtp1.Port:=25;
idsmtp1.Username:='12345';
idsmtp1.Password:='123456';
idmsg1.Subject:=edt2.Text;
idmsg1.Body.Text:=mmo1.Text;
idmsg1.CharSet:='gb2312';
idmsg1.From.Address:='12345@163.com';
idmsg1.Recipients.EMailAddresses:=edt1.Text;
try
idsmtp1.Connect;
try
idsmtp1.Send(idmsg1);
lbl1.Caption:='send success';
except
lbl1.Caption:='send field';
end;
finally
idsmtp1.Disconnect;
end;
end;
begin
idsmtp1.Host:='smtp.163.com';
idsmtp1.Port:=25;
idsmtp1.Username:='12345';
idsmtp1.Password:='123456';
idmsg1.Subject:=edt2.Text;
idmsg1.Body.Text:=mmo1.Text;
idmsg1.CharSet:='gb2312';
idmsg1.From.Address:='12345@163.com';
idmsg1.Recipients.EMailAddresses:=edt1.Text;
try
idsmtp1.Connect;
try
idsmtp1.Send(idmsg1);
lbl1.Caption:='send success';
except
lbl1.Caption:='send field';
end;
finally
idsmtp1.Disconnect;
end;
end;
注意事项:
1. 测试用的163的邮箱会验证From.Address发信人的邮箱地址是否与发信账号一致,所以必须保持发信账号和发信地址一致,其他邮箱或许不验证,具体看邮件服务器的情况。
2. 最好设置IdMessage的CharSet属性,不设置则中文会显示乱码。
代码测试环境:Windows XP SP3 + Delphi 2010