delphi - 如何立即发送邮件而不提示确认对话框

标签 delphi delphi-xe3 mapi

我正在使用 Delphi XE3,下面是我的示例应用程序:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    function Send(const FromAddr, ToAddr, Subject: String; const AttachFiles: array
        of string; const MsgBody: String): boolean;
  end;

var
  Form1: TForm1;

implementation

uses Winapi.Mapi;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Send('', 'lmengyew@gmail.com', 'test', [], '');
end;

function TForm1.Send(const FromAddr, ToAddr, Subject: String; const
    AttachFiles: array of string; const MsgBody: String): boolean;
var Msg: TMapiMessage;
    lpSender, lpRecipient: TMapiRecipDesc;
    Attach: array of TMapiFileDesc;
    SMTP: TFNMapiSendMail;
    MAPIModule: HModule;
    i: integer;
    S: string;
begin
  Result := False;
  FillChar(Msg, SizeOf(Msg), 0);

  Msg.lpszSubject := PAnsiChar(UTF8String(Subject));
  Msg.lpszNoteText := PAnsiChar(UTF8String(MsgBody));

  if FromAddr <> '' then begin
    lpSender.ulRecipClass := MAPI_ORIG;
    lpSender.lpszName     := PAnsiChar(UTF8String(FromAddr));
    lpSender.lpszAddress  := PAnsiChar(UTF8String(FromAddr));
    lpSender.ulReserved   := 0;
    lpSender.ulEIDSize    := 0;
    lpSender.lpEntryID    := Nil;
    Msg.lpOriginator      := @lpSender;
  end;

  if ToAddr <> '' then begin
    lpRecipient.ulRecipClass := MAPI_TO;
    lpRecipient.lpszName     := PAnsiChar(UTF8String(ToAddr));
    lpRecipient.lpszAddress  := PAnsiChar(UTF8String(ToAddr));
    lpRecipient.ulReserved   := 0;
    lpRecipient.ulEIDSize    := 0;
    lpRecipient.lpEntryID    := Nil;
    Msg.nRecipCount          := 1;
    Msg.lpRecips             := @lpRecipient;
  end;

  SetLength(Attach, Length(AttachFiles));
  FillChar(Attach[0], Length(Attach) * SizeOf(TMapiFileDesc), 0);
  i := 0;
  for S in AttachFiles do begin
    Attach[i].nPosition := Cardinal($FFFFFFFF);
    Attach[i].lpszPathName := PAnsiChar(UTF8String(S));
    Inc(i);
  end;
  Msg.nFileCount := Length(AttachFiles);

  if Msg.nFileCount = 0 then
    Msg.lpFiles := nil
  else
    Msg.lpFiles := @Attach[0];

  MAPIModule := LoadLibrary(PChar(MAPIDLL));

  if MAPIModule <> 0 then begin
    try
      @SMTP := GetProcAddress(MAPIModule, 'MAPISendMail');
      if @SMTP <> nil then
        Result := SMTP(0, Application.Handle, Msg, 0, 0) = SUCCESS_SUCCESS;
    finally
      FreeLibrary(MAPIModule);
    end;
  end;
end;

end.

enter image description here

当我点击Button1时,它会提示确认对话框作为打印屏幕。我的问题是如何立即发送邮件而不提示确认对话框?这可以实现吗?

最佳答案

这很容易。不要使用MAPI。 :-)

恶意软件/ spy 软件会在没有用户干预的情况下通过 MAPI 发送邮件,因此 Windows 会阻止它以防止这种情况发生。您无法绕过该安全措施,因为它是专门为防止您这样做而添加的。

想象一下,如果你可以的话,你正在写一些糟糕的东西。您可以扫描用户的计算机,抓取任何您想要的文件(例如财务文档、个人信息等),并在未经用户许可的情况下通过电子邮件将其发送到任何地方。或者您可以开始从用户的电子邮件帐户发送病毒和垃圾邮件。哎呀!您正在做的事情与过去一模一样,这就是为什么现在会询问用户是否通过 MAPI 发送电子邮件。

您可以让用户为您的软件配置其传出帐户,然后使用 TIdSMTP(或任何其他 SMTP 组件)通过该帐户发送邮件。这允许用户确认您的应用程序将发送邮件,并提供电子邮件所需的任何凭据(服务器名称、电子邮件帐户)和安全信息(用户名、密码等)。

关于delphi - 如何立即发送邮件而不提示确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17376523/

相关文章:

Delphi:泛型后代的泛型列表并以泛型作为参数

delphi - 有没有办法在Windows 2000上安装Delphi 2010

delphi - 如何检查特定用户是否对 Delphi 中的文件夹/文件具有特定访问权限

mysql - DBX 错误 : Driver could not be properly initialised

php - 使用 PHP 访问 Exchange 的最佳方式?

c# - 在 C# 中使用在 Delphi 7 中编译的 DLL

delphi - 如何知道项目中的所有条件定义

delphi - TObjectList出现在两个单元中

outlook - 仅在本地保存邮件属性 (Outlook)

unicode - 如何声明我的Simple MAPI提供程序DLL是Unicode并支持MapiSendMailW?