Use TNMSMTP controls to send mail on the required authentication server

zhaozj2021-02-08  227

Preface:

Now many STMP servers need to re-authenticate when sending emails, and Delphi's TNMSMTP control is not very "visualization" support, so that many people have a big question mark during the development process.

Due to the time period, when you do "9CBS Query Assistant", it is also a server (163.com) that needs to be certified. Some of the code is resolved from other places, and now this is now shared.

achieve:

1. Add code to the NMSMTP on the onConnect event:

Var strusername, strpassword: = encodestring ('coolslob'); // coolslob is the server's account strpassword: = encodeString ('password'); // password is a password {authentication, enter the encoded username , Password} nmsmtp1.transaction ('ehlo'); nmsmtp1.transaction ('auth login'); nmsmtp1.transaction; nmsmtp1.transaction; statusbar1.simpletext: = 'connection success'; end;

2, EncodeString function implementation process:

Decoded parameters {Base64 encoded string, returns the encoded string} function EncodeString (Decoded: string): String; var mmTemp, mmDecoded: TMemoryStream; strTemp: TStrings; begin mmTemp: = TMemoryStream.Create; mmDecoded: = TMemoryStream .Create; strtemp: = tstringlist.create; strTemp.add (decoded); strTemp.savetostream (mmTemp); mmtemp.position: = 0; {MmTemp from strtemp characters # 13 # 10} mmdecoded.copyfrom MmTemp, mmtemp.size-2; {MMDecoded Base64 encoding (mmTemp); {Return Results must be obtained from Strtemp [0], if using strTemp.text will bring unnecessary characters # 13 # 10} result: = strTemp [0];

3, EncodeBase64 function implementation process:

function EncodeBASE64 (Encoded: TMemoryStream; Decoded: TMemoryStream): Integer; const _Code64: String [64] = ( 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /'); var I: LongInt; B: array [0..2279] of Byte; J, K, L, M, Quads: Integer; stream: string; begin encoded.clear; stream: = '; quads: = 0; {To improve efficiency, each 2280 byte stream is a set of groups } J: = decoded.size div 2280; decoded.position: = 0; {{{{2 2 2 2 字 字}} for i: = 1 to j Do Begin Decoded.read (B, 2280); for m : = 0 TO 39 Do Begin fork: = 0 to 18 DO Begin L: = 57 * m 3 * K; stream [quads 1]: = _CODE64 [(B [L] Div 4) 1]; stream [Quads 2]: = _CODE64 [(B [L] MOD 4) * 16 (B [L 1] Div 16) 1]; stream [quads 3]: = _CODE64 [(B [L 1 ] MOD 16) * 4 (B [L 2] DIV 64) 1]; stream [quads 4]: = _CODE64 [B [L 2] mod 64 1]; Inc (Quads, 4); IF quads = 76 THEN Begin Stream [0]: = # 76; Encline: = stream # 13 # 10; Encoded.write (Encline [1], Length (ENCLINE)); quadds: = 0; end; end; end;

{Coding the remaining digital throttle with 2280} J: = (decoded.size mod 2280) Div 3; for i: = 1 to j do begin decoded.read (b, 3); stream [quads 1 ]: = _CODE64 [(B [0] DIV 4) 1]; stream [quads 2]: = _CODE64 [(B [0] MOD 4) * 16 (B [1] Div 16) 1]; Stream [quads 3]: = _CODE64 [(B [1] MOD 16) * 4 (B [2] Div 64) 1]; stream [quads 4]: = _CODE64 [B [2] mod 64 1]; INC (Quads, 4); {76 characters per row} if quads = 76 The begin stream [0]: = # 76; Encline: = stream # 13 # 10; Encoded.write (Encline [1], Length (encline)); quads: = 0; end; end; {"=" 补 (decoded.size mod 3) = 2 Then Begin decoded.read (b, 2); stream [quads 1]: = _CODE64 [(B [0] DIV 4) 1]; stream [quads 2]: = _CODE64 [(B [0] MOD 4) * 16 (B [1] DIV 16) 1]; stream [ Quads 3]: = _CODE64 [(B [1] MOD 16) * 4 1]; stream [quads 4]: = '='; Inc (quads, 4); end; if (decoded.size mod 3 ) = 1 dam decoded.read (b, 1); str EAM [quads 1]: = _CODE64 [(B [0] Div 4) 1]; stream [quads 2]: = _CODE64 [(B [0] MOD 4) * 16 1]; stream [quads 3]: = '='; stream [quads 4]: = '='; inc (quads, 4);

Stream [0]: = chr (quads); if quads> 0 THEN BEGIN ENCLINE: = stream # 13 # 10; Encoded.write (Encline [1], Length (Encline)); END;

RESULT: = Encoded.Size;

statement:

The above code is not that I do, because I don't know if I have the original source, it is still as "original"!

转载请注明原文地址:https://www.9cbs.com/read-2799.html

New Post(0)