Delphi 如何向传真收件人发送带有附件的MAPI电子邮件?
我正在使用从Delphi应用程序内部发送带有PDF附件的MAPI电子邮件 它会弹出一个MS Outlook“新建邮件”窗口,其中已附加pdf文档,收件人为空 如果你输入一个普通的电子邮件联系人,那么它就可以通过罚款 但是,如果选择传真收件人,它会显示在我的“已发送邮件”文件夹中,但传递会自动失败(没有错误,没有MS Outlook“传递失败”消息,也没有传递消息) “传真收件人”是在MS Outlook中设置的,只有传真号码。没有电子邮件什么的。我们使用电子邮件将这些“传真”发送到outlook收件箱 如果您愿意,我为该联系人填写的唯一字段是标有“商务传真”的字段 如果我手动(即,在我的应用程序之外)创建一封标准的MS Outlook电子邮件,并选择完全相同的传真收件人,然后手动附加完全相同的PDF,那么它将正常运行 因此,似乎使用MAPI发送传真号码会导致失败。 有人能给我一些建议吗 谢谢 更新:如果我使用MAPI创建电子邮件,但是我手动删除了附件,那么它就不起作用了。因此,在outlook中,我可以通过电子邮件将附件发送给传真收件人,但使用MAPI时失败 完整的源代码如下:Delphi 如何向传真收件人发送带有附件的MAPI电子邮件?,delphi,email,mapi,fax,Delphi,Email,Mapi,Fax,我正在使用从Delphi应用程序内部发送带有PDF附件的MAPI电子邮件 它会弹出一个MS Outlook“新建邮件”窗口,其中已附加pdf文档,收件人为空 如果你输入一个普通的电子邮件联系人,那么它就可以通过罚款 但是,如果选择传真收件人,它会显示在我的“已发送邮件”文件夹中,但传递会自动失败(没有错误,没有MS Outlook“传递失败”消息,也没有传递消息) “传真收件人”是在MS Outlook中设置的,只有传真号码。没有电子邮件什么的。我们使用电子邮件将这些“传真”发送到outlook
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function SendEMailUsingMAPI(const Subject, Body, FileName, SenderName,
SenderEMail, RecipientName, RecipientEMail: string): integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses
Mapi;
procedure TForm1.Button1Click(Sender: TObject);
begin
//this will bring up an MS Outlook dialog.
//inside that dialog, if i choose a normal email recipient, it works.
// if i choose a fax recipient, it fails silently.
//if i create the email from w/in outlook, it can go to *either* with success.
SendEmailUsingMAPI(
'Subject', //subject of email
'Body', //body of email text
'c:\my_doc.pdf', //attachment file name
'My name', //sender email name
'myemail@mydomain.com', //sender email address
'', //recipient email name
''); //recipient email address
end;
function TForm1.SendEMailUsingMAPI(const Subject, Body, FileName, SenderName,
SenderEMail, RecipientName, RecipientEMail: string): Integer;
var
Message: TMapiMessage;
lpSender, lpRecipient: TMapiRecipDesc;
FileAttach: TMapiFileDesc;
SM: TFNMapiSendMail;
MAPIModule: HModule;
FileType: TMapiFileTagExt;
begin
FillChar(Message,SizeOf(Message),0);
if (Subject <> '') then begin
Message.lpszSubject := PChar(Subject);
end;
if (Body <> '') then begin
Message.lpszNoteText := PChar(Body);
end;
if (SenderEmail <> '') then
begin
lpSender.ulRecipClass := MAPI_ORIG;
if (SenderName = '') then begin
lpSender.lpszName := PChar(SenderEMail);
end
else begin
lpSender.lpszName := PChar(SenderName);
end;
lpSender.lpszAddress := PChar(SenderEmail);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
Message.lpOriginator := @lpSender;
end;
if (RecipientEmail <> '') then begin
lpRecipient.ulRecipClass := MAPI_TO;
if (RecipientName = '') then begin
lpRecipient.lpszName := PChar(RecipientEMail);
end
else begin
lpRecipient.lpszName := PChar(RecipientName);
end;
lpRecipient.lpszAddress := PChar(RecipientEmail);
lpRecipient.ulReserved := 0;
lpRecipient.ulEIDSize := 0;
lpRecipient.lpEntryID := nil;
Message.nRecipCount := 1;
Message.lpRecips := @lpRecipient;
end
else begin
Message.lpRecips := nil;
end;
if (FileName = '') then begin
Message.nFileCount := 0;
Message.lpFiles := nil;
end
else begin
FillChar(FileAttach,SizeOf(FileAttach),0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(FileName);
FileType.ulReserved := 0;
FileType.cbEncoding := 0;
FileType.cbTag := 0;
FileType.lpTag := nil;
FileType.lpEncoding := nil;
FileAttach.lpFileType := @FileType;
Message.nFileCount := 1;
Message.lpFiles := @FileAttach;
end;
MAPIModule := LoadLibrary(PChar(MAPIDLL));
if MAPIModule = 0 then begin
Result := -1;
end
else begin
try
@SM := GetProcAddress(MAPIModule,'MAPISendMail');
if @SM <> nil then begin
Result := SM(0,Application.Handle,Message,
MAPI_DIALOG or MAPI_LOGON_UI,0);
end
else begin
Result := 1;
end;
finally
FreeLibrary(MAPIModule);
end;
end;
if Result <> 0 then begin
MessageDlg('Error sending mail ('+IntToStr(Result)+').',mtError,[mbOK],0);
end;
end;
end.
unitmain;
接口
使用
窗口、消息、系统工具、类、图形、控件、窗体、对话框、,
StdCtrls;
类型
TForm1=类(TForm)
按钮1:t按钮;
程序按钮1点击(发送方:ToObject);
私有的
函数SendEMailUsingMAPI(常量主题、正文、文件名、发件人名称、,
SenderEMail,RecipientName,RecipientEMail:string):整数;
公众的
{公开声明}
结束;
变量
表1:TForm1;
实施
{$R*.DFM}
使用
Mapi;
程序TForm1.按钮1单击(发送方:TObject);
开始
//这将打开一个MS Outlook对话框。
//在这个对话框中,如果我选择一个普通的电子邮件收件人,它就会工作。
//如果我选择传真收件人,它会自动失败。
//如果我从outlook中的w/创建电子邮件,它可以转到*或*并成功。
使用MAPI发送电子邮件(
'Subject',//电子邮件的主题
“Body”,//电子邮件文本的正文
'c:\my_doc.pdf',//附件文件名
'我的名字',//发件人电子邮件名称
'myemail@mydomain.com“,//发件人电子邮件地址
'',//收件人电子邮件名称
''); //收件人电子邮件地址
结束;
函数TForm1.SendEMailUsingMAPI(常量主题、正文、文件名、发件人名称、,
SenderEMail,RecipientName,RecipientEMail:string):整数;
变量
消息:TMapiMessage;
lpSender,lpRecipient:TMapiRecipDesc;
FileAttach:TMapiFileDesc;
SM:TFNMapiSendMail;
MAPIModule:HModule;
文件类型:TMapiFileTagExt;
开始
FillChar(消息,SizeOf(消息),0);
如果是(主题“”),则开始
Message.lpszSubject:=PChar(Subject);
结束;
如果是(正文“”),则开始
Message.lpszNoteText:=PChar(正文);
结束;
如果(SenderEmail“”),则
开始
lpSender.ulRecipClass:=MAPI_ORIG;
如果(SenderName=''),则开始
lpSender.lpszName:=PChar(SenderEMail);
结束
否则开始
lpSender.lpszName:=PChar(SenderName);
结束;
lpSender.lpszAddress:=PChar(SenderEmail);
lpSender.ulReserved:=0;
lpSender.ulEIDSize:=0;
lpSender.lpEntryID:=nil;
Message.lpOriginator:=@lpSender;
结束;
如果(收件人电子邮件“”),则开始
lpRecipient.ulRecipClass:=MAPI_TO;
如果(RecipientName=''),则开始
lpRecipient.lpszName:=PChar(RecipientEMail);
结束
否则开始
lpRecipient.lpszName:=PChar(RecipientName);
结束;
lpRecipient.lpszAddress:=PChar(RecipientEmail);
lpRecipient.ulReserved:=0;
lpRecipient.ulEIDSize:=0;
lpRecipient.lpEntryID:=nil;
Message.nRecipCount:=1;
Message.lpRecips:=@lpRecipient;
结束
否则开始
Message.lpRecips:=nil;
结束;
如果(文件名=“”),则开始
Message.nFileCount:=0;
Message.lpFiles:=nil;
结束
否则开始
FillChar(FileAttach,SizeOf(FileAttach),0);
FileAttach.nPosition:=基数($ffffffffff);
FileAttach.lpszPathName:=PChar(文件名);
FileType.ulReserved:=0;
FileType.cbEncoding:=0;
FileType.cbTag:=0;
FileType.lpTag:=nil;
FileType.lpEncoding:=nil;
FileAttach.lpFileType:=@FileType;
Message.nFileCount:=1;
Message.lpFiles:=@FileAttach;
结束;
MAPIModule:=加载库(PChar(MAPIDLL));
如果MAPIModule=0,则开始
结果:=-1;
结束
否则开始
尝试
@SM:=GetProcAddress(MAPIModule,'MAPISendMail');
如果@SM nil,则开始
结果:=SM(0,Application.Handle,Message,
MAPI_对话框或MAPI_登录界面,0);
结束
否则开始
结果:=1;
结束;
最后
免费图书馆(MAPI模块);
结束;
结束;
如果结果为0,则开始
MessageDlg('Error sending mail('+IntToStr(Result)+'),mtError[mbOK],0);
结束;
结束;
结束。
可能是传真地址在0(临时)会话中不可用吗?换句话说,是否先使用MAPILogon登录会话,然后在MAPISendMail呼叫帮助中提供会话 您可以尝试启用,希望会出现一些(任何)错误消息。确保首先记录手动传真(工作情况),以检查此日志中是否显示任何相关内容
不幸的是,我个人通过此日志解决问题的成功率很高,但尝试获取更多信息不会有什么坏处,对吗?好的,您的更新指向附件,因此我将提出另一个猜测:尝试将附件的文件类型显式设置为“application/pdf”(您当前的代码未设置lpFileType字段)。传真处理可能依赖于此。您只需将MapiFileTagExt(lpFileType类型指向)的编码部分保留为空,只需填充字符即可
FillChar(FileAttach,SizeOf(FileAttach),0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(FileName);
//
MimeType := 'application/pdf';
//
FileType.ulReserved := 0;
FileType.cbTag := Length( MimeType );
FileType.lpTag := PByte(MimeType);
FileType.cbEncoding := 0;
FileType.lpEncoding := nil;
//
FileAttach.lpFileType := @FileType;
Message.nFileCount := 1;
Message.lpFiles := @FileAttach;