📄 frmemailsender.pas
字号:
unit frmEmailSender;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, Grids, Buttons, Menus, IdBaseComponent,
IdMessage, IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient,
IdSMTP, ImgList;
type
TFormEmailSender = class(TForm)
bbtnCancel: TBitBtn;
bbtnOk: TBitBtn;
btnAttachment: TBitBtn;
cboPriority: TComboBox;
chkReturnReciept: TCheckBox;
edtBCC: TEdit;
edtCC: TEdit;
edtSubject: TEdit;
edtTo: TEdit;
grpAttachment: TGroupBox;
IdMsgSend: TIdMessage;
lblBCC: TLabel;
lblCC: TLabel;
lblPriority: TLabel;
lblSubject: TLabel;
lblTo: TLabel;
lvFiles: TListView;
Memo1: TMemo;
pnlBottom: TPanel;
pnlButtons: TPanel;
pnlMainDetails: TPanel;
pnlSmallButtons: TPanel;
pnlTop: TPanel;
pnlTopLeft: TPanel;
StatusBar1: TStatusBar;
SMTP: TIdSMTP;
OpenDialog1: TOpenDialog;
BitBtn1: TBitBtn;
procedure bbtnOkClick(Sender: TObject);
procedure btnAttachmentClick(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure SMTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
private
{ Private declarations }
procedure ResetAttachmentListView;
public
{ Public declarations }
procedure SetToEmail(AValue: string);
procedure SetAttachmentFileName(AValue: string);
end;
var
FormEmailSender: TFormEmailSender;
{TODO: Handle message body which is typed in the RichEdit}
implementation
uses IniFiles, CommonFunc, frmEmailConfig;
{$R *.DFM}
procedure TFormEmailSender.bbtnOkClick(Sender: TObject);
var
LFromEmail, LServerName, LUserName, LPassword: string;
LPort: Integer;
LIsAuth: Boolean;
LIniFile: TIniFile;
begin
LIniFile := TIniFile.Create(GetApplicationPath + CONFIGFILENAME);
LFromEmail := LIniFile.ReadString('SysConfig', 'From', '');
LServerName := LIniFile.ReadString('SysConfig', 'ServerName', '');
LUserName := LIniFile.ReadString('SysConfig', 'UserName', '');
LPassword := LIniFile.ReadString('SysConfig', 'Password', '');
LPort := LIniFile.ReadInteger('SysConfig', 'Port', 25);
LIsAuth := LIniFile.ReadBool('SysConfig', 'IsAuth', true);
LIniFile.Free;
with IdMsgSend do
begin
Body.Assign(Memo1.Lines);
From.Text := LFromEmail;
ReplyTo.EMailAddresses := LFromEmail;
Recipients.EMailAddresses := edtTo.Text; { To: header }
Subject := edtSubject.Text; { Subject: header }
Priority := TIdMessagePriority(cboPriority.ItemIndex); { Message Priority }
CCList.EMailAddresses := edtCC.Text; {CC}
BccList.EMailAddresses := edtBCC.Text; {BBC}
if chkReturnReciept.Checked then
begin {We set the recipient to the From E-Mail address }
ReceiptRecipient.Text := From.Text;
end
else
begin {indicate that there is no receipt recipiant}
ReceiptRecipient.Text := '';
end;
end;
{authentication settings}
case LIsAuth of
false: SMTP.AuthenticationType := atNone;
true: SMTP.AuthenticationType := atLogin; {Simple Login}
end;
SMTP.Username := LUserName;
SMTP.Password := LPassword;
{General setup}
SMTP.Host := LServerName;
SMTP.Port := LPort;
{now we send the message}
SMTP.Connect;
try
SMTP.Send(IdMsgSend);
finally
SMTP.Disconnect;
end;
end;
procedure TFormEmailSender.btnAttachmentClick(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
TIdAttachment.Create(IdMsgSend.MessageParts, OpenDialog1.FileName);
ResetAttachmentListView;
end;
end;
procedure TFormEmailSender.ResetAttachmentListView;
var li: TListItem;
idx: Integer;
begin
lvFiles.Items.Clear;
for idx := 0 to Pred(IdMsgSend.MessageParts.Count) do
begin
li := lvFiles.Items.Add;
if IdMsgSend.MessageParts.Items[idx] is TIdAttachment then
begin
li.ImageIndex := 0;
li.Caption := TIdAttachment(IdMsgSend.MessageParts.Items[idx]).Filename;
li.SubItems.Add(TIdAttachment(IdMsgSend.MessageParts.Items[idx]).ContentType);
end
else
begin
li.ImageIndex := 1;
li.Caption := IdMsgSend.MessageParts.Items[idx].ContentType;
end;
end;
end;
procedure TFormEmailSender.SetToEmail(AValue: string);
begin
edtTo.Text := AValue;
end;
procedure TFormEmailSender.SetAttachmentFileName(AValue: string);
begin
TIdAttachment.Create(IdMsgSend.MessageParts, AValue);
ResetAttachmentListView;
end;
procedure TFormEmailSender.BitBtn1Click(Sender: TObject);
var
LDlg: TFormEmailConfig;
begin
LDlg := TFormEmailConfig.Create(nil);
LDlg.ShowModal;
LDlg.Free;
end;
procedure TFormEmailSender.SMTPWorkEnd(Sender: TObject;
AWorkMode: TWorkMode);
begin
LCShowMessage('发送成功!');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -