在调用EMAIL的客户端发送邮件时如何指定邮件的地址,标题,内容,附件?(100分)

  • 主题发起人 主题发起人 mycwcgr
  • 开始时间 开始时间
M

mycwcgr

Unregistered / Unconfirmed
GUEST, unregistred user!
在调用EMAIL的客户端发送邮件时如何指定邮件的地址,标题,内容,附件?
指定地址,标题很容易,mailto:my_email@21cn.com 关键是如何指定标题,如何
指定附件?
 
测试过的,可以用,你在form上放一个button
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses mapi;
{$R *.DFM}
FUNCTION SendEMail(Handle : THandle;
Mail : TStrings):Cardinal;
TYPE
TAttachAccessArray = ARRAY [0..0] OF TMapiFileDesc;
PAttachAccessArray = ^TAttachAccessArray;
VAR
MapiMessage : TMapiMessage;
Receip : TMapiRecipDesc;
Attachments : PAttachAccessArray;
AttachCount : INTEGER;
iCount : INTEGER;
FileName : STRING;
begin

fillChar(MapiMessage, SizeOf(MapiMessage), #0);
Attachments := NIL;
fillChar(Receip,SizeOf(Receip), #0);
IF Mail.Values['to'] <> ''
then

begin

Receip.ulReserved := 0;
Receip.ulRecipClass := MAPI_TO;
Receip.lpszName := StrNew(PChar(Mail.Values['to']));
Receip.lpszAddress := StrNew(PChar('SMTP:' + Mail.Values['to']));
Receip.ulEIDSize := 0;
MapiMessage.nRecipCount := 1;
MapiMessage.lpRecips := @Receip;
end;

AttachCount := 0;
FOR iCount := 0 TO MaxInt
do

begin
IF Mail.Values['attachment' + IntToStr(iCount)] = ''
then

BREAK;
AttachCount := AttachCount + 1;
end;

IF AttachCount > 0
then
begin

GetMem(Attachments,SizeOf(TMapiFileDesc) * AttachCount);
FOR iCount := 0 TO (AttachCount - 1)
do
begin

FileName := Mail.Values['attachment' + IntToStr(iCount)];
Attachments[iCount].ulReserved := 0;
Attachments[iCount].flFlags := 0;
Attachments[iCount].nPosition := ULONG($FFFFFFFF);
Attachments[iCount].lpszPathName := StrNew(PChar(FileName));
Attachments[iCount].lpszFileName := StrNew(PChar(ExtractFileName(FileName)));
Attachments[iCount].lpFileType := NIL;
end;

MapiMessage.nFileCount := AttachCount;
MapiMessage.lpFiles := @Attachments^;
end;

IF Mail.Values['subject'] <> ''
then

MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject']));
IF Mail.Values['body'] <> ''
then

MapiMessage.lpszNoteText := StrNew(PChar(Mail.Values['body']));

Result := MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle <> 0) OR MAPI_LOGON_UI OR MAPI_NEW_SESSION, 0);

FOR iCount := 0 TO (AttachCount - 1)
do
begin

strDispose(Attachments[iCount].lpszPathName);
strDispose(Attachments[iCount].lpszFileName);
end;

IF assigned(MapiMessage.lpszSubject)
then

strDispose(MapiMessage.lpszSubject);
IF assigned(MapiMessage.lpszNoteText)
then

strDispose(MapiMessage.lpszNoteText);
IF assigned(Receip.lpszAddress)
then

strDispose(Receip.lpszAddress);
IF assigned(Receip.lpszName)
then
strDispose(Receip.lpszName);
end;


procedure TForm1.Button1Click(Sender: TObject);
VAR
mail : TStringList ;
begin
mail := TStringList.Create;
mail.values['to'] := 'delphi2000@8848.net';
mail.values['subject'] := 'A subject';
mail.values['body'] := 'Some body text (line 1)';
mail.values['body'] := 'Some more body text (line 2)';
mail.values['attachment0'] := 'f:/a.txt';//附件路径要存在
sendEMail(Application.Handle, mail);
mail.Free;
end;

end.
 
谢谢terry_lzs,gi 不过您的方法只能在outlook中使用,Foxmail不能用.
能否用ShellExecute(Handle,NIL,Pchar('mailto:aaa@163.net?subject=title'),..)
这一类的简单形式
 
shellexecute这种方式我也一直在寻找,不过目前没有实现
如果你能做到了知会一声,谢谢了
 
以前有关于邮件发送的贴子,自己找找看
 
多人接受答案了。
 
to:terry_lzs
您的程序只能在wondows 2000中使用,
在WINDOWS 98中则没有执行任务操作
 
我用的操作系统就是win98,我在自己的机子上调试了以后才发给你的,你看看是不是你的
附件路径没有写对?如果不对是不会启动outlook的
 
to terry_lzs:
真是是很奇怪,我在 windows 2000能正常通过
但是我在中文windows 98 英文windows中都试过了
在运行时仅仅是屏幕闪了一下,没有启动 outlook
这是我写的您帮助我看一看,其中FUNCTION SendEMail的位置加对了没有?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses mapi;
{$R *.DFM}
FUNCTION SendEMail(Handle : THandle;
Mail : TStrings):Cardinal;
TYPE
TAttachAccessArray = ARRAY [0..0] OF TMapiFileDesc;
PAttachAccessArray = ^TAttachAccessArray;
VAR
MapiMessage : TMapiMessage;
Receip : TMapiRecipDesc;
Attachments : PAttachAccessArray;
AttachCount : INTEGER;
iCount : INTEGER;
FileName : STRING;
begin

fillChar(MapiMessage, SizeOf(MapiMessage), #0);
Attachments := NIL;
fillChar(Receip,SizeOf(Receip), #0);
IF Mail.Values['to'] <> ''
then

begin

Receip.ulReserved := 0;
Receip.ulRecipClass := MAPI_TO;
Receip.lpszName := StrNew(PChar(Mail.Values['to']));
Receip.lpszAddress := StrNew(PChar('SMTP:' + Mail.Values['to']));
Receip.ulEIDSize := 0;
MapiMessage.nRecipCount := 1;
MapiMessage.lpRecips := @Receip;
end;

AttachCount := 0;
FOR iCount := 0 TO MaxInt
do

begin
IF Mail.Values['attachment' + IntToStr(iCount)] = ''
then

BREAK;
AttachCount := AttachCount + 1;
end;
IF AttachCount > 0
then
begin

GetMem(Attachments,SizeOf(TMapiFileDesc) * AttachCount);
FOR iCount := 0 TO (AttachCount - 1)
do
begin

FileName := Mail.Values['attachment' + IntToStr(iCount)];
Attachments[iCount].ulReserved := 0;
Attachments[iCount].flFlags := 0;
Attachments[iCount].nPosition := ULONG($FFFFFFFF);
Attachments[iCount].lpszPathName := StrNew(PChar(FileName));
Attachments[iCount].lpszFileName := StrNew(PChar(ExtractFileName(FileName)));
Attachments[iCount].lpFileType := NIL;
end;

MapiMessage.nFileCount := AttachCount;
MapiMessage.lpFiles := @Attachments^;
end;

IF Mail.Values['subject'] <> ''
then

MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject']));
IF Mail.Values['body'] <> ''
then
MapiMessage.lpszNoteText := StrNew(PChar(Mail.Values['body']));

Result := MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle <> 0) OR MAPI_LOGON_UI OR MAPI_NEW_SESSION, 0);

FOR iCount := 0 TO (AttachCount - 1)
do
begin

strDispose(Attachments[iCount].lpszPathName);
strDispose(Attachments[iCount].lpszFileName);
end;

IF assigned(MapiMessage.lpszSubject)
then

strDispose(MapiMessage.lpszSubject);
IF assigned(MapiMessage.lpszNoteText)
then
strDispose(MapiMessage.lpszNoteText);
IF assigned(Receip.lpszAddress)
then

strDispose(Receip.lpszAddress);
IF assigned(Receip.lpszName)
then
strDispose(Receip.lpszName);
end;




procedure TForm1.Button1Click(Sender: TObject);
VAR
mail : TStringList ;
begin
mail := TStringList.Create;
mail.values['to'] := 'delphi2000@8848.net';
mail.values['subject'] := 'A subject';
// mail.values['body'] := 'Some body text (line 1)';
mail.values['body'] := 'Some more body text (line 2)';
mail.values['attachment0'] := 'c:/autoexec.bat';//附件路径要存在
sendEMail(Application.Handle, mail);
mail.Free;
end;


end.

 
另外下面一段程序您能调试通过吗?

const olMailItem = 0;
var Outlook: OLEVariant;
MailItem: Variant;
begin

try
Outlook := GetActiveOleObject('Outlook.Application');
except
Outlook := CreateOleObject('Outlook.Application');
end;

MailItem := Outlook.CreateItem(olMailItem);
MailItem.Recipients.Add('mshkolnik@rs-ukraine.kiev.ua');
MailItem.Subject := 'your subject';
MailItem.Body := 'Welcome to my homepage:http://www.geocities.com/mshkolnik';
MailItem.Attachments.Add('C:/Windows/Win.ini');
MailItem.Send;
Outlook := Unassigned;
 
你的第一段代码我测试过了,没有问题可以用,我想你会不会是大意了,把代码拷贝过去
放上按钮后没有把按钮事件和代码关联?
你的第二段代码我也测试了,如果你运行之前已经配置好了office中的outlook,是可以用
 
1、同样的代码,我在办公室 windows 2000中一点问题没有
在家里windows 95中却不行。
2、思前想后,我想在家里是不是因为安装了FOXMAIL软件的原因?
后来我将outlook也配置好,并设置为默认邮件程序,却还是不行
为什么?outlook还要怎样配置,我的WINDOWS 98 的VERSION 为4.10.2222A
3、第二段代码我测试也通不过,似乎还是outlook没有配置好,难道您的outlook
是单独安装的,我是在安装windows 98 时自动安装上去的,从来没有改过
 
我给你的那段代码我起动的是outlook express不是office outlook,我也没有什么配置
 
to terry_lzs:
唯一的解释就是我delphi的系统与您的不一样,可能其内部存在相互影响
我的delphi是 delphi5.0+update 2#+ADO2.6+ADO 2.6补丁
您能将上面您编译好的程序发给我测试一下吗?
我的EMAIL为: mycgr@21cn.com
谢谢!!!
 
发出去给你了,如果有问题说一声,能帮到的我会去做
 
在家中的中文及英文windows 98 不能运行的程序,我放在同事的中文windows 98
居然运行得很好。百思不得其解!!!估计是和 delphi5 的 升级包有冲突。
请问'调用office outlook的程序..',是指在另外安装的 office 2000中的outlook
而不是自带的outlook?
 
感谢大家,我终于找到原因了。
其实,上面两种方法,都是需要安装office outlook的,而不是只有outlook express就够了
terry_lzs 您说对吗?
 
后退
顶部