给你一段代码,看着改吧
procedure TForm1.M_sendmail_byoutlook(M_filename: string);
//参数为附件路径
TYPE
TAttachAccessArray = ARRAY [0..0] OF TMapiFileDesc;
PAttachAccessArray = ^TAttachAccessArray;
VAR
MapiMessage : TMapiMessage;
Receip : TMapiRecipDesc;
Attachments : PAttachAccessArray;
AttachCount : INTEGER;
iCount : INTEGER;
FileName : STRING;
Mail : TStringlist;
begin
Mail:= Tstringlist.Create;
mail.values['to'] := '';
mail.values['subject'] := '';
mail.values['body'] := '';
mail.values['attachment0'] := M_filename;//附件路径要存在
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']));
//MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle <> 0) OR MAPI_LOGON_UI OR MAPI_NEW_SESSION, 0);
MapiSendMail(0, Application.Handle, MapiMessage,
MAPI_DIALOG 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);
*)
//MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle <> 0) OR MAPI_LOGON_UI OR MAPI_NEW_SESSION, 0);
mail.Free;
end;