本人写了一个电子邮件发送程序,可是,老是出现错误,求高手帮忙解决:
procedure TForm1.Button1Click(Sender: TObject);
begin
with IdMessage1 do
begin
Body.Text:=Memo1.Text;
From.Address:='[email protected]';
Recipients.EMailAddresses:='[email protected]';
Subject:='实验邮件!';
end;
with IdSmtp1 do
begin
Host:='smtp.163.com';
Username:='[email protected]';
Password:='********'; //这里是我的密码
Connect(-1);
Send(Idmessage1);
Disconnect;
end;
ShowMessage('发送完毕!');
end;
可是却出现了
You are not authorized to send mail as <MAIL FROM:<[email protected]>>,authentication is required
的错误,请问,这个问题该如何解决阿 ?
是不是在IdMessage1中也要加什么密码的阿?
要是要加,那又该如何加呢?
我没有看到什么
IdMessage1.Password什么的,请高手指教!!
procedure TForm1.Button1Click(Sender: TObject);
begin
with IdMessage1 do
begin
Body.Text:=Memo1.Text;
From.Address:='[email protected]';
Recipients.EMailAddresses:='[email protected]';
Subject:='实验邮件!';
end;
with IdSmtp1 do
begin
Host:='smtp.163.com';
Username:='[email protected]';
Password:='********'; //这里是我的密码
Connect(-1);
Send(Idmessage1);
Disconnect;
end;
ShowMessage('发送完毕!');
end;
可是却出现了
You are not authorized to send mail as <MAIL FROM:<[email protected]>>,authentication is required
的错误,请问,这个问题该如何解决阿 ?
是不是在IdMessage1中也要加什么密码的阿?
要是要加,那又该如何加呢?
我没有看到什么
IdMessage1.Password什么的,请高手指教!!
可是在验证的时候if self.IdSMTP.Authenticate then 总是出错,
能否把你的贴出来,让我们学习一下呢?现在用NMSMTP完成了一个带身份验证的,不过还有bug,对于企业邮箱不能带附件:
function EncodeBase64(Source: string): string;
var
Times, LenSrc, i: Integer;
x1, x2, x3, x4: Char;
xt: Byte;
begin
Result := '';
LenSrc := Length(Source);
if LenSrc mod 3 = 0 then
Times := LenSrc div 3
else
Times := LenSrc div 3 + 1;
for i := 0 to Times - 1 do
begin
if LenSrc >= (3 + i * 3) then
begin
x1 := BaseTable[(ord(Source[1 + i * 3]) shr 2)+1];
xt := (ord(Source[1 + i * 3]) shl 4) and 48;
xt := xt or (ord(Source[2 + i * 3]) shr 4);
x2 := BaseTable[xt + 1];
xt := (Ord(Source[2 + i * 3]) shl 2) and 60;
xt := xt or (Ord(Source[3 + i * 3]) shr 6);
x3 := BaseTable[xt + 1];
xt := (ord(Source[3 + i * 3]) and 63);
x4 := BaseTable[xt + 1];
end
else if LenSrc >= (2 + i * 3) then
begin
x1 := BaseTable[(Ord(Source[1 + i * 3]) shr 2) + 1];
xt := (Ord(Source[1 + i * 3]) shl 4) and 48;
xt := xt or (Ord(Source[2 + i * 3]) shr 4);
x2 := BaseTable[xt + 1];
xt := (Ord(Source[2 + i * 3]) shl 2) and 60;
x3 := BaseTable[xt + 1];
x4 := '=';
end else
begin
x1 := BaseTable[(Ord(Source[1 + i * 3]) shr 2)+1];
xt := (Ord(Source[1 + i * 3]) shl 4) and 48;
x2 := BaseTable[xt + 1];
x3 := '=';
x4 := '=';
end;
Result := Result + x1 + x2 + x3 + x4;
end;
end;procedure TForm1.NMSMTPConnect(Sender: TObject);
var
AuthOK: Boolean;
begin
AuthOK := False;
NMSMTP.Transaction('auth login');
if NMSMTP.ReplyNumber <> 500 then
begin
if NMSMTP.ReplyNumber = 334 then
begin
NMSMTP.Transaction(EncodeBase64('myUseName')); // UserName
NMSMTP.Transaction(EncodeBase64('****')); // Password
end;
if NMSMTP.ReplyNumber = 235 then
self.Memo2.Lines.Add('验证成功!')
else
self.Memo2.Lines.Add('验证失败!');
end
else
self.Memo2.Lines.Add('该SMTP服务器无需用户验证。'); try
self.NMSMTP.PostMessage.FromAddress := '[email protected]';
self.NMSMTP.PostMessage.FromName := 'myUseName';
self.NMSMTP.PostMessage.Subject := 'test';
self.NMSMTP.PostMessage.ToAddress.Add(self.Edit1.Text);
self.NMSMTP.PostMessage.Attachments.AddStrings(self.lstbxAdditional.Items);
self.NMSMTP.PostMessage.Body.Assign(self.memo1.Lines); self.NMSMTP.SendMail;
finally
self.NMSMTP.Disconnect;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
self.Memo2.Lines.Add('--使用NMSMTP发送邮件开始--');
self.NMSMTP.ClearParameters;
self.NMSMTP.UserID := 'myUseName';
self.NMSMTP.Port := 25;
self.NMSMTP.Host := 'smtp.126.com'; self.NMSMTP.Connect;
end;该程序不能发送带附件的邮件到企业邮。
begin
Clear;
From.Address := CboSender.Text;
// From.Name := edit1.text; //自動產生成 name<e_mail>
Recipients.EMailAddresses := edtRecipient.Text; {收件人}
Priority := TIdMessagePriority(2); // 0-4, 0表示最高優先順序
CCList.EMailAddresses := edtCopyAdd.Text; {CC}
BccList.EMailAddresses := edtConfidential.Text; {BBC}
ReceiptRecipient.Text := From.Text; //要寄件回涵
Subject := edtSubject.Text; { Subject: header } IdMessage.MessageParts.Clear;
Tidtext.Create(IdMessage.MessageParts);
with Tidtext.Create(IdMessage.MessageParts, mmoMessageText.Lines) do
begin
ContentType := 'text/html';
end;
for i := 0 to Listbox1.Items.Count - 1 do
TIdAttachment.Create(IdMessage.MessageParts, Listbox1.Items[i])
end;
with IdSMTP do
begin
(FDmdMant as TDmdOAMail).QrySmtpServer.Active := False;
(FDmdMant as TDmdOAMail).QrySmtpServer.ParamByName('RecAdd').AsString := CboSender.Text;
(FDmdMant as TDmdOAMail).QrySmtpServer.Active := True;
Host := (FDmdMant as TDmdOAMail).QrySmtpServer.FieldByName('SMTPServer').AsString;
UserName := (FDmdMant as TDmdOAMail).QrySmtpServer.FieldByName('AccountName').AsString; //請注意indy81以上為username
Password := (FDmdMant as TDmdOAMail).QrySmtpServer.FieldByName('Password').AsString;
try
sbMain.SimpleText := '連線測試中...請稍候';
application.ProcessMessages;
idsmtp.Connect;
sbMain.SimpleText := '連線測試成功!';
send(IdMessage);
application.ProcessMessages; sbMain.SimpleText := '邮件已发送成功!'; idsmtp.Disconnect;
btnSendMail.Enabled := false; //正確後將按鈕disabled,提供下一頁按鈕的判斷,可以避免重複測試
begin
F_sendmail.idSMTP.Username := vSmtpid;
F_sendmail.idSMTP.Password :=vpassword;
F_sendmail.idSMTP.Host :=vSmtphost;
F_sendmail.idSMTP.Port := vSmtpPort;
Body.Assign(F_sendmail.Memcontent.Lines);
From.Text :=vmail;
Subject := F_sendmail.etitle.Text; { Subject: header }
// F_sendmail.idSMTP.AuthenticationType := atLogin; {Simple Login}
F_sendmail.IdSMTP.AuthenticationType := atNone; F_sendmail.IdSMTP.Connect; try
if
F_sendmail.IdSMTP.AuthSchemesSupported.IndexOf('LOGIN')>-1 then
begin
F_sendmail.IdSMTP.AuthenticationType := atLogin;
if not F_sendmail.IdSMTP.Authenticate then
begin
showmessage('身份验证失败!!!!');
F_sendmail.IdSMTP.Disconnect;
exit;
end;
end;
F_sendmail.idmsg.Recipients.EMailAddresses:=listbox2.Items[i];
F_sendmail.idSMTP.Send(F_sendmail.IdMsg);
end;