使用Delphi发送Outlook电子邮件 [英] Sending Outlook Email with Delphi

查看:180
本文介绍了使用Delphi发送Outlook电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过OLE在Delphi 10.4和Outlook 365之间成功发送电子邮件.

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

当我使用这个

MailItem.Send;

我明白了

EOleSysError: The parameter is incorrect

怎么了?如何跳过编辑器而仅发送电子邮件?有什么建议吗?

What's wrong ? How can I skip the editor and just send the email ? Any suggestion ?

谢谢

推荐答案

今天可能是您的幸运日,鲍勃!

Today might be your lucky day, Bob!

我维护一个使用Outlook对象模型发送电子邮件的应用程序.发送电子邮件的代码几年来一直没有变化,成千上万的用户已经成功使用它.

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.

在过去的几周中,其中一些用户升级到了最新版本的Office 365,并开始体验完全相同的参数不正确".调用MailItem.Send时发生错误.所有这些用户的共同点是相同的:

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:

  1. 他们没有升级我们的应用程序(它们仍在运行可以正常使用多年的相同版本)
  2. 他们 DID 升级Office 365.
  1. They did NOT upgrade our application (they are still running the same version that was working fine for years)
  2. They DID upgrade Office 365.

鉴于以上两点,并不需要火箭科学家得出结论,Microsoft必须在最新版本的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.

我与Microsoft的Office 365支持团队一起开了一个案子,并且不出所料,此案四处走动,而Microsoft没有提供任何解决方案.

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.

关于错误消息参数不正确"的愚蠢的事情.是MailItem.Send没有任何参数!

The stupid thing about the error message "The parameter is incorrect" is that MailItem.Send does not take any parameters!

无论如何,我放弃了Microsoft的Office 365支持团队,开始摆弄代码,多年来一直没有更改.

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.

经过大量修改,我发现由于某些未知和未记录的原因,对MailItem.GetInspector的调用似乎导致对MailItem.Send的后续调用引发了错误.当我注释掉MailItem.GetInspector时,MailItem.Send正常工作,并且没有引发错误.

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.

但是对我来说,注释掉MailItem.GetInspector并不是一个长期的选择,因为我使用Inspector对电子邮件正文进行了几种操作.

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.

因此,我阅读了Inspector对象的文档,并找到了Close方法.在电子邮件正文操作之后和MailItem.Send调用之前,我添加了对Inspector.Close的调用.答对了!修复了错误!!!

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!!!!

这是一些有效的VBA示例代码.我在Excel中对其进行了测试.

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

鲍勃,请回覆告诉我Inspector.关闭为您解决了该错误.

Bob, please reply back to let me know if Inspector.Close fixed the error for you.

这篇关于使用Delphi发送Outlook电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆