邮件发送

这段代码展示了使用Delphi实现邮件发送的功能。设置了邮件的基本信息,如发件人、收件人、主题等,还配置了SMTP服务器的认证方式、主机和端口。在发送邮件时进行了异常处理,若发送失败会显示错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

procedure TForm1.Button2Click(Sender: TObject);
var
tmp:string;
begin
  with IdMessage1 do
  begin
    Clear; // 清除前一次產生的 body & headerm, 以免第二次按時重複
    Body.Assign(Memo1.Lines);
    From.Text := 'yangtang_newton@163.com';  // 寄件者
    Recipients.EMailAddresses := 'yangtang_newton@163.com'; // 收件者
    Subject := 'subject 主題';
    //Priority := TIdMessagePriority(2); // 0-4, 0表示最高優先順序
    //CCList.EMailAddresses := 'a@a.com;b@b.com';
    //BccList.EMailAddresses := 'd@d.com;e@e.com';      
    ReceiptRecipient.Text := '';
    //  ReceiptRecipient.Text := From.Text; //若要寄件回函,改成這行
  end;

  // 附加的檔案
//  TIdAttachment.Create(IdMessage1.MessageParts, 'c:/t1.zip');
//  TIdAttachment.Create(IdMessage1.MessageParts, 'c:/t2.zip');

  //SMTP server 認證的方式
//  IdSMTP1.AuthenticationType := atNone;  // 不用認證,要認證改為下面三行
  IdSMTP1.AuthenticationType := atLogin; {Simple Login}
  IdSMTP1.UserId := 'yangtang_newton';
  IdSMTP1.Password := '????';

  //一般設定
  IdSMTP1.Host := 'smtp.163.com';
  IdSMTP1.Port := 25;

  //寄送
  try
    IdSMTP1.Connect;
    try
      IdSMTP1.Send(IdMessage1);
      ShowMessage('E-Mail 已寄出');
    finally
      IdSMTP1.Disconnect;
    end;
  except
    on e: exception do
    begin
      ShowMessage('Mail 寄送失敗:' + e.Message);
    end;
  end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值