0

I have been using ShellExecute to generate 5 or 6 emails in one go using the Windows default mail client:

ShellExecute(Self.Handle, nil, Pchar(email), nil, nil, SW_NORMAL);

This then allows the user to decide if they want to send them or not.

The issue I am having is if the email body text is too long, it gets truncated in the created email. I presume this is a limitation of creating emails in this way.

I have looked at switching the way I generate the emails to using MAPI instead, but the issue I found is that all the MAPI routines or components I have tried seem to only allow me to create one email at a time. The end user then has to decide if they want to send it or not before the next one is generated.

Is there either a fix for ShellExecute's length issue, or is there a way to still generate a batch of emails in one go using MAPI or another way? I would like to use the default Windows email client to dipslay the messages.

I am using Delphi 7.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
colin
  • 2,983
  • 6
  • 41
  • 49
  • I think your approach is difficult. I'd create my own dialog with the appearance of an email composition window, display the proposed email there for approval (which allows as many as you want to be presented at the same time), and then actually send each of them using MAPI when the user clicks a send button. It vastly simplifies things from a programming standpoint. – Ken White Jun 22 '12 at 01:19
  • @Ken Thanks, I ended up taking your approach. – colin Jul 02 '12 at 11:47

1 Answers1

0

Im using JclMapi.pas to send e-mails using MAPI. I wrote a class to make easy to use the TJclEmail class.

The code to send email is:

class function TMAPISendMail.Execute(const Name, EmailTo, Subject, Body: string; Attachment: TStrings): boolean;
var
  I: Integer;
  Email: string;
  MAPISendMail: TMAPISendMail;
begin
  MAPISendMail:= TMAPISendMail.Create;
  try
    I:= 1;
    while I > 0 do
    begin
      Email:= TString.Token(EmailTo, ';', I);
      if Email <> '' then
        MAPISendMail.AddRecipient(Email, Name);
    end;
    for I := 0 to Attachment.Count - 1 do
      MAPISendMail.AddAttachment(Attachment[I]);
    MAPISendMail.MailSubject:= Subject;
    MAPISendMail.HTMLBody:= False;
    MAPISendMail.MailBody:= Body;
    Result:= MAPISendMail.SendMail;
  finally
    MAPISendMail.Free;
  end;
end;

And the class interface is:

 TMAPISendMail = class
  private
    FAJclEmail: TJclEmail;
    FPrerequisites: TPrerequisites;
    FResolveNames: Boolean;
    FShowDialog: Boolean;
    [...]
  protected
    function DoSendMail: Boolean; virtual;
  public
    constructor Create;
    destructor Destroy; override;
    class function Execute(const Name, EmailTo, Subject, Body: string; Attachment: TStrings): boolean; static;
    property MailBody: string read GetMailBody write SetMailBody;
    property HTMLBody: Boolean read GetHTMLBody write SetHTMLBody;
    property ShowDialog: Boolean read FShowDialog write FShowDialog;
    property MailSubject: string read GetMailSubject write SetMailSubject;
    property ResolveNames: Boolean read FResolveNames write FResolveNames;
    property Prerequisites: TPrerequisites read FPrerequisites;
    procedure AddRecipient(const Address: string; const Name: string = '');
    procedure AddAttachment(const FileName: string);
    function SendMail: Boolean;
  end;

Hope it can be useful for you.

Check this thread too: Sending Email with signature

Community
  • 1
  • 1
Cesar Romero
  • 4,027
  • 1
  • 25
  • 44
  • This is the same suggestion I made earlier (in an answer now deleted), because it doesn't meet the requirements. (The poster wants multiple email composition windows to appear at the same time, so the user can read and decide whether to send them or not. The `MAPI` send mail function you're using pauses after each message until the window is closed, which isn't what's being asked. I also wrote the answer you linked to in your final paragraph. ) – Ken White Jun 22 '12 at 01:17