请问各位大侠,使用nmsmtp控件时,有效性验证问题如何解决(100分)

  • 主题发起人 主题发起人 laigang666
  • 开始时间 开始时间
L

laigang666

Unregistered / Unconfirmed
GUEST, unregistred user!
使用nmsmtp控件时,老是提示需要认证,不知道问题出在什么地方,请各位大侠帮帮忙!
 
没人乐意回答这个问题么
 
你是说用户名和密码认证吗,nmstmp没有自带的验证,建议使用IdSMTP,其中有userid和password属性,可惜我不知道他的附件功能怎么实现,至今还是遗憾哪!
 
请用idmessage
idsmtp
delphi的demo中有
 
用nmsmtp组件不行么
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Button1: TButton;
IdSMTP1: TIdSMTP;
IdMessage1: TIdMessage;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Edit9: TEdit;
Label9: TLabel;
ListView1: TListView;
Button2: TButton;
OpenDialog1: TOpenDialog;
function FindAttachment(stFilename: string): integer;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure ResetAttachmentListView;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
SmtpAuthType: Integer;
UserEmail: string;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin



useremail:=edit9.text;
idmessage1.Body.Assign(Memo1.Lines);
idmessage1.From.Text := UserEmail;
idmessage1.Recipients.EMailAddresses := edit1.Text; { To: header }
idmessage1.Subject := edit2.Text; { Subject: header }
idmessage1.CCList.EMailAddresses := edit3.Text; {CC}
idmessage1.BccList.EMailAddresses := edit4.Text; {BBC}
idsmtp1.Host:=edit5.Text;
idsmtp1.UserId:=edit6.Text;
idsmtp1.Password:=edit7.Text;
idsmtp1.Port:=strtoint(edit8.Text);
idsmtp1.Connect;
{now we send the message}
idsmtp1.Send(idmessage1);
// idsmtp1.Disconnect;
end;

function TForm1.FindAttachment(stFilename: string): integer;
var
intIndex: Integer;
found: boolean;
begin
intIndex := -1;
result := -1;
if (idmessage1.MessageParts.Count < 1) then
exit; //no attachments (or anything else)
found := false;
stFilename := uppercase(stFilename);
repeat
inc(intIndex);
if (idmessage1.MessageParts.Items[intIndex] is TIdAttachment) then
begin //general attachment
if stFilename =
uppercase(TIdAttachment(idmessage1.MessageParts.Items[intIndex]).Filename) then
found := true;
end;
until found or (intIndex > Pred(idmessage1.MessageParts.Count));
if found then
result := intIndex
else
result := -1;
end; (* *)


procedure TForm1.ResetAttachmentListView;
var
li: TListItem;
idx: Integer;
begin
listview1.Items.Clear;
for idx := 0 to Pred(Idmessage1.MessageParts.Count) do
begin
li := listview1.Items.Add;
if IdMessage1.MessageParts.Items[idx] is TIdAttachment then
begin
li.ImageIndex := 0;
li.Caption := TIdAttachment(IdMessage1.MessageParts.Items[idx]).Filename;
li.SubItems.Add(TIdAttachment(Idmessage1.MessageParts.Items[idx]).ContentType);
end
else
begin
li.ImageIndex := 1;
li.Caption := Idmessage1.MessageParts.Items[idx].ContentType;
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
TIdAttachment.Create(Idmessage1.MessageParts, OpenDialog1.FileName);
ResetAttachmentListView;
end;

end;

end.
随便写的,我在自己的mail服务器上试过可以用,也可以发附件,但是广域网上,可以发,,不知道怎么收不到,你自己改改吧 。备注(参考demo)。
 
//在连接时使用命令进行验证
//EncodeBASE64函数实现过程:
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[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个字符}
if Quads = 76 then
begin
Stream[0] := #76;
EncLine := Stream+#13#10;
Encoded.Write(EncLine[1], Length(EncLine));
Quads := 0;
end;
end;
{“=”补位}
if (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 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);
if Quads > 0 then
begin
EncLine := Stream+#13#10;
Encoded.Write(EncLine[1], Length(EncLine));
end;

Result := Encoded.Size;
end;
{对参数Decoded字符串进行Base64编码,返回编码后的字符串}
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从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;


procedure TEMailSender.DoOnConnect(Sender: TObject);
var
strUserName, strPassword: String;
begin
if Sender is TNMSMTP then
with TNMSMTP(Sender) do
begin
strUserName := EncodeString(FUserID); //服务器的帐号
strPassword := EncodeString(FPassword); //服务器的帐号密码
{进行认证,输入编码后的用户名、密码}
Transaction('HELO');
Transaction('AUTH LOGIN');
Transaction(strUserName);
Transaction(strPassword);
end;
end;
 
谢谢,用idsmtp和idmessage 已经解决问题,同时问一下,可不可以在程序中设置邮件的优先级?
 
再请问高手,用VIP邮箱时,怎么会提示'connected close gracefully'.盼速解决.
 
后退
顶部