用Indy控件,通过需要简单认证的SMTP服务器,发送带附件的Email。

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)
    IdMessage1: TIdMessage;
    IdSMTP1: TIdSMTP;
    Edit1: TEdit;
    Label1: TLabel;
    Label4: TLabel;
    Edit4: TEdit;
    Label5: TLabel;
    Edit5: TEdit;
    Memo1: TMemo;
    Label6: TLabel;
    Edit6: TEdit;
    Label7: TLabel;
    Memo2: TMemo;
    Label8: TLabel;
    Button1: TButton;
    Label2: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure IdSMTP1Status(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: String);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Clear;
  IdSMTP1.Host:=Edit1.Text;
  IdSMTP1.Port:=25;
  IdSMTP1.Username:='用户名';
  IdSMTP1.Password:='密码';

  IdMessage1.From.Address:=Edit4.Text;
  IdMessage1.Recipients.EMailAddresses:=Edit5.Text;
  IdMessage1.Subject:=Edit6.Text;
  IdMessage1.Body.Text:=Memo2.Text;

  if FileExists(Edit2.Text) then
    TIdAttachment.Create(IdMessage1.MessageParts,Edit2.Text);

  try
    try
      IdSMTP1.Connect;
      if IdSMTP1.AuthSchemesSupported.IndexOf('LOGIN')>-1 then
      begin
        IdSMTP1.AuthenticationType := atLogin;
        IdSMTP1.Authenticate;
      end;
      IdSMTP1.Send(IdMessage1);
    except on E:Exception do
      Memo1.Lines.Insert(0, 'ERROR: ' + E.Message);
    end;
  finally
    if IdSMTP1.Connected then
    IdSMTP1.Disconnect;
  end;

end;

procedure TForm1.IdSMTP1Status(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: String);
begin
  memo1.Lines.Add('Status:'+AStatusText);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  OpenDialog1.Execute;
  if OpenDialog1.FileName<>'' then
    Edit2.Text:=OpenDialog1.FileName;
end;

end.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值