问题
I can successfully send an email by OLE between Delphi 10.4 and Outlook 365.
try
Outlook:=GetActiveOleObject('Outlook.Application');
except
Outlook:=CreateOleObject('Outlook.Application');
end;
try
MailItem:= Outlook.CreateItem(olMailItem) ;
SubjectLine:= 'Whatver';
MailItem.Subject:= SubjectLine;
EmailTo:= 'somebody@somewhere.com';
MailItem.Recipients.Add(EmailTo);
MailItem.BodyFormat := olFormatPlain;
MailItem.GetInspector;
Attachment:= 'C:\File.doc';
MailItem.Attachments.Add(Attachment);
MessageBodyStr:= 'Dear Friend';
MailItem.Body:= MessageBodyStr;
MailItem.Display(False);
finally
Outlook := Unassigned;
end;
This is working just fine. But I would prefer sending the email directly without displaying the email editor
When I am using this
MailItem.Send;
I get this
EOleSysError: The parameter is incorrect
What's wrong ? How can I skip the editor and just send the email ? Any suggestion ?
Thanks
回答1:
Today might be your lucky day, Bob!
I maintain an application that uses the Outlook Object Model to send emails. The code for sending the email has been unchanged for several years, and thousands of users have been using it successfully.
In the last few weeks, some of those users upgraded to the latest versions of Office 365 and started experiencing the exact same "The parameter is incorrect" error when MailItem.Send is called. All of those users had the same things in common:
- They did NOT upgrade our application (they are still running the same version that was working fine for years)
- They DID upgrade Office 365.
Given those two points above, it doesn't take a rocket scientist to conclude that Microsoft must have changed something in the latest versions of Office 365 and that change is causing this error.
I opened a case with Microsoft's Office 365 Support Team, and, as might be expected, the case went around and around circles without Microsoft providing any solution whatsoever.
The stupid thing about the error message "The parameter is incorrect" is that MailItem.Send does not take any parameters!
Anyway, I gave up on Microsoft's Office 365 Support Team and started fiddling with the code, which hasn't been changed in many years.
After much tinkering, I discovered that, for some unknown and undocumented reason, the call to MailItem.GetInspector seems to cause the subsequent call to MailItem.Send to raise the error. When I commented out MailItem.GetInspector, MailItem.Send worked perfectly and did not raise the error.
But for me, commenting out MailItem.GetInspector is not a long-term option because I use the Inspector to do several manipulations of the email body.
So, I read the docs for the Inspector object and found the method Close. I added a call to Inspector.Close after the email body manipulations and before the call to MailItem.Send. Bingo! That fixed the error!!!!
Here is some VBA sample code that works. I tested it in Excel.
Sub TestSendEmail()
Dim app As Outlook.Application
Dim nameSpace As Outlook.nameSpace
Dim folder As Outlook.MAPIFolder
Dim mailItem As Outlook.mailItem
Dim insp As Outlook.Inspector
Dim wordDocumentEditor As Word.Document
On Error GoTo errorHandler
Set app = New Outlook.Application
Set nameSpace = app.GetNameSpace("MAPI")
Set folder = nameSpace.GetDefaultFolder(Outlook.olFolderOutbox)
Set mailItem = app.CreateItem(Outlook.olMailItem)
mailItem.Subject = "Test Subject"
mailItem.To = "joe.kelly@binarystream.com"
Set insp = mailItem.GetInspector
Set wordDocumentEditor = insp.WordEditor
wordDocumentEditor.Range(0, 0).InsertBefore ("Test Body")
insp.Close (olSave)
MsgBox "Calling Send"
mailItem.Send
MsgBox "Send Complete"
Done:
Exit Sub
errorHandler:
MsgBox "The following error occurred: " & Err.Number & ": " & Err.Description
End Sub
Bob, please reply back to let me know if Inspector.Close fixed the error for you.
来源:https://stackoverflow.com/questions/64213396/sending-outlook-email-with-delphi