Delphi 程序如何通过默认电子邮件客户端发送带有附件的电子邮件? [英] How can a Delphi Program send an Email with Attachments via the DEFAULT E-mail Client?

查看:65
本文介绍了Delphi 程序如何通过默认电子邮件客户端发送带有附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我使用安装在用户机器上的默认电子邮件客户端软件编写要发送的电子邮件.

Within my program, I am composing an email to send using the default e-mail client software installed on a user's machine.

我已经编写了 mailto 地址、主题、多行正文,并且我有几个附件要包含在内.

I have composed the mailto address, the subject, the multilined body, and I have several attachments to be included.

我几乎使用 mailto 和 ShellExecute 完成了这项工作,如下所示:

I almost got this working using mailto and ShellExecute as follows:

  Message := 'mailto:someone@somewhere.com'
    + '?subject=This is the subjectBehold Error Report'
    + '&body=This is line 1' + '%0D%0A'
    + 'This is line 2' + '%0D%0A'
    + 'This is line 3'
    + '&Attach=c:file1.txt';
  RetVal := ShellExecute(Handle, 'open', PChar(Message), nil, nil, SW_SHOWNORMAL);
  if RetVal <= 32 then
    MessageDlg('Cannot find program to send e-mail.', mtWarning, [mbOK], 0);

在 Windows Vista 机器上使用 Delphi 2009,这将打开 Microsoft Mail创建邮件"窗口,其中正确填写了收件人、主题和正文.但是文件没有被附加.

Using Delphi 2009 on a Windows Vista machine, this will open a Microsoft Mail "Create Mail" window, with the To, Subject and Body filled correctly. However the file does not get attached.

当我研究这个时,我注意到一些评论指出这种技术不适用于所有邮件客户端.然而,大部分评论都相当陈旧,因为我意识到这是一种非常古老的技术.

As I researched this, I noticed some commentary stating that this technique does not work with all mail clients. However, most of the commentary is fairly old, as I realize this is a very old technique.

然后我发现 Zarko Gajic 说这种方式可以,但是这种方式不能发送附件".

Then I found that Zarko Gajic said that "this approach is ok, but you are unable to send attachments in this way".

我也看到了 Windows 简单邮件 API (MAPI),但 Zarko 说只有在最终用户拥有符合 MAPI 的电子邮件软件时才有效.有关于在 Delphi 中使用 MAPI 的详细记录技术(例如 Sending e-使用 mapi 发送邮件),但他们都声明 MAPI 并不总是随 Windows 一起安装.

I have seen theres also the Windows Simple Mail API (MAPI), but Zarko says that only works if the end-user has MAPI-compliant email software. There are well documented techniques on using MAPI with Delphi (e.g. Sending e-mail using mapi), but they all have the disclaimer that MAPI is not always installed with Windows.

此外,我真的希望消息首先出现在用户的默认电子邮件程序中,因此他们会将其作为电子邮件记录的一部分,他们可以对其进行编辑并决定是否以及何时发送.我不确定 MAPI 是如何工作的,以及它是否会这样做.

Besides, I really want the message to come up first in the user's default email program, so they will have it as part of their email records and they can edit it and decide if and when they want to send it. I'm not sure how MAPI works and if it will do that.

所以我的要求是:

  1. 在用户的邮件程序中显示电子邮件.

  1. To bring the email up in the user's mail program.

允许一个或多个附件.

(希望如此)使用 XP 及以上任何 Windows 计算机(即 XP、Vista 或 7)上的所有电子邮件客户端.

To work with (hopefully) all email clients on any Windows machine from XP up (i.e. XP, Vista or 7).

有这种动物吗?或者也许有人知道如何使用 mailto/ShellExecute 技术获取附件?

Is there such an animal? Or maybe does someone know how to get attachments to work with the mailto/ShellExecute technique?

大多数人做什么?

有一些 MAPI 解决方案甚至 Indy 解决方案的答案.

There have been a few answers with MAPI solutions and even an Indy solution.

我对他们的问题是他们不一定使用默认的邮件客户端.例如,在我的 Vista 机器上,我已将 Windows Mail 设置为我的默认客户端.当我发送 MAPI 时,它不会打开 Windows Mail,而是在 Outlook 中打开和设置电子邮件.我不想要那个.

The problem I have with them is that they don't necessarily use the default mail client. On my Vista machine, for example, I have set up Windows Mail as my default client. When I do a MAPI send, it does not bring up Windows Mail, but it brings up and sets up the email in Outlook instead. I don't want that.

我的程序的两个用户抱怨:

Two of my users of my program complained:

您的调试例程无法发送文件,因为它尝试启动 Windows 邮件,原因是它自己已知的某种原因,而不是使用默认的邮件客户端(在我的情况下为 Thunderbird)

Your debug routine fails to send the file, as it tries to start windows mail for some reason known to it's self rather than using the default mail client (in my case thunderbird)

我试图填写异常报告但在要求时放弃了这个服务器,那个服务器!然后我得到了真的很生气,因为它推出了外表- 我从来没有,从来没有使用过它,或者想要使用它.

I tried to fill up the exception report but gave up when it asked for this server, that server! I then got really annoyed because it launched Outlook - I never, ever use it or want to use it.

我不需要 MAPI 或 Indy 的代码.它们随时可用.但是,如果您建议使用 MAPI 或 Indy,我真正需要的是一种找到默认客户端并确保它是通过电子邮件发送的客户端的方法.

I don't need code for MAPI or Indy. They are readily available. But if you suggest MAPI or Indy, what I really need is a way to find the default client and ensure that it is the one that is passed the email to be sent.

另外,我需要知道 MAPI 现在是否通用.5 年前,它不能保证在所有机器上都能运行,因为它没有作为操作系统的一部分安装.是否仍然如此,或者 MAPI 现在默认与 Windows XP、Vista 和 7 一起提供?

Also, I need to know if MAPI is now universal. 5 years ago, it wasn't guaranteed to work on all machines because it wasn't installed as part of the operating system. Is that still true, or does MAPI now come with Windows XP, Vista and 7 by default?

同样的问题适用于 Indy 或任何其他建议的解决方案.它可以与默认客户端一起使用吗?它可以在几乎所有 Windows XP 和更高版本的机器上使用吗?

Same questions go for Indy or any other suggested solutions. Can it work with the default client and will it work on almost all Windows XP and later machines?

mailto"解决方案之所以这么好,是因为为了处理网页上的HTML mailto 语句,所有机器都必须支持它.现在要是我能用它来添加附件就好了……

The reason why the "mailto" solution is so nice, is that all machines have to support it for the purpose of handling the HTML mailto statement found on webpages. Now if only I could use it to add attachments ...

找到了可能的解决方案:mjustin 指出了使用操作系统的 sendto 命令的替代方法.这很可能是要走的路.

Likely solution found: mjustin pointed out an alternative that makes use of the Operating System's sendto command. That most likely is the way to go.

mailto 不像 HTML mailto 那样被限制为 256 个字符,但我很震惊地发现它最终被限制为 2048 个字符.幸运的是,几个小时后,mjustin 给出了答案.

The mailto was not limited to 256 characters like the HTML mailto is, but I was devastated to find out it ended up being limited to 2048 characters. Fortunately a few hours later, mjustin gave his answer.

如果实施顺利,他的回答将为我完成.如果没有,我会在这里添加我的评论.

If implementation of that goes okay, his answer will have done it for me. If not, I'll add my comments here.

没有.事实证明,sendto 解决方案并不总是打开默认的电子邮件程序.在我的机器上,当我的默认邮件程序是 Windows Mail 时,它会打开 Outlook.太糟糕了.尽管有 2048 个字符的限制,我还是不得不回到 mailto 方法.

No. As it turns out, the sendto solution will not always open the default email program. On my machine, it opens Outlook when my default mailer is Windows Mail. Too bad. I've had to go back to the mailto method, despite the 2048 character limit.

然而,我在文章中发现:SendTo 邮件收件人:

I did, however, find in the article: SendTo mail recipient that:

此时,你可以更换:: ShellExecute 深思熟虑::WinExec 调用,使用实际mailto 命令行中声明的注册并定位当前电子邮件客户(例如,"%ProgramFiles%OutlookExpressmsimn.exe"/mailurl:%1). 但是那么限制是 32 KB.作为一个结论,没有办法发送大于 32KB 的电子邮件使用mailto 协议.

At this point, you could replace ::ShellExecute with a well thought ::WinExec call, using the actual mailto command line declared in the registry and target the current e-mail client (for instance, "%ProgramFiles%Outlook Expressmsimn.exe" /mailurl:%1). But then the limitation is 32 KB. As a conclusion, there is no way to send e-mails larger than 32KB using the mailto protocol.

但随后我必须确定每种情况下的邮件客户端是谁.我预计这会导致进一步的并发症.

but then I'd have to determine who the mail client is in each case. I expect that would lead to further complications.

我发现的另一件事是 mailto 允许设置to"、cc"、bcc"、subject"和body",但不允许设置附件.而 sendto 只允许附件,然后设置带有默认消息的默认电子邮件,而您无法设置各种字段和正文.

The one other thing I found out is that mailto allows setting of "to", "cc", "bcc", "subject" and "body" but no attachments. Whereas sendto ONLY allows attachments and then sets up a default email with a default message and no way for you to set the various fields and body.

推荐答案

ShellExecute 中的 mailto 似乎无法发送附件.

It appears that mailto in a ShellExecute is not capable of sending attachments.

MAPI 和 Indy 有一个不幸的特点,即不一定选择用户的电子邮件客户端.

MAPI and Indy have the unfortunate characteristic of not necessarily selecting the user's email client.

所以另一种可能是继续使用 ShellExecute,但要找到另一种方法将附件放入电子邮件客户端.

So the other possibility is to continue using ShellExecute, but find another way to get the attachments into the email client.

我决定做的是在创建电子邮件的对话框上,我现在有一个 FileListBox 列出用户可能想要附加到电子邮件的文件.当电子邮件弹出时,他们只需将它们拖放到电子邮件中即可.

What I decided to do was on my Dialog that creates the email, I now have a FileListBox listing the files the user may want to attach to the email. When the email pops up, they can simply drag and drop them to the email.

就我而言,这实际上是一个很好的解决方案,因为这允许用户选择他们想要包含的文件.另一种方法(自动附加它们)将要求他们删除不想包含的那些.(即已经为您检查了添加 Google 工具栏"选项并不好)

In my case, this is actually a good solution, since this allows the users to select the files they want to include. The other method (automatically attaching them) will require they delete the ones they don't want included. (i.e. Having the "Add Google toolbar" option already checked for you is NOT good)

目前这个解决方案是可行的.

For the time being this solution will work.

感谢所有提供答案并帮助我解决问题的人(所有 +1).

Thank you to all those who contributed answers and helped me see my way through this (all +1).

这篇关于Delphi 程序如何通过默认电子邮件客户端发送带有附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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