?
Solved

delphi 2010 and mapi auto send

Posted on 2011-02-17
3
Medium Priority
?
1,119 Views
Last Modified: 2012-08-13
Using the code below I can generate an email but I would like to send it without any human intervention does anybody know how please

unit PromptMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, shellapi, mapi;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function SendMailMAPI(const strSubject, strBody, strFileName, strSenderName, strSenderEMail,
                  strRecepientName, strRecepientEMail: ansiString) : Integer;
var
  message: TMapiMessage;           //changing string->ansiString, char to ansiChar pChar to pAnsiChar
  lpSender,
  lpRecepient: TMapiRecipDesc;
  FileAttach: TMapiFileDesc;
  SM: TFNMapiSendMail;
  MAPIModule: HModule;
begin
  FillChar(message, SizeOf(message), 0);
  with message do
  begin
    if (strSubject<>'') then
    begin
      lpszSubject := pAnsiChar(strSubject)
    end;
    if (strBody<>'') then
    begin
      lpszNoteText := PAnsiChar(strBody)
    end;
    if (strSenderEMail<>'') then
    begin
      lpSender.ulRecipClass := MAPI_ORIG;
      if (strSenderName='') then
      begin
        lpSender.lpszName := PAnsiChar(strSenderEMail)
      end
      else
      begin
        lpSender.lpszName := PAnsiChar(strSenderName)
      end;
      lpSender.lpszAddress := PAnsiChar('SMTP:'+strSenderEMail);
      lpSender.ulReserved := 0;
      lpSender.ulEIDSize := 0;
      lpSender.lpEntryID := nil;
      lpOriginator := @lpSender;
    end;
    if (strRecepientEMail<>'') then
    begin
      lpRecepient.ulRecipClass := MAPI_TO;
      if (strRecepientName='') then
      begin
        lpRecepient.lpszName := PAnsiChar(strRecepientEMail)
      end
      else
      begin
        lpRecepient.lpszName := PAnsiChar(strRecepientName)
      end;
      lpRecepient.lpszAddress := PAnsiChar('SMTP:'+strRecepientEMail);
      lpRecepient.ulReserved := 0;
      lpRecepient.ulEIDSize := 0;
      lpRecepient.lpEntryID := nil;
      nRecipCount := 1;
      lpRecips := @lpRecepient;
    end
    else
    begin
      lpRecips := nil
    end;
    if (strFileName='') then
    begin
      nFileCount := 0;
      lpFiles := nil;
    end
    else
    begin
      FillChar(FileAttach, SizeOf(FileAttach), 0);
      FileAttach.nPosition := Cardinal($FFFFFFFF);
      FileAttach.lpszPathName := PAnsiChar(strFileName);
      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;
end;
////////////////////////////////////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
var
  strFileName,
  strBody,
  strRecepientName,
  strRecepientEMail,
  strSenderName,
  strSenderEMail,
  strSubject : string;
begin

  strRecepientName := 'Chris';
  strRecepientEMail := 'email address';
  strSenderName := 'name';
  strSenderEMail := 'email address';
  strSubject := 'message';

  strBody := 'generated by me';
  SendMailMAPI(strSubject,strBody,strFileName,strSenderName,strSenderEMail,strRecepientName,strRecepientEMail);
end;

end.
0
Comment
Question by:CtexAndy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Expert Comment

by:pasolo
ID: 34920527
As far as I notice all human intervention you have is click the button.
But you can make it self click by calling Button1Click(self) from another method.
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 34920840

Not very sure what you mean by user intervention, Are you trying to use MAPI without showing the dialog?

Then why not just use TIdSMTP component
Much simpler to use and there are no dialogs, message is just sent straight to server
0
 

Author Comment

by:CtexAndy
ID: 34924026
thanks ewangoya: top tip it works a treat using TIdSMTP. MAPI started outlook and required a send button press to work but this solution talks direct to the mail server.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question