下面的是用Delphi實現郵件發送的代碼.因為加了部分異常處理,使代碼看起來反而有些煩瑣,其實去掉這部分還是很簡潔的.
procedure TForm1.Button1Click(Sender: TObject);
var
v_pos: integer;
v_addressStr, v_OneAddress,r_msg: string;
p_toaddress:string;
begin
p_toaddress := ;
NMSMTP2.ClearParameters;
NMSMTP2.Host := 'mailin.asustek.com.cn';
with NMSMTP2 do
begin
PostMessage.FromAddress := ;
PostMessage.FromName := Copy(, 1, Pos(, ) - 1);
if Pos(';', p_ToAddress) = 0 then
begin
PostMessage.ToAddress.Add(p_ToAddress);//只有一個收件人
end
else
begin //同時發送多個收件人,并以';'分隔收件人地址.
v_AddressStr := p_ToAddress;
while v_addressStr <> '' do
begin
v_Pos := Pos(';', v_AddressStr);
if v_Pos <> 0 then
begin
v_OneAddress := copy(v_AddressStr, 1, v_pos - 1);
v_AddressStr := Copy(v_AddressStr, v_pos + 1, Length(v_AddressStr) - Length(v_OneAddress));
end
else
begin
v_OneAddress := v_addressStr;
v_addressStr := '';
end;
PostMessage.ToAddress.Add(v_OneAddress);
end;
end;
Postmessage.Body.Add('測試結果 );
PostMessage.Subject := 'CDM File Error Information!';
PostMessage.ToCarbonCopy.Text := '類的,可以發送多個副本人.
PostMessage.ToBlindCarbonCopy.Text := 密件,同上.
PostMessage.Attachments.Text := ExtractFilePath(Application.ExeName) + '\SfisAbout.bmp'; //發送附件
end;
try
NMSMTP2.Connect;
if NMSMTP2.Connected then
begin
NMSMTP2.SendMail;
end
else
Exit;
except on e: exception do
begin
r_msg := r_msg + #13#10 + 'SendEmail Error: ' + e.Message;
exit;
end;
end;
if NMSmtp2.connected then
NMSMTP2.Disconnect;
ShowMessage('Send Mail OK!');