DelphiXE10.1BerlinIndy发送QQ邮件
DelphiXE10.1BerlinIndy发送QQ邮件
突发奇想, 如果服务在运⾏过程中, 出现某些问题, 需要提醒管理员时, 就发⼀封邮件出来, 在⽹上也了些资料, 学习了下使⽤Indy发送QQ邮件.
发件⼈我⽤的QQ邮箱, 关于这个, QQ邮箱需要进⾏⼀些设置, 进⽽获取授权码.
QQ邮箱设置可参考:
获取授权码后, 当邮箱密码⽤即可.
代码如下:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
IdMessageClient, IdSMTPBase, IdSMTP, IdMessage, IdIOHandler,
IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdSSLOpenSSLHeaders;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label1: TLabel;
Edit1: TEdit;
GroupBox2: TGroupBox;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
Edit3: TEdit;
GroupBox3: TGroupBox;
Label4: TLabel;
Edit4: TEdit;
GroupBox4: TGroupBox;
Label5: TLabel;
Memo1: TMemo;
Button1: TButton;
IdSMTP: TIdSMTP;
IdMessage: TIdMessage;
Label6: TLabel;
Edit5: TEdit;
Label7: TLabel;
Edit6: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
fIdSSL: TIdSSLIOHandlerSocketOpenSSL;
public
{ Public declarations }
end;
var
Form1: TForm1;
iResult: boolean;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
IdSMTP.Host:= Trim(Edit1.Text);                  //SMTP地址
IdSMTP.Port:=  StrToInt(Edit5.Text);            //端⼝
IdSMTP.Username:= Trim(Edit2.Text);              //⽤户名
IdSMTP.Password:= Trim(Edit3.Text);              //密码
try
try
IdSMTP.Connect;                                  //建⽴连接
IdMessage.Recipients.EMailAddresses:= Trim(Edit4.Text);  //收件⼈地址      IdMessage.Subject:= Trim(Edit6.Text);                    //邮件标题
IdMessage.CharSet:= 'UTF-8';
IdMessage.Body.Clear;
IdMessage.Body.Add('This is my first email!');          //邮件正⽂
qq用户名IdMessage.From.Address:= Trim(Edit2.Text);              //发件⼈地址
IdMessage.Priority:= mpHigh;                            //优先级
IdSMTP.Send(IdMessage);                                  //发送邮件
ShowMessage('发送成功!');
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
finally
IdSMTP.Disconnect();
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
fIdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
fIdSSL.SSLOptions.Method := sslvSSLv23;
fIdSSL.SSLOptions.Mode := sslmClient;
IdSMTP.IOHandler:= fIdSSL;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if Assigned(fIdSSL) then
FreeAndNil(fIdSSL);
end;
initialization
iResult := IdSSLOpenSSLHeaders.Load();
end.
点击发送后, 收件⼈收到:
需要注意的是, 使⽤了SSL的两个dll⽂件(libeay32.dll, ssleay32.dll). 这两个⽂件需要根据开发⼯具的版本去对应的.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。