关于发送电子邮件需要身份验证的问题!(up有分)(100分)

春意

Unregistered / Unconfirmed
GUEST, unregistred user!
各位仁兄:
delphi 6.0中自带了IdSmtp组件以发送电子邮件,我看了Delphi 6.0程序demo中关
于电子邮件的例子,此例好像能收取邮件,但不能发送邮件,需要身份验证!
现请教:做个程序向163.com或者sohu.com、sina.com……等等这些邮箱发送邮件
成功的例子或方法(一定自己要试成功)…………
注:回答好的另加分,绝不失言!
 
如果你使用的idSmtp,设置AuthenticationType:=atLogin;然后设置userid,和password就可以了

如果你使用的是nmsmtp,就要麻烦很多。如下
var
AuthSucc:boolean;// 是否需要密码验证
const
BaseTable:string='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
function EncodeBase64(Source:string):string; //编码函数
function FindInTable(CSource:char):integer;

function FindInTable(CSource:char):integer;
begin
result:=Pos(string(CSource),BaseTable)-1;
end;
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 TWebModule1.NMSMTP1Connect(Sender: TObject);
begin
if nmsmtp1.ReplyNumber = 250 then
nmsmtp1.Transaction('auth login'); //开始认证
if nmsmtp1.ReplyNumber =334 then //返回值为334,让你输入用BASE64编码后的用户名
nmsmtp1.Transaction(EncodeBase64('shadow'));// 用户名aaaaa
if nmsmtp1.ReplyNumber =334 then // 返回值为334,让你输入用BASE64编码后的用户密码
nmsmtp1.Transaction(EncodeBase64('xujian79')); //密码为123456
if nmsmtp1.ReplyNumber =235 then
begin
AuthSucc:=true;
end;
end;
 
基本同意 lcl_003,
但是如果只是这样的话对有些邮件服务器如163.com依然不能成功发送,需要把IdMessage.pas中
下面一行注释掉,重新编译indy或者直接把IdMessage.pas包含在项目里
Values['X-Library'] := gsIdProductName + ' ' + gsIdVersion ; {do not localize}
我刚用BCB5+indy9。00。10试验过
如果需要源代码我可以贴出
 
谢谢sdzhch:
源代码发到我的信箱,我这儿有C++builder 5.0,我去试试!
如果成功,另加分相送!
本人信箱:jd_fmxx@163.com
 
请将代码贴出来好吗
 
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "UnitMain.h"
#include "UnitConfigure.h"
#include "UnitAccount.h"
#include "UnitGlobe.h"
#include <registry.hpp>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdMessage"
#pragma link "IdMessageClient"
#pragma link "IdPOP3"
#pragma link "IdSMTP"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpeedButtonAddClick(TObject *Sender)
{
bool NeedAdd = false;
if ( OpenDialog1->Execute())
{
AnsiString tmpName = OpenDialog1->FileName ;
for (int j= ListBox1->Items->Count -1 ; j>= 0 ; j--)
{
if (ListBox1->Items->Strings[j] == tmpName)
{
AnsiString hintName = tmpName + " 已经存在,要重新加入码?";
if (MessageBox(NULL, hintName.c_str(), "确 认", MB_OKCANCEL |MB_ICONWARNING)== IDOK )
{
ListBox1->Items->Add(tmpName);
}
NeedAdd = true;
break;
}
}
if (!NeedAdd)
ListBox1->Items->Add(tmpName);
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::SpeedButtonDelClick(TObject *Sender)
{
if (ListBox1->Items->Count<= 0 )
return;
for (int i = ListBox1->Items->Count -1; i>=0 ; i--)
{
if (ListBox1->Selected)
{
AnsiString tmpS = "确实要删除" + ListBox1->Items->Strings ;
if (MessageBox(NULL, tmpS.c_str(), "确 认", MB_OKCANCEL |MB_ICONWARNING)== IDOK )
{
ListBox1->Items->Delete(i);
}
}
} //for
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtnSendClick(TObject *Sender)
{
//读取配置文件
if ( FileExists(ExtractFilePath(ParamStr(0)) + "Mail.ini"))
{
MailSetupFile = new TIniFile(ExtractFilePath(ParamStr(0)) + "Mail.ini") ;
try
{
ReadSetup(MailSetupFile);
}
__finally
{
MailSetupFile->Free();
}
}

if (EditToAddress->Text == "")
{
MessageBox(NULL,"收件人没有填写,请正确填写收件人地址","警 告",MB_OK |MB_ICONWARNING);
return ;
}
TIdEMailAddressItem* AnItem;

TStringList *StrAddress;
StrAddress = new TStringList();
try
{
Devide_Colon_Address(EditToAddress->Text, *StrAddress);
for (int i = 0 ; i<StrAddress->Count; i++)
{
AnItem = IdMessage->Recipients->Add();
AnItem->Address = StrAddress->Strings;
}
if (EditToCC->Text !="")
{
StrAddress->Clear();
Devide_Colon_Address(EditToCC->Text , *StrAddress);
for (int i = 0 ; i < StrAddress->Count; i++)
{
AnItem = IdMessage->CCList->Add();
AnItem->Address = StrAddress->Strings;
}
} // if (EditToCC->Text !="")
if (EditToBCC->Text != "")
{
StrAddress->Clear();
Devide_Colon_Address(EditToBCC->Text , *StrAddress);
for (int i = 0 ; i < StrAddress->Count; i++)
{
AnItem = IdMessage->BccList->Add();
AnItem->Address = StrAddress->Strings;
}
} // if (EditToBCC->Text != "")
}
__finally
{
StrAddress->Free();
}

if (EditSubject->Text == "" )
{
if ( MessageBox(NULL,"主题是空的 是否要以空的主题发送","提 示", MB_OKCANCEL|MB_ICONQUESTION ) != IDOK )
return ;
}
// IdMessage->Body->Assign(MemoContent->Lines);
IdMessage->UseNowForDate = true;
IdMessage->From->Text = UserEMail;
IdMessage->ReplyTo->EMailAddresses = UserReplyEMail ;
IdMessage->ReceiptRecipient->Text = UserEMail;
IdMessage->Subject = EditSubject->Text ;
IdMessage->Recipients->EMailAddresses = EditToAddress->Text ;
//IdMessage->CCList->EMailAddresses = EditToCC->Text;
// IdMessage->BccList->EMailAddresses = EditToBCC->Text;
IdMessage->Priority = TIdMessagePriority(ComboBox1->ItemIndex) ;

Process_Body(IdMessage,MemoContent->Lines,ListBox1->Items );

IdSMTP->Host = SMTPSeverName ;
if (IdSMTP->Host=="")
{
MessageBox(NULL,"没有正确设置服务器主机,请设置","提 示", MB_OK|MB_ICONSTOP ) ;
return ;
}
IdSMTP->Port = SMTPSeverPort ;


if ( SMTPVerify )
IdSMTP->AuthenticationType = atLogin ;
else
IdSMTP->AuthenticationType = atNone ;

IdSMTP->Username = SMTPSeverUser ;
IdSMTP->Password = SMTPSeverPassword ;
IdSMTP->Connect(IdTimeoutDefault);
try
{
IdSMTP->Send(IdMessage);
}
__finally
{
IdSMTP->Disconnect();
}

}
//---------------------------------------------------------------------------

void __fastcall TFormMain::IdSMTPConnected(TObject *Sender)
{
StatusBar1->SimpleText = "状态:已经成功连接服务器主机";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::IdSMTPWork(TObject *Sender, TWorkMode AWorkMode,
const int AWorkCount)
{
StatusBar1->SimpleText = "状态:正在发送邮件";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtnSetupClick(TObject *Sender)
{
FormConfigure = new TFormConfigure(this) ;
FormConfigure->ShowModal();
FormConfigure->Free();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Button1Click(TObject *Sender)
{
TStringList *SS;
SS = new TStringList();
try
{
Devide_Colon_Address(EditToAddress->Text, *SS);
for (int Count = 0 ; Count < SS->Count; Count++)
{
MemoContent->Lines->Add(SS->Strings[Count]);
}
}
__finally
{
SS->Free();
}
Process_Body(IdMessage,MemoContent->Lines,ListBox1->Items );
}
//---------------------------------------------------------------------------
bool __fastcall TFormMain::Ext_to_MIME(String Extension, String&amp; MIME_String)
{
TRegistry *SysReg;
bool Result = false;
SysReg = new TRegistry();
try
{
SysReg->RootKey = HKEY_CLASSES_ROOT;
AnsiString tmpstr = "//"+ Extension ;
if ( SysReg->KeyExists(tmpstr))
{
SysReg->OpenKey(tmpstr, false);
if (SysReg->ValueExists("Content Type"))
{
MIME_String = SysReg->ReadString("Content Type");
Result = true;
}
else
Result = false;
SysReg->CloseKey();
}
else
Result = false;
}
__finally
{
SysReg->Free();
}
return Result;
}
//----------------------------------------------------------------------------
bool __fastcall TFormMain::process_Body(TIdMessage* Message , TStrings* Content,
TStrings* Attchment)
{
TIdText* Msg_Text ;
TIdAttachment* Msg_File;
String TheFile, Ext, MIME ;
Msg_Text = new TIdText(Message->MessageParts,Content);
Msg_Text->ContentType = "text/plain";
Msg_Text = new TIdText(Message->MessageParts,Content);
Msg_Text->ContentType = "text/plain";
if (Attchment->Count >0 )
{
for (int index= 0; index < Attchment->Count; index++ )
{
TheFile = Attchment->Strings[index];
Ext = ExtractFileExt(TheFile);
bool Res = Ext_to_MIME(Ext, MIME);
Msg_File = new TIdAttachment(Message->MessageParts, TheFile);
Msg_File->ExtraHeaders->Values["Content-Location"] = TheFile;
if ( Res )
Msg_File->ContentType = MIME;
}
}
return true;
}
//----------------------------------------------------------------------------
bool __fastcall TFormMain::ReadSetup(TIniFile* IniFile)
{
SMTPSeverName = IniFile->ReadString("Servers" ,"SMTP Server Name" ,"");
SMTPSeverPort = IniFile->ReadInteger("Servers","SMTP Server Port", 25);
SMTPVerify = IniFile->ReadBool("Servers", "SMTP Verify",false);
SMTPSeverUser = IniFile->ReadString("Servers", "SMTP User Account","");
SMTPSeverPassword = IniFile->ReadString("Servers", "SMTP User Password","");
POP3SeverUser = IniFile->ReadString("Sevrers","POP3 User Account" ,"");
POP3SeverPassword = IniFile->ReadString("Sevrers","User Password" ,"");
UserEMail = IniFile->ReadString("Persons","EMail" ,"");
UserReplyEMail = IniFile->ReadString("Persons","Reply EMail" ,"");
}
//----------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
if ( FileExists(ExtractFilePath(ParamStr(0)) + "Mail.ini"))
{
MailSetupFile = new TIniFile(ExtractFilePath(ParamStr(0)) + "Mail.ini") ;
try
{
ReadSetup(MailSetupFile);
}
__finally
{
MailSetupFile->Free();
}
}
ComboBox1->ItemIndex = (int)IdMessage->Priority;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::IdSMTPStatus(TObject *ASender,
const TIdStatus AStatus, const AnsiString AStatusText)
{
StatusBar1->SimpleText = "状态:" + AStatusText ;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ClearClick(TObject *Sender)
{
MemoContent->Clear();
}
//---------------------------------------------------------------------------
 
非常感谢:sdzhch贴出源代码!同时也感谢lcl_003,虽然这个代码我以前也看过,试了一下没
有成功,不过还是非常感谢!
我打算另开贴加分,请在12.16日中午13:00上线!
 
多人接受答案了。
 
顶部