附件中是MSMAPI32.OCX的License文件,复制到系统目录,然后导入REG文件即可。
利用MAPI COM来发送邮件,这下支持附件了!
利用MAPI COM对象来法送邮件
调用方法: SendMail("Kingron@163.net")
翻译成Delphi的也可以阿,这样支持附件了。:),ShellExecute不能支持附件,如果没有Outlook只有Express的话,现在可以让Outlook Express也支持附件了。
Public Sub SendMail(Address)
Dim MS, MM
On Error Resume Next
Set MS = CreateObject("MSMAPI.MAPISession")
Set MM = CreateObject("MSMAPI.MAPIMessages")
MS.DownLoadMail = False
MS.NewSession = False
MS.LogonUI = True
MS.SignOn
MM.SessionID = MS.SessionID
MM.Compose
MM.RecipIndex = 0
MM.RecipAddress = Address
MM.MsgSubject = ActiveDocument.Name
ActiveDocument.Save
FName = "C:\" + ActiveDocument.Name
CopyFile ActiveDocument.FullName, FName, False
MM.AttachmentIndex = 0
MM.AttachmentPathName = FName
MM.Send True
Kill FName
MS.SignOff
End Sub
procedure SendMail(Subject, Body, RecvAddress: string; Attachs: array of string);
var
MM, MS: Variant;
i: integer;
begin
MS := CreateOleObject('MSMAPI.MAPISession');
MM := CreateOleObject('MSMAPI.MAPIMessages');
MS.DownLoadMail := False;
MS.NewSession := False;
MS.LogonUI := True;
MS.SignOn;
MM.SessionID := MS.SessionID;
MM.Compose;
MM.RecipIndex := 0;
MM.RecipAddress := RecvAddress;
MM.MsgSubject := Subject;
MM.MsgNoteText := Body;
for i := Low(Attachs) to High(Attachs) do
begin
MM.AttachmentIndex := i;
MM.AttachmentPathName := Attachs[i];
end;
MM.Send(True);
MS.SignOff;
VarClear(MS);
VarClear(MM);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SendMail('Subject', 'Body'#13#10'Second', 'abc@163.net', ['F:\Bin\1.exe', 'F:\Bin\2.log']);
end;
上面的代码有国外的网友反映无法运行,后又有网友贴出下面的代码,说可以解决,没有验证:
uses Mapi;
function SendMail(const Subject, Body, FileName,
SenderName, SenderEMail,
RecipientName, RecipientEMail: string): Integer;
var
Message: TMapiMessage;
lpSender, lpRecipient: TMapiRecipDesc;
FileAttach: TMapiFileDesc;
SM: TFNMapiSendMail;
MAPIModule: HModule;
begin
FillChar(Message, SizeOf(Message), 0);
with Message do
begin
if (Subject <> '') then
lpszSubject := PChar(Subject);
if (Body <> '') then
lpszNoteText := PChar(Body);
if (SenderEmail <> '') then
begin
lpSender.ulRecipClass := MAPI_ORIG;
if (SenderName = '') then
lpSender.lpszName := PChar(SenderEMail)
else
lpSender.lpszName := PChar(SenderName);
lpSender.lpszAddress := PChar(SenderEmail);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := @lpSender;
end;
if (RecipientEmail <> '') then
begin
lpRecipient.ulRecipClass := MAPI_TO;
if (RecipientName = '') then
lpRecipient.lpszName := PChar(RecipientEMail)
else
lpRecipient.lpszName := PChar(RecipientName);
lpRecipient.lpszAddress := PChar(RecipientEmail);
lpRecipient.ulReserved := 0;
lpRecipient.ulEIDSize := 0;
lpRecipient.lpEntryID := nil;
nRecipCount := 1;
lpRecips := @lpRecipient;
end
else
lpRecips := nil;
if (FileName = '') then
begin
nFileCount := 0;
lpFiles := nil;
end
else
begin
FillChar(FileAttach, SizeOf(FileAttach), 0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(FileName);
nFileCount := 1;
lpFiles := @FileAttach;
end;
end;
MAPIModule := LoadLibrary(PChar(MAPIDLL));
if MAPIModule = 0 then
Result := -1
else
try
@SM := GetProcAddress(MAPIModule, 'MAPISendMail');
if @SM <> nil then
begin
Result := SM(0, Application.Handle, Message, MAPI_DIALOG or MAPI_LOGON_UI, 0);
end
else
Result := 1;
finally
FreeLibrary(MAPIModule);
end;
if Result <> 0 then
MessageDlg('Error sending mail (' + IntToStr(Result) + ').', mtError,
[mbOK], 0);
end;
PS: you must add the MAPI unit in USES-clause. To execute this procedure:
procedure TForm1.Button1Click(Sender: TObject);
begin
SendMail('Re: mailing from Delphi',
'Welcome to http://www.scalabium.com'#13#10'Mike Shkolnik',
'c:\autoexec.bat',
'your name', 'your@address.com',
'Mike Shkolnik', 'mshkolnik@scalabium.com');
end;
---------------------------------------
MSMAPI; // Microsoft MAPI Controls 6.0
Enum DeleteConstants; // Constants for Delete
GUID={20C62CB1-15DA-101B-B9A8-444553540000};
mapMessageDelete = 0;
mapRecipientDelete = 1;
mapAttachmentDelete = 2;
Enum MessagesActionConstants; // Constants for Messages control Action property.
GUID={B54A7431-FC5B-11CF-89BF-00AA00688B10};
mapFetch = 1;
mapSendDialog = 2;
mapSend = 3;
mapSave = 4;
mapCopy = 5;
mapCompose = 6;
mapReply = 7;
mapReplyAll = 8;
mapForward = 9;
mapDelete = 10;
mapShowAddressBook = 11;
mapShowRecipDetails = 12;
mapResolveName = 13;
mapDeleteRecip = 14;
mapDeleteAttachment = 15;
Enum MAPIErrors; // MAPI Error Codes
GUID={20C62CB2-15DA-101B-B9A8-444553540000};
mapSuccessSuccess = $7D00;
mapUserAbort = $7D01;
mapFailure = $7D02;
mapLoginFail = $7D03;
mapDiskFull = $7D04;
mapInsufficientMem = $7D05;
mapAccessDenied = $7D06;
mapGeneralFailure = $7D07;
mapTooManySessions = $7D08;
mapTooManyFiles = $7D09;
mapTooManyRecipients = $7D0A;
mapAttachmentNotFound = $7D0B;
mapAttachmentOpenFailure = $7D0C;
mapAttachmentWriteFailure = $7D0D;
mapUnknownRecipient = $7D0E;
mapBadRecipType = $7D0F;
mapNoMessages = $7D10;
mapInvalidMessage = $7D11;
mapTextTooLarge = $7D12;
mapInvalidSession = $7D13;
mapTypeNotSupported = $7D14;
mapAmbiguousRecipient = $7D15;
mapMessageInUse = $7D16;
mapNetworkFailure = $7D17;
mapInvalidEditFields = $7D18;
mapInvalidRecips = $7D19;
mapNotSupported = $7D1A;
mapSessionExist = $7D32;
mapInvalidBuffer = $7D33;
mapInvalidReadBufferAction = $7D34;
mapNoSession = $7D35;
mapInvalidRecipient = $7D36;
mapInvalidComposeBufferAction = $7D37;
mapControlFailure = $7D38;
mapNoRecipients = $7D39;
mapNoAttachment = $7D3A;
Enum ErrorConstants; // Error Constants
GUID={3438EDD0-D90A-11CF-89B4-00AA00688B10};
mpeOutOfMemory = 7;
mpeErrorLoadingMAPI = 48;
mpeInvalidPropertyValue = $17C;
mpeInvalidPropertyArrayIndex = $17D;
mpeSetNotSupported = $17F;
mpeGetNotSupported = $18A;
mpeUserCancelled = $7D01;
mpeUnspecifiedFailure = $7D02;
mpeLoginFailed = $7D03;
mpeDiskFull = $7D04;
mpeInsufficientMemory = $7D05;
mpeAccessDenied = $7D06;
mpeMAPIFailure = $7D07;
mpeTooManySessions = $7D08;
mpeTooManyFiles = $7D09;
mpeTooManyRecipients = $7D0A;
mpeAttachmentNotFound = $7D0B;
mpeFailedOpeningAttachment = $7D0C;
mpeFailedWritingAttachment = $7D0D;
mpeUnknownRecipient = $7D0E;
mpeInvalidRecipientType = $7D0F;
mpeNoMessages = $7D10;
mpeInvalidMessage = $7D11;
mpeTextTooLarge = $7D12;
mpeInvalidSession = $7D13;
mpeTypeNotSupported = $7D14;
mpeAmbiguousRecipient = $7D15;
mpeMessageInUse = $7D16;
mpeNetworkFailure = $7D17;
mpeInvalidEditFields = $7D18;
mpeInvalidRecipients = $7D19;
mpeNotSupported = $7D1A;
mpeUserAbortedAction = $7D1B;
mpeMAPIMissing = $7D30;
mpeLogonFailure = $7D32;
mpePropertyIsReadOnly = $7D33;
mpeInvalidAction = $7D34;
mpeNoValidSessionID = $7D35;
mpeNoOriginator = $7D36;
mpeActionNotValid = $7D37;
mpeNoMessageList = $7D38;
mpeNoRecipients = $7D39;
mpeNoAttachments = $7D3A;
Enum RecipTypeConstants; // Constants for RecipType Property
GUID={20C62CB3-15DA-101B-B9A8-444553540000};
mapOrigList = 0;
mapToList = 1;
mapCcList = 2;
mapBccList = 3;
Enum AttachTypeConstants; // Constants for AttachType Property
GUID={20C62CB4-15DA-101B-B9A8-444553540000};
mapData = 0;
mapEOLE = 1;
mapSOLE = 2;
Enum SessionActionConstants; // Constants for Session control Action property.
GUID={20C62CB5-15DA-101B-B9A8-444553540000};
mapSignOn = 1;
mapSignOff = 2;
Dispatch IMapiSession; // Microsoft MAPI Session Control
GUID={F49AC0B0-DF74-11CF-8E74-00A0C90F26F8};
function QueryInterface(riid:^GUID; out ppvObj:^^void);
function AddRef: UI4;
function Release: UI4;
function GetTypeInfoCount(out pctinfo:^UINT);
function GetTypeInfo(itinfo:UINT; lcid:UI4; out pptinfo:^^void);
function GetIDsOfNames(riid:^GUID; rgszNames:^^I1; cNames:UINT; lcid:UI4; out rgdispid:^I4);
function Invoke(dispidMember:I4; riid:^GUID; lcid:UI4; wFlags:UI2; pdispparams:^DISPPARAMS; out pvarResult:^variant; out pexcepinfo:^EXCEPINFO; out puArgErr:^UINT);
property-get DownLoadMail: bool;
property-put DownLoadMail(bool);
property-get LogonUI: bool;
property-put LogonUI(bool);
property-get NewSession: bool;
property-put NewSession(bool);
property-get Action: I2;
property-put Action(I2);
property-get SessionID: I4;
property-put SessionID(I4);
property-get Password: BSTR;
property-put Password(BSTR);
property-get UserName: BSTR;
property-put UserName(BSTR);
function SignOn;
function SignOff;
function AboutBox;
Dispatch MAPISessionEvents; // Microsoft MAPI Session Control
GUID={20C62CA2-15DA-101B-B9A8-444553540000};
Class MAPISession; // Microsoft MAPI Session Control
GUID={20C62CA0-15DA-101B-B9A8-444553540000};
function QueryInterface(riid:^GUID; out ppvObj:^^void);
function AddRef: UI4;
function Release: UI4;
function GetTypeInfoCount(out pctinfo:^UINT);
function GetTypeInfo(itinfo:UINT; lcid:UI4; out pptinfo:^^void);
function GetIDsOfNames(riid:^GUID; rgszNames:^^I1; cNames:UINT; lcid:UI4; out rgdispid:^I4);
function Invoke(dispidMember:I4; riid:^GUID; lcid:UI4; wFlags:UI2; pdispparams:^DISPPARAMS; out pvarResult:^variant; out pexcepinfo:^EXCEPINFO; out puArgErr:^UINT);
property-get DownLoadMail: bool;
property-put DownLoadMail(bool);
property-get LogonUI: bool;
property-put LogonUI(bool);
property-get NewSession: bool;
property-put NewSession(bool);
property-get Action: I2;
property-put Action(I2);
property-get SessionID: I4;
property-put SessionID(I4);
property-get Password: BSTR;
property-put Password(BSTR);
property-get UserName: BSTR;
property-put UserName(BSTR);
function SignOn;
function SignOff;
function AboutBox;
Dispatch IMapiMessages; // Microsoft MAPI Messages Control
GUID={F49AC0B2-DF74-11CF-8E74-00A0C90F26F8};
function QueryInterface(riid:^GUID; out ppvObj:^^void);
function AddRef: UI4;
function Release: UI4;
function GetTypeInfoCount(out pctinfo:^UINT);
function GetTypeInfo(itinfo:UINT; lcid:UI4; out pptinfo:^^void);
function GetIDsOfNames(riid:^GUID; rgszNames:^^I1; cNames:UINT; lcid:UI4; out rgdispid:^I4);
function Invoke(dispidMember:I4; riid:^GUID; lcid:UI4; wFlags:UI2; pdispparams:^DISPPARAMS; out pvarResult:^variant; out pexcepinfo:^EXCEPINFO; out puArgErr:^UINT);
property-get AddressCaption: BSTR;
property-put AddressCaption(BSTR);
property-get AddressEditFieldCount: I2;
property-put AddressEditFieldCount(I2);
property-get AddressLabel: BSTR;
property-put AddressLabel(BSTR);
property-get AddressModifiable: bool;
property-put AddressModifiable(bool);
property-get AddressResolveUI: bool;
property-put AddressResolveUI(bool);
property-get AttachmentCount: I4;
property-put AttachmentCount(I4);
property-get AttachmentIndex: I4;
property-put AttachmentIndex(I4);
property-get AttachmentName: BSTR;
property-put AttachmentName(BSTR);
property-get AttachmentPathName: BSTR;
property-put AttachmentPathName(BSTR);
property-get AttachmentPosition: I4;
property-put AttachmentPosition(I4);
property-get AttachmentType: I2;
property-put AttachmentType(I2);
property-get FetchMsgType: BSTR;
property-put FetchMsgType(BSTR);
property-get FetchSorted: bool;
property-put FetchSorted(bool);
property-get FetchUnreadOnly: bool;
property-put FetchUnreadOnly(bool);
property-get MsgConversationID: BSTR;
property-put MsgConversationID(BSTR);
property-get MsgCount: I4;
property-put MsgCount(I4);
property-get MsgDateReceived: BSTR;
property-put MsgDateReceived(BSTR);
property-get MsgID: BSTR;
property-put MsgID(BSTR);
property-get MsgIndex: I4;
property-put MsgIndex(I4);
property-get MsgNoteText: BSTR;
property-put MsgNoteText(BSTR);
property-get MsgOrigAddress: BSTR;
property-put MsgOrigAddress(BSTR);
property-get MsgOrigDisplayName: BSTR;
property-put MsgOrigDisplayName(BSTR);
property-get MsgRead: bool;
property-put MsgRead(bool);
property-get MsgReceiptRequested: bool;
property-put MsgReceiptRequested(bool);
property-get MsgSent: bool;
property-put MsgSent(bool);
property-get RecipAddress: BSTR;
property-put RecipAddress(BSTR);
property-get RecipCount: I4;
property-put RecipCount(I4);
property-get RecipDisplayName: BSTR;
property-put RecipDisplayName(BSTR);
property-get RecipIndex: I4;
property-put RecipIndex(I4);
property-get RecipType: I2;
property-put RecipType(I2);
property-get SessionID: I4;
property-put SessionID(I4);
property-get MsgSubject: BSTR;
property-put MsgSubject(BSTR);
property-get MsgType: BSTR;
property-put MsgType(BSTR);
property-get Action: I2;
property-put Action(I2);
function Compose;
function Copy;
function Delete([vObj:variant]);
function Fetch;
function Forward;
function Reply;
function ReplyAll;
function ResolveName;
function Save;
function Show([vDetails:variant]);
function AboutBox;
function Send([vDialog:variant]);
Dispatch MAPIMessagesEvents; // Event interface for Microsoft MAPI Messages Control
GUID={20C62CAD-15DA-101B-B9A8-444553540000};
Class MAPIMessages; // Microsoft MAPI Messages Control
GUID={20C62CAB-15DA-101B-B9A8-444553540000};
function QueryInterface(riid:^GUID; out ppvObj:^^void);
function AddRef: UI4;
function Release: UI4;
function GetTypeInfoCount(out pctinfo:^UINT);
function GetTypeInfo(itinfo:UINT; lcid:UI4; out pptinfo:^^void);
function GetIDsOfNames(riid:^GUID; rgszNames:^^I1; cNames:UINT; lcid:UI4; out rgdispid:^I4);
function Invoke(dispidMember:I4; riid:^GUID; lcid:UI4; wFlags:UI2; pdispparams:^DISPPARAMS; out pvarResult:^variant; out pexcepinfo:^EXCEPINFO; out puArgErr:^UINT);
property-get AddressCaption: BSTR;
property-put AddressCaption(BSTR);
property-get AddressEditFieldCount: I2;
property-put AddressEditFieldCount(I2);
property-get AddressLabel: BSTR;
property-put AddressLabel(BSTR);
property-get AddressModifiable: bool;
property-put AddressModifiable(bool);
property-get AddressResolveUI: bool;
property-put AddressResolveUI(bool);
property-get AttachmentCount: I4;
property-put AttachmentCount(I4);
property-get AttachmentIndex: I4;
property-put AttachmentIndex(I4);
property-get AttachmentName: BSTR;
property-put AttachmentName(BSTR);
property-get AttachmentPathName: BSTR;
property-put AttachmentPathName(BSTR);
property-get AttachmentPosition: I4;
property-put AttachmentPosition(I4);
property-get AttachmentType: I2;
property-put AttachmentType(I2);
property-get FetchMsgType: BSTR;
property-put FetchMsgType(BSTR);
property-get FetchSorted: bool;
property-put FetchSorted(bool);
property-get FetchUnreadOnly: bool;
property-put FetchUnreadOnly(bool);
property-get MsgConversationID: BSTR;
property-put MsgConversationID(BSTR);
property-get MsgCount: I4;
property-put MsgCount(I4);
property-get MsgDateReceived: BSTR;
property-put MsgDateReceived(BSTR);
property-get MsgID: BSTR;
property-put MsgID(BSTR);
property-get MsgIndex: I4;
property-put MsgIndex(I4);
property-get MsgNoteText: BSTR;
property-put MsgNoteText(BSTR);
property-get MsgOrigAddress: BSTR;
property-put MsgOrigAddress(BSTR);
property-get MsgOrigDisplayName: BSTR;
property-put MsgOrigDisplayName(BSTR);
property-get MsgRead: bool;
property-put MsgRead(bool);
property-get MsgReceiptRequested: bool;
property-put MsgReceiptRequested(bool);
property-get MsgSent: bool;
property-put MsgSent(bool);
property-get RecipAddress: BSTR;
property-put RecipAddress(BSTR);
property-get RecipCount: I4;
property-put RecipCount(I4);
property-get RecipDisplayName: BSTR;
property-put RecipDisplayName(BSTR);
property-get RecipIndex: I4;
property-put RecipIndex(I4);
property-get RecipType: I2;
property-put RecipType(I2);
property-get SessionID: I4;
property-put SessionID(I4);
property-get MsgSubject: BSTR;
property-put MsgSubject(BSTR);
property-get MsgType: BSTR;
property-put MsgType(BSTR);
property-get Action: I2;
property-put Action(I2);
function Compose;
function Copy;
function Delete([vObj:variant]);
function Fetch;
function Forward;
function Reply;
function ReplyAll;
function ResolveName;
function Save;
function Show([vDetails:variant]);
function AboutBox;
function Send([vDialog:variant]);