专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Delphi教程 » asp.net服务器控件:使用TNMSMTPControl控件在需认证服务器上发送邮件 »正文

asp.net服务器控件:使用TNMSMTPControl控件在需认证服务器上发送邮件

来源: 发布时间:星期五, 2008年12月26日 浏览:75次 评论:0
前言:

  现在很多STMP服务器在发送邮件时均需重新认证而DelphiTNMSMTPControl控件对它没有很“可视化”支持使很多人在开发过程中大打问号

  由于前段时间在做CSDN查询助手时候使用也是需认证服务器(163.com)从其它地方摘取了部分代码得以解决现在此发布和大家共享

实现:

1、在NMSMTPOnConnect事件中添加代码:

var  strUserName, strPassword: String;
begin
  strUserName := EncodeString('CoolSlob');//CoolSlob是服务器帐号
  strPassword := EncodeString('Password');//Password是密码
  {进行认证输入编码后用户名、密码}
  nmsmtp1.Transaction('EHLO') ;
  nmsmtp1.Transaction('AUTH LOGIN');
  nmsmtp1.Transaction(strUserName);
  nmsmtp1.Transaction(strPassword);
  StatusBar1.SimpleText := '连接成功';
end;

2、EncodeString实现过程:

{对参数Decoded串进行Base64编码返回编码后串}
function EncodeString(Decoded:):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从strTemp中带来#13#10}
    mmDecoded.CopyFrom(mmTemp,mmTemp.Size-2);
    {对mmDecoded进行Base64编码由mmTemp返回编码后结果}
    EncodeBASE64(mmTemp,mmDecoded);
    {获得Base64编码后串}
    mmTemp.Position:=0;
    strTemp.LoadFromStream(mmTemp);
    {返回结果必须从strTemp[0]中获得如果使用strTemp.Text会
    带来不必要#13#10}
    Result:=strTemp[0];
end;

3、EncodeBASE64实现过程:

function EncodeBASE64(Encoded: TMemoryStream ; Decoded: TMemoryStream): Integer;
const
    _Code64: String[64] =
        ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
var
    I: LongInt;
    B: .gif' />[0..2279] of Byte;
    J, K, L, M, Quads: Integer;
    Stream: [76];
    EncLine: String;
begin
    Encoded.Clear;
    Stream := '';
    Quads := 0;
    {为提高效率每2280字节流为组进行编码}
    J := Decoded.Size div 2280;
    Decoded.Position := 0;
    {对前J*2280个字节流进行编码}
    for I := 1 to J do
    begin
        Decoded.Read(B, 2280);
        for M := 0 to 39 do
        begin
            for K := 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);
                 Quads = 76 then
                begin
                    Stream[0] := #76;
                    EncLine := Stream+#13#10;
                    Encoded.Write(EncLine[1], Length(EncLine));
                    Quads := 0;
                end;
            end;
        end;
    end;

    {对以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个}
         Quads = 76 then
        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;

     (Decoded.Size mod 3) = 1 then
    begin
        Decoded.Read(B, 1);
        Stream[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);
    end;

    Stream[0] := Chr(Quads);
     Quads > 0 then
    begin
        EncLine := Stream+#13#10;
        Encoded.Write(EncLine[1], Length(EncLine));
    end;

    Result := Encoded.Size;
end;
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: