个不同数据类型的部分,各个部分之间用"---SwfEmail by JDH"(不包括引号)区分。在邮件内容的这些部分中,根据每部分的内容不同,再为其添加相应的MIME消息头,具体可参见
程序源代码。
二、编程实战
启动Delphi 5,参考图1在Form1上创建各种控件。图中黑色方框内标明了控件名称,其中txt前缀表示TEdit控件,mem前缀表示TMemo控件,chk前缀表示TCheckbox控件,btn前缀表示TButton控件,NMSMTP1是TNMSMTP控件,OpenDialog1是TOpenDialog控件。图2列出了一部分控件的某些关键属性。
现在添加代码如下:
{******Unit1.pas源代码内容如下******}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Psock, NMsmtp;
type
TForm1 = class(TForm)
Label1: TLabel;
txtTo: TEdit;
Label2: TLabel;
txtFrom: TEdit;
Label3: TLabel;
txtSubject: TEdit;
Label4: TLabel;
memContents: TMemo;
Label5: TLabel;
txtUserName: TEdit;
Label6: TLabel;
txtPassword: TEdit;
chkSmtpVerify: TCheckBox;
btnSend: TButton;
btnOpen: TButton;
txtSwfFile: TEdit;
Label7: TLabel;
OpenDialog1: TOpenDialog;
Label8: TLabel;
txtSmtpServer: TEdit;
NMSMTP1: TNMSMTP;
Label9: TLabel;
txtPort: TEdit;
procedure btnOpenClick(Sender: TObject);
procedure btnSendClick(Sender: TObject);
procedure NMSMTP1SendStart(Sender: TObject);
procedure NMSMTP1Connect(Sender: TObject);
procedure chkSmtpVerifyClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function EncodeString(Decoded:string):String;
function EncodeBASE64(Encoded: TMemoryStream {TMailText}; Decoded: TMemoryStream):
Integer; //编码函数
implementation
{$R *.dfm}
{对参数TMemoryStrema中的字节流进行Base64编码,编码后的结果
保存在Encoded中,函数返回编码长度}
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[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);
if Quads = 76 then
begin
Stream[0] := #76;
EncLine := Stream+#13#10;
Encoded.Write(EncLine, 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 div 16)+1];
Stream[Quads+3] := _Code64[(B mod 16)*4 + (B div 64)+1];
Stream[Quads+4] := _Code64[B mod 64+1];
Inc(Quads, 4);
{每行76个字符}
if Quads = 76 then
begin
Stream[0] := #76;
EncLine := Stream+#13#10;
Encoded.Write(EncLine, Length(EncLine));
Quads := 0;
end;
end;
{“=”