发送邮件和接收邮件的服务器 使用TNMSMTP控件在需认证服务器上发送邮件
使用TNMSMTP控件在需认证服务器上发送邮件
前言 现在很多STMP服务器在发送邮件时均需重新认证一遍 而Delphi的TNMSMTP控件对它没有很 可视化 的支持 使很多人在开发过程中大打问号
由于前段时间在做《CSDN查询助手》的时候 使用的也是需认证的服务器( ) 从其它地方摘取了部分代码得以解决 现在此发布与大家共享
实现
在NMSMTP的OnConnect事件中添加代码
var strUserName strPassword: String;begin strUserName := EncodeString( CoolSlob );//CoolSlob是服务器的帐号 strPassword := EncodeString( Password );//Password是密码 {进行认证 输入编码后的用户名 密码} nmsmtp Transaction( EHLO ) ; nmsmtp Transaction( AUTH LOGIN ); nmsmtp Transaction(strUserName); nmsmtp Transaction(strPassword); StatusBar SimpleText := 连接成功 ;end;
EncodeString函数实现过程
{对参数Decoded字符串进行Base 编码 返回编码后的字符串}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 := ; {剔除mmTemp从strTemp中带来的字符# # } mmDecoded CopyFrom(mmTemp mmTemp Size ); {对mmDecoded进行Base 编码 由mmTemp返回编码后的结果} EncodeBASE (mmTemp mmDecoded); {获得Base 编码后的字符串} mmTemp Position:= ; strTemp LoadFromStream(mmTemp); {返回结果必须从strTemp[ ]中获得 如果使用strTemp Text会 带来不必要的字符# # } Result:=strTemp[ ];end;
EncodeBASE 函数实现过程

function EncodeBASE (Encoded: TMemoryStream ; Decoded: TMemoryStream): Integer;const _Code : String[ ] = ( ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz +/ );var I: LongInt; B: array[ ] of Byte; J K L M Quads: Integer; Stream: string[ ]; EncLine: String;begin Encoded Clear; Stream := ; Quads := ; {为提高效率 每 字节流为一组进行编码} J := Decoded Size div ; Decoded Position := ; {对前J* 个字节流进行编码} for I := to J do begin Decoded Read(B ); for M := to do begin for K := to do begin L:= *M + *K; Stream[Quads+ ] := _Code [(B[L] div )+ ]; Stream[Quads+ ] := _Code [(B[L] mod )* + (B[L+ ] div )+ ]; Stream[Quads+ ] := _Code [(B[L+ ] mod )* + (B[L+ ] div )+ ]; Stream[Quads+ ] := _Code [B[L+ ] mod + ]; Inc(Quads ); if Quads = then begin Stream[ ] := # ; EncLine := Stream+# # ; Encoded Write(EncLine[ ] Length(EncLine)); Quads := ; end; end; end; end;
{对以 为模的余数字节流进行编码} J := (Decoded Size mod ) div ; for I := to J do begin Decoded Read(B ); Stream[Quads+ ] := _Code [(B[ ] div )+ ]; Stream[Quads+ ] := _Code [(B[ ] mod )* + (B[ ] div )+ ]; Stream[Quads+ ] := _Code [(B[ ] mod )* + (B[ ] div )+ ]; Stream[Quads+ ] := _Code [B[ ] mod + ]; Inc(Quads ); {每行 个字符} if Quads = then begin Stream[ ] := # ; EncLine := Stream+# # ; Encoded Write(EncLine[ ] Length(EncLine)); Quads := ; end; end; { = 补位} if (Decoded Size mod ) = then begin Decoded Read(B ); Stream[Quads+ ] := _Code [(B[ ] div )+ ]; Stream[Quads+ ] := _Code [(B[ ] mod )* + (B[ ] div )+ ]; Stream[Quads+ ] := _Code [(B[ ] mod )* + ]; Stream[Quads+ ] := = ; Inc(Quads ); end;
if (Decoded Size mod ) = then begin Decoded Read(B ); Stream[Quads+ ] := _Code [(B[ ] div )+ ]; Stream[Quads+ ] := _Code [(B[ ] mod )* + ]; Stream[Quads+ ] := = ; Stream[Quads+ ] := = ; Inc(Quads ); end;
Stream[ ] := Chr(Quads); if Quads > then begin EncLine := Stream+# # ; Encoded Write(EncLine[ ] Length(EncLine)); end;
lishixinzhi/Article/program/Delphi/201311/25054