[Delphi]用IDSMTP發送郵件(正文和附件)

来源:互联网 发布:python 远程 编辑:程序博客网 时间:2024/05/18 00:38

        最近公司的一个系统,需要通过邮件发送excel,原本用的控件是TNMSMTP,结果发现邮件的附件大小有限制,超过一定的大小到收件人那就变成乱码,于是用IDSMTP替代,发现可以解决这个问题。

1.添加两个控件

TIdSMTP和TIdMessage

2.代码

Type

        SMTP: TIdSMTP;
        MSG: TIdMessage;

procedure Send_Mail()

var

    tStr: Tstrings;

begin

   tStr := TStringList.Create;

   tStr.add('Hello!This is a test file!');

   SMTP.Host := '172.21.232.163';

   MSG.Clear;  //注意如果这句不加,那只要程序不关闭,附件会一直累加,直到程序关闭才清空

   MSG.Recipients.Clear;

   MSG.Recipients.Add.Address := 'ljy22_2011@sina.com';

   MSG.From.Address := 'SFIS_SYSTEM';

   MSG.Body := tStr;

   MSG.Subject := 'Hello!Test!';

   MSG.From.Name := 'SFIS_SYSTEM';

   MSG.From.Text := 'SFIS_SYSTEM';

   TIdAttachment.Create(MSG.MessageParts,'D:\TEST\Test.xls');

try

   smtp.Connect();
   smtp.Authenticate;
   smtp.Send(msg); 

finally

   smtp.Disconnect;

end;

end.