How to immediate send mail without prompt confirmation dialog

Posted by lmengyew on Stack Overflow See other posts from Stack Overflow or by lmengyew
Published on 2013-06-29T03:52:57Z Indexed on 2013/06/29 4:21 UTC
Read the original article Hit count: 406

Filed under:
|
|

I'm using Delphi XE3 and below is my sample application:

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('', '[email protected]', '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

When i click the Button1, it will prompt the confirmation dialog as print screen. My question is how to immediate send mail without prompt the confirmation dialog? Is this possible to achieve?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-xe3