如何實現在delphi.6中發送Email ( 积分: 100 )

  • 主题发起人 主题发起人 kitty_li
  • 开始时间 开始时间
K

kitty_li

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好!
感謝您在原百忙中看我的問題,我想請教您,如何實現在delphi.6中發送Email?
如果用到ini 文件,其代碼如果編程?
非常感謝!
 
大家好!
感謝您在原百忙中看我的問題,我想請教您,如何實現在delphi.6中發送Email?
如果用到ini 文件,其代碼如果編程?
非常感謝!
 
Api函数如下
ShellExecute(0,nil,PChar(cEmail),nil,nil,sw_ShowNormal);
 
Indy中有Demos
 
FUNCTION SendEMail(Handle : THandle;
Mail : TStrings):Cardinal;
TYPE
TAttachAccessArray = ARRAY [0..0] OF TMapiFileDesc;
PAttachAccessArray = ^TAttachAccessArray;
VAR
MapiMessage : TMapiMessage;
Receip : TMapiRecipDesc;
Attachments : PAttachAccessArray;
AttachCount : INTEGER;
iCount : INTEGER;
FileName : STRING;
begin
fillChar(MapiMessage, SizeOf(MapiMessage), #0);
Attachments := NIL;
fillChar(Receip,SizeOf(Receip), #0);
IF Mail.Values['to'] <> ''
then
begin
Receip.ulReserved := 0;
Receip.ulRecipClass := MAPI_TO;
Receip.lpszName := StrNew(PChar(Mail.Values['to']));
Receip.lpszAddress := StrNew(PChar('SMTP:' + Mail.Values['to']));
Receip.ulEIDSize := 0;
MapiMessage.nRecipCount := 1;
MapiMessage.lpRecips := @Receip;
end;

AttachCount := 0;
FOR iCount := 0 TO MaxInt
do

begin
IF Mail.Values['attachment' + IntToStr(iCount)] = ''
then

BREAK;
AttachCount := AttachCount + 1;
end;

IF AttachCount > 0
then
begin

GetMem(Attachments,SizeOf(TMapiFileDesc) * AttachCount);
FOR iCount := 0 TO (AttachCount - 1)
do
begin

FileName := Mail.Values['attachment' + IntToStr(iCount)];
Attachments[iCount].ulReserved := 0;
Attachments[iCount].flFlags := 0;
Attachments[iCount].nPosition := ULONG($FFFFFFFF);
Attachments[iCount].lpszPathName := StrNew(PChar(FileName));
Attachments[iCount].lpszFileName := StrNew(PChar(ExtractFileName(FileName)));
Attachments[iCount].lpFileType := NIL;
end;

MapiMessage.nFileCount := AttachCount;
MapiMessage.lpFiles := @Attachments^;
end;

IF Mail.Values['subject'] <> ''
then

MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject']));
IF Mail.Values['body'] <> ''
then

MapiMessage.lpszNoteText := StrNew(PChar(Mail.Values['body']));

Result := MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle <> 0) OR MAPI_LOGON_UI OR MAPI_NEW_SESSION, 0);

FOR iCount := 0 TO (AttachCount - 1)
do
begin

strDispose(Attachments[iCount].lpszPathName);
strDispose(Attachments[iCount].lpszFileName);
end;

IF assigned(MapiMessage.lpszSubject)
then

strDispose(MapiMessage.lpszSubject);
IF assigned(MapiMessage.lpszNoteText)
then

strDispose(MapiMessage.lpszNoteText);
IF assigned(Receip.lpszAddress)
then

strDispose(Receip.lpszAddress);
IF assigned(Receip.lpszName)
then
strDispose(Receip.lpszName);
end;


procedure TForm1.Button1Click(Sender: TObject);
VAR
mail : TStringList ;
begin
mail := TStringList.Create;
mail.values['to'] := 'delphi2000@8848.net';
mail.values['subject'] := 'A subject';
mail.values['body'] := 'Some body text (line 1)';
mail.values['body'] := 'Some more body text (line 2)';
mail.values['attachment0'] := 'f:/a.txt';//附件路?要存在
sendEMail(Application.Handle, mail);
mail.Free;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
MyOutLook:variant;
Item:variant;
FilePathandName:string;
begin
{try
MyOutlook := GetActiveOleObject('Outlook.Application');
except
MyOutLook:=CreateOleObject('Outlook.Application');
end;

item:=MyOutlook.CreateItem(0);
item.recipients.add(EmailSite);
item.subject:=EmailSubject;
// item.body:='hello';
item.Attachments.add('c:/' + EmailAttachment,1,1,EmailAttachment);
item.display(1);
end;
}
end;
里面有二套發送email的方法﹐
 
Simple send email through outlook
Maybe it is better to read the addresses (SMTP / POP servers addresses and user email address)
from the default mail client's parameters in the registery (I think Outlook has it in the registry) and then
simply use the standard delphi NMSMTP component
如果是這們的話,具體的代碼如果寫呢?
 
是写客户端还是调用客户端软件,如果是写的话就用INDY套件吧,调用则使用ShellExecute函数。
 
后退
顶部