用Delphi发邮件

用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;   


--咳,又忘了出处。。老早以前保存的。。。抱歉抱歉

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值