1

我在 Eudora 中使用以下电子邮件功能。由于某种原因,附件文件名被重命名为其他名称。如何确保附件文件名保持不变?

function SendMailMAPI(const Subject, Body, FileName, SenderName, SenderEMail, 
                  RecepientName, RecepientEMail: String) : Integer; 
var 
  message: TMapiMessage; 
  lpSender, 
  lpRecepient: TMapiRecipDesc; 
  FileAttach: TMapiFileDesc; 
  SM: TFNMapiSendMail; 
  MAPIModule: HModule; 
begin 
  FillChar(message, SizeOf(message), 0); 
  with message do 
  begin 
    if (Subject<>'') then 
    begin 
      lpszSubject := PChar(Subject) 
    end; 
    if (Body<>'') then 
    begin 
      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('SMTP:'+SenderEMail); 
      lpSender.ulReserved := 0; 
      lpSender.ulEIDSize := 0; 
      lpSender.lpEntryID := nil; 
      lpOriginator := @lpSender; 
    end; 
    if (RecepientEMail<>'') then 
    begin 
      lpRecepient.ulRecipClass := MAPI_TO; 
      if (RecepientName='') then 
      begin 
        lpRecepient.lpszName := PChar(RecepientEMail) 
      end 
      else 
      begin 
        lpRecepient.lpszName := PChar(RecepientName) 
      end; 
      lpRecepient.lpszAddress := PChar('SMTP:'+RecepientEMail); 
      lpRecepient.ulReserved := 0; 
      lpRecepient.ulEIDSize := 0; 
      lpRecepient.lpEntryID := nil; 
      nRecipCount := 1; 
      lpRecips := @lpRecepient; 
    end 
    else 
    begin 
      lpRecips := nil 
    end; 
    if (FileName='') then 
    begin 
      nFileCount := 0; 
      lpFiles := nil; 
    end 
    else 
    begin 
      FillChar(FileAttach, SizeOf(FileAttach), 0); 
      FileAttach.nPosition := Cardinal($FFFFFFFF); 
      FileAttach.lpszPathName := PChar(FileName); 
      nFileCount := 1; 
      lpFiles := @FileAttach; 
    end; 
  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; 
4

1 回答 1

1

您必须设置FileAttach.lpszFileName为您希望收件人看到的名称。如果您不这样做,则“C:\Document And Settings\MyUser\Local Settings\Temp\Hello.pdf”之类的附件对收件人来说将类似于“C__DOCUME~1_MyUser_LOCALS~1_Temp_Hello.pdf”(这可能与每个电子邮件客户端)。

所以设置 FileAttach.lpszPathName 只包含文件名:

begin 
  FillChar(FileAttach, SizeOf(FileAttach), 0); 
  FileAttach.nPosition := Cardinal($FFFFFFFF); 
  FileAttach.lpszPathName := PChar(FileName); 
  FileAttach.lpszFileName := PChar(ExtractFileName(FileName)); //add this
  nFileCount := 1; 
  lpFiles := @FileAttach; 
end;

收件人现在将看到名为“Hello.pdf”而不是“C__DOCUME~1_MyUser_LOCALS~1_Temp_Hello.pdf”的附件。

于 2010-04-22T06:53:47.550 回答