分享
 
 
 

直接用WinSock API 发E-mail.

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

unit SMTP_Connections;

//------------------------------------------

//定义单元

//---------------------------------------------

interface

uses

Classes, StdCtrls;

const

WinSock = 'wsock32.dll';

Internet = 2;

Stream = 1;

fIoNbRead = $4004667F;

WinSMTP = $0001;

LinuxSMTP = $0002;

type

TWSAData = packed record

wVersion: Word;

wHighVersion: Word;

szDescription: array[0..256] of Char;

szSystemStatus: array[0..128] of Char;

iMaxSockets: Word;

iMaxUdpDg: Word;

lpVendorInfo: PChar;

end;

PHost = ^THost;

THost = packed record

Name: PChar;

aliases: ^PChar;

addrtype: Smallint;

Length: Smallint;

addr: ^Pointer;

end;

TSockAddr = packed record

Family: Word;

Port: Word;

Addr: Longint;

Zeros: array[0..7] of Byte;

end;

function WSAStartup(Version:word; Var Data:TwsaData):integer; stdcall; far; external winsock;

function socket(Family,Kind,Protocol:integer):integer; stdcall; far; external winsock;

function shutdown(Socket,How:Integer):integer; stdcall; far; external winsock;

function closesocket(socket:Integer):integer; stdcall; far; external winsock;

function WSACleanup:integer; stdcall; far; external winsock;

function bind(Socket:Integer; Var SockAddr:TSockAddr; AddrLen:integer):integer; stdcall; far; external winsock;

function listen(socket,flags:Integer):integer; stdcall; far; external winsock;

function connect(socket:Integer; Var SockAddr:TSockAddr; AddrLen:integer):integer; stdcall; far; external winsock;

function accept(socket:Integer; Var SockAddr:TSockAddr; Var AddrLen:Integer):integer; stdcall; far; external winsock;

function WSAGetLastError:integer; stdcall; far; external winsock;

function recv(socket:integer; data:pchar; datalen,flags:integer):integer; stdcall; far; external winsock;

function send(socket:integer; var data; datalen,flags:integer):integer; stdcall; far; external winsock;

function gethostbyname(HostName:PChar):PHost; stdcall; far; external winsock;

function WSAIsBlocking:boolean; stdcall; far; external winsock;

function WSACancelBlockingCall:integer; stdcall; far; external winsock;

function ioctlsocket(socket:integer; cmd: Longint; var arg: longint): Integer; stdcall; far; external winsock;

function gethostname(name:pchar; size:integer):integer; stdcall; far; external winsock;

procedure _authSendMail(MailServer,uname,upass,mFrom,mFromName,mToName,Subject:string;mto,mbody:TStringList);

function ConnectServer(mhost:string;mport:integer):integer;

function ConnectServerwin(mhost:string;mport:integer):integer;

function DisConnectServer:integer;

function Stat: string;

function SendCommand(Command: String): string;

function SendData(Command: String): string;

function SendCommandWin(Command: String): string;

function ReadCommand: string;

function encryptB64(s:string):string;

var

mconnHandle: Integer;

mFin, mFOut: Textfile;

EofSock: Boolean;

mactive: Boolean;

mSMTPErrCode: Integer;

mSMTPErrText: string;

mMemo: TMemo;

implementation

uses

SysUtils, Sockets, IdBaseComponent,

IdCoder, IdCoder3to4, IdCoderMIME, IniFiles,Unit1;

var

mClient: TTcpClient;

procedure _authSendMail(MailServer, uname, upass, mFrom, mFromName,

mToName, Subject: string; mto, mbody: TStringList);

var

tmpstr: string;

cnt: Integer;

mstrlist: TStrings;

RecipientCount: Integer;

begin

if ConnectServerWin(Mailserver, 25) = 250 then

begin

Sendcommandwin('AUTH LOGIN ');

SendcommandWin(encryptB64(uname));

SendcommandWin(encryptB64(upass));

SendcommandWin('MAIL FROM: ' + mfrom);

for cnt := 0 to mto.Count - 1 do

SendcommandWin('RCPT TO: ' + mto[cnt]);

Sendcommandwin('DATA');

SendData('Subject: ' + Subject);

SendData('From: "' + mFromName + '" <' + mfrom + '>');

SendData('To: ' + mToName);

SendData('Mime-Version: 1.0');

SendData('Content-Type: multipart/related; boundary="Esales-Order";');

SendData(' type="text/html"');

SendData('');

SendData('--Esales-Order');

SendData('Content-Type: text/html;');

SendData(' charset="iso-8859-9"');

SendData('Content-Transfer-Encoding: QUOTED-PRINTABLE');

SendData('');

for cnt := 0 to mbody.Count - 1 do

SendData(mbody[cnt]);

Senddata('');

SendData('--Esales-Order--');

Senddata(' ');

mSMTPErrText := SendCommand(crlf + '.' + crlf);

try

mSMTPErrCode := StrToInt(Copy(mSMTPErrText, 1, 3));

except

end;

SendData('QUIT');

DisConnectServer;

end;

end;

function Stat: string;

var

s: string;

begin

s := ReadCommand;

Result := s;

end;

function EchoCommand(Command: string): string;

begin

SendCommand(Command);

Result := ReadCommand;

end;

function ReadCommand: string;

var

tmp: string;

begin

repeat

ReadLn(mfin, tmp);

if Assigned(mmemo) then

mmemo.Lines.Add(tmp);

until (Length(tmp) < 4) or (tmp[4] <> '-');

Result := tmp

end;

function SendData(Command: string): string;

begin

Writeln(mfout, Command);

end;

function SendCommand(Command: string): string;

begin

Writeln(mfout, Command);

Result := stat;

end;

function SendCommandWin(Command: string): string;

begin

Writeln(mfout, Command + #13);

Result := stat;

end;

function FillBlank(Source: string; number: Integer): string;

var

a: Integer;

begin

Result := '';

for a := Length(trim(Source)) to number do

Result := Result + ' ';

end;

function IpToLong(ip: string): Longint;

var

x, i: Byte;

ipx: array[0..3] of Byte;

v: Integer;

begin

Result := 0;

Longint(ipx) := 0;

i := 0;

for x := 1 to Length(ip) do

if ip[x] = '.' then

begin

Inc(i);

if i = 4 then Exit;

end

else

begin

if not (ip[x] in ['0'..'9']) then Exit;

v := ipx[i] * 10 + Ord(ip[x]) - Ord('0');

if v > 255 then Exit;

ipx[i] := v;

end;

Result := Longint(ipx);

end;

function HostToLong(AHost: string): Longint;

var

Host: PHost;

begin

Result := IpToLong(AHost);

if Result = 0 then

begin

Host := GetHostByName(PChar(AHost));

if Host <> nil then Result := Longint(Host^.Addr^^);

end;

end;

function LongToIp(Long: Longint): string;

var

ipx: array[0..3] of Byte;

i: Byte;

begin

Longint(ipx) := long;

Result := '';

for i := 0 to 3 do Result := Result + IntToStr(ipx[i]) + '.';

SetLength(Result, Length(Result) - 1);

end;

procedure Disconnect(Socket: Integer);

begin

ShutDown(Socket, 1);

CloseSocket(Socket);

end;

function CallServer(Server: string; Port: Word): Integer;

var

SockAddr: TSockAddr;

begin

Result := socket(Internet, Stream, 0);

if Result = -1 then Exit;

FillChar(SockAddr, SizeOf(SockAddr), 0);

SockAddr.Family := Internet;

SockAddr.Port := swap(Port);

SockAddr.Addr := HostToLong(Server);

if Connect(Result, SockAddr, SizeOf(SockAddr)) <> 0 then

begin

Disconnect(Result);

Result := -1;

end;

end;

function OutputSock(var F: TTextRec): Integer; far;

begin

if F.BufPos <> 0 then

begin

Send(F.Handle, F.BufPtr^, F.BufPos, 0);

F.BufPos := 0;

end;

Result := 0;

end;

function InputSock(var F: TTextRec): Integer; far;

var

Size: Longint;

begin

F.BufEnd := 0;

F.BufPos := 0;

Result := 0;

repeat

if (IoctlSocket(F.Handle, fIoNbRead, Size) < 0) then

begin

EofSock := True;

Exit;

end;

until (Size >= 0);

F.BufEnd := Recv(F.Handle, F.BufPtr, F.BufSize, 0);

EofSock := (F.Bufend = 0);

end;

function CloseSock(var F: TTextRec): Integer; far;

begin

Disconnect(F.Handle);

F.Handle := -1;

Result := 0;

end;

function OpenSock(var F: TTextRec): Integer; far;

begin

if F.Mode = fmInput then

begin

EofSock := False;

F.BufPos := 0;

F.BufEnd := 0;

F.InOutFunc := @InputSock;

F.FlushFunc := nil;

end

else

begin

F.Mode := fmOutput;

F.InOutFunc := @OutputSock;

F.FlushFunc := @OutputSock;

end;

F.CloseFunc := @CloseSock;

Result := 0;

end;

procedure AssignCrtSock(Socket:integer; Var Input,Output:TextFile);

begin

with TTextRec(Input) do

begin

Handle := Socket;

Mode := fmClosed;

BufSize := SizeOf(Buffer);

BufPtr := @Buffer;

OpenFunc := @OpenSock;

end;

with TTextRec(Output) do

begin

Handle := Socket;

Mode := fmClosed;

BufSize := SizeOf(Buffer);

BufPtr := @Buffer;

OpenFunc := @OpenSock;

end;

Reset(Input);

Rewrite(Output);

end;

function ConnectServer(mhost: string; mport: Integer): Integer;

var

tmp: string;

begin

mClient := TTcpClient.Create(nil);

mClient.RemoteHost := mhost;

mClient.RemotePort := IntToStr(mport);

mClient.Connect;

mconnhandle := callserver(mhost, mport);

if (mconnHandle<>-1) then

begin

AssignCrtSock(mconnHandle, mFin, MFout);

tmp := stat;

tmp := SendCommand('HELO bellona.com.tr');

if Copy(tmp, 1, 3) = '250' then

begin

Result := StrToInt(Copy(tmp, 1, 3));

end;

end;

end;

function ConnectServerWin(mhost: string; mport: Integer): Integer;

var

tmp: string;

begin

mClient := TTcpClient.Create(nil);

mClient.RemoteHost := mhost;

mClient.RemotePort := IntToStr(mport);

mClient.Connect;

mconnhandle := callserver(mhost, mport);

if (mconnHandle<>-1) then

begin

AssignCrtSock(mconnHandle, mFin, MFout);

tmp := stat;

tmp := SendCommandWin('HELO bellona.com.tr');

if Copy(tmp, 1, 3) = '250' then

begin

Result := StrToInt(Copy(tmp, 1, 3));

end;

end;

end;

function DisConnectServer: Integer;

begin

closesocket(mconnhandle);

mClient.Disconnect;

mclient.Free;

end;

function encryptB64(s: string): string;

var

hash1: TIdEncoderMIME;

p: string;

begin

if s <> '' then

begin

hash1 := TIdEncoderMIME.Create(nil);

p := hash1.Encode(s);

hash1.Free;

end;

Result := p;

end;

end.

//------------------------------------------

// 怎么使用定义好得相关单元

//---------------------------------------------

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Memo1: TMemo;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

uses

SMTP_Connections;

procedure TForm1.Button1Click(Sender: TObject);

var

mto, mbody: TStringList;

MailServer, uname, upass, mFrom, mFromName,

mToName, Subject: string;

begin

mMemo := Memo1; // 定义相关发送服务器

//..........................

MailServer := 'mail.163.com';

uname := 'username';

upass := 'password';

mFrom := 'laolij@163.com;

mFromName := 'forename surname';

mToName := '';

Subject := 'Your Subject';

//..........................

mto := TStringList.Create;

mbody := TStringList.Create;

try

mto.Add('anybody@xyz.net');

mbody.Add('测试邮件');

//发送.................

_authSendMail(MailServer, uname, upass, mFrom, mFromName, mToName, Subject, mto, mbody);

//..........................

finally

mto.Free;

mbody.Free;

end;

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有