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

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

问题描述

在我的程序中,我正在撰写一封电子邮件,使用用户机器上安装的默认电子邮件客户端软件进行发送。



我已经编写了mailto地址,主题,多边体,我有几个附件被包含。



我使用mailto和ShellExecute几乎可以这样工作:

 消息:='mailto:someone@somewhere.com '
+'?subject =这是subjectBehold错误报告'
+'& body =这是第1行'+'%0D%0A'
+'这是第2行' +'%0D%0A'
+'这是第3行'
+'& Attach = c:\file1.txt';
RetVal:= ShellExecute(Handle,'open',PChar(Message),nil,nil,SW_SHOWNORMAL);
如果RetVal <= 32 then
MessageDlg('找不到程序发送电子邮件',mtWarning,[mbOK],0);

在Windows Vista机器上使用Delphi 2009,这将打开一个Microsoft Mail创建邮件窗口, To,Subject和Body正确填充。但是,该文件没有附加。



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



然后我发现 Zarko Gajic表示:这种方法没关系,但是你无法以这种方式发送附件。



我也看过Windows简单邮件API(MAPI),但Zarko表示,只有最终用户具有符合MAPI标准的电子邮件软件才有效。有关使用MAPI与Delphi的详细记录技术(例如发送电子邮件 - 邮件使用mapi ),但他们都有免责声明MAPI并不总是与Windows一起安装。



此外,我真的希望消息首先出现在用户的默认电子邮件程序中,所以他们将它作为电子邮件记录的一部分,他们可以编辑它,并决定是否和何时发送它。我不知道MAPI如何工作,如果这样做。



所以我的要求是:


  1. 将邮件发送到用户的邮件程序。


  2. 允许一个或多个附件。


  3. 与XP(即XP,Vista或7)的任何Windows机器上的所有电子邮件客户端合作(希望)。


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



大多数人做什么?






编辑:



MAPI解决方案甚至是Indy解决方案都有一些答案。 >

我与他们的问题是他们不一定使用默认的邮件客户端。例如,在Vista机器上,我设置了Windows Mail作为我的默认客户端。当我进行MAPI发送时,它不会启动Windows Mail,但它会打开并设置Outlook中的电子邮件。我不想要



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


您的调试例程失败发送文件,因为它尝试启动Windows邮件,因为它是自己的一些原因,而不是使用默认邮件客户端(在我的情况下是雷鸟)



我试图填写例外
报告,但放弃当它要求
这个服务器,该服务器!我然后得到
真的很懊恼,因为它启动
Outlook
- 我从来没有使用它或想要使用它。


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



此外,我需要知道MAPI现在是否普及。 5年前,它不能保证在所有机器上都可以工作,因为它没有作为操作系统的一部分安装。这是否仍然是真的,或者MAPI现在默认使用Windows XP,Vista和7?



与Indy或任何其他建议的解决方案相同的问题。它可以与默认的客户端一起使用,几乎所有的Windows XP和更高版本的机器都可以使用它。



mailto解决方案的原因很好,就是所有机器必须支持它,以便处理在网页上发现的HTML mailto语句。现在,如果只有我可以使用它添加附件...






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



mailto不限于256个字符,如HTML mailto,但我被摧毁了,最终被限制为2048个字符。幸运的是,几个小时后,mjustin给了他答案。



如果这样执行还行,他的答案将为我做。如果没有,我会在这里添加我的评论。






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



但是,我在文章中找到了: SendTo邮件收件人


在这一点上,您可以用
:: ShellExecute替换
:: WinExec调用,使用
注册表和目标中声明的实际
mailto命令行当前的电子邮件
客户端(例如,
%ProgramFiles%\Outlook
Express\msimn.exe/ mailurl:%1)。但是
那么限制是32 KB。作为
结论,没有办法使用
mailto协议发送大于32KB的
电子邮件。


但是,我必须确定每种情况下邮件客户端是谁。我预计会导致进一步的复杂化。



我发现的另一件事是,mailto允许设置到,cc,bcc,主体和身体,但没有附件。而sendto ONLY允许附件,然后设置一个带有默认消息的默认电子邮件,没有办法设置各种字段和正文。

解决方案

看来,ShellExecute中的mailto无法发送附件。



MAPI和Indy有不一定选择用户电子邮件客户端的不幸特征。 / p>

所以另一个可能性是继续使用ShellExecute,但是找到另一种方式来将附件放入电子邮件客户端。



我决定做的是在我的Dialog上创建电子邮件,我现在有一个FileListBox列出用户可能要附加到电子邮件的文件。当电子邮件弹出时,他们可以将其拖放到电子邮件中。



在我的情况下,这实际上是一个很好的解决方案,因为这允许用户选择他们想要包括的文件。另一种方法(自动附加它们)将需要删除不希望包含的方法。 (即使您已经选中添加Google工具栏选项不是很好)



目前该解决方案可以正常工作。



感谢所有提供答案的人,并帮助我看到了这一切(所有+1)。


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

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

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);

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.

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

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.

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.

So my requirements are:

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

  2. To allow one or more attachments.

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

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

What do most people do?


Edit:

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

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:

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.

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.

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?

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?

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


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.

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.


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.

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

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 Express\msimn.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.

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.

解决方案

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

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

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

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.

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.

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

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

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