首页 技术 正文
技术 2022年11月21日
0 收藏 714 点赞 2,605 浏览 2600 个字

idMessage / idSMTP

首先对idMessage类的各种属性进行赋值(邮件的基本信息,如收件人、邮件主题、邮件正文等),其次通过idSMTP连接邮箱服务器,最后通过idSMTP的Send方法将idMessage发送出去。

界面布局如下:

Delphi – Indy TIdMessage和TIdSMTP实现邮件的发送

代码如下:

 unit uMain; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, RzPanel, RzShellDialogs, IdMessage, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdMessageClient, IdSMTP,
RzButton, StdCtrls, RzEdit, RzBtnEdt, Mask, RzLabel; type
TMainFrm = class(TForm)
gbMsgSet: TRzGroupBox;
gbSrvSet: TRzGroupBox;
lbSubject: TRzLabel;
lbRsd: TRzLabel;
lbCc: TRzLabel;
lbBCc: TRzLabel;
lbAth: TRzLabel;
lbBdy: TRzLabel;
lbUserName: TRzLabel;
lbHost: TRzLabel;
lbPsd: TRzLabel;
edtSub: TRzEdit;
edtRsd: TRzEdit;
edtCc: TRzEdit;
edtBCc: TRzEdit;
beAth: TRzButtonEdit;
mmBdy: TRzMemo;
btnSendMail: TRzBitBtn;
edtUN: TRzEdit;
edtHst: TRzEdit;
edtPsd: TRzEdit;
IdSMTP: TIdSMTP;
IdMessage: TIdMessage;
odMain: TRzOpenDialog;
procedure beAthButtonClick(Sender: TObject);
procedure btnSendMailClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
MainFrm: TMainFrm; implementation {$R *.dfm} procedure TMainFrm.beAthButtonClick(Sender: TObject);
begin
with odMain do
begin
Execute;
if FileName <> '' then
begin
beAth.Text := FileName;
end;
end;
end; procedure TMainFrm.btnSendMailClick(Sender: TObject);
begin
try
if (Trim(edtCc.Text) = '') and (Trim(edtRsd.Text) = '') and (Trim(edtBCc.Text) = '') then
begin
MessageDlg('You should input Rsd, please check,thanks!', mtInformation, [mbOK], );
edtRsd.SetFocus;
Exit;
end;
with IdMessage do
begin
Clear;
Subject := edtSub.Text;
From.Text := edtUN.Text;
Recipients.EMailAddresses := edtRsd.Text;
CCList.EMailAddresses := edtCC.Text;
BccList.EMailAddresses := edtBCc.Text;
Priority := TIdMessagePriority();
if Trim(beAth.Text) <> '' then
begin
TIdAttachment.Create(MessageParts, Trim(beAth.Text));
end;
Body.Assign(mmBdy.Lines);
end;
except
on E: Exception do
begin
MessageDlg('Msg Set Failed with Err information [' + E.Message + ']', mtWarning, [mbOK], );
Exit;
end;
end;
try
if (Trim(edtUN.Text) = '') or (Trim(edtHst.Text) = '') or (Trim(edtPsd.Text) = '') then
begin
MessageDlg('You should input UN, please check,thanks!', mtInformation, [mbOK], );
edtUN.SetFocus;
Exit;
end;
with IdSMTP do
begin
if Connected then Disconnect;
AuthenticationType := atLogin;
Port := ;
UserName := edtUN.Text;
Password := edtPsd.Text;
Host := edtHst.Text;
Connect;
end;
except
on E: Exception do
begin
MessageDlg('Srv Set Failed with Err information [' + E.Message + ']', mtWarning, [mbOK], );
Exit;
end;
end; try
IdSMTP.Send(IdMessage);
IdSMTP.Disconnect;
MessageDlg('OK!', mtInformation, [mbOK], );
except
on E: Exception do
begin
MessageDlg('Send Failed with Err information [' + E.Message + ']', mtWarning, [mbOK], );
Exit;
end;
end; end; end.

作者:Jeremy.Wu
  出处:https://www.cnblogs.com/jeremywucnblog/

  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,993
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845