Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delphi 7 - Opening default mail client via code.

Posted on 2011-03-24
9
Medium Priority
?
2,278 Views
Last Modified: 2012-05-11
Hi, I need to open the default mail client via a button click code, how can I accomplish this ?

Thanks.
0
Comment
Question by:John86a
[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
  • 3
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:Thommy
ID: 35211365
This is exactly what you want...

Sending Email Messages with Attachments using Delphi and Indy
http://delphi.about.com/od/indy/a/email-send-indy.htm
0
 

Author Comment

by:John86a
ID: 35211389
Sorry, not really, I need to open the default mail client configured in windows, that's all..
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35211631
try
unit Unit2;

interface

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

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

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses
  MAPI;

function MAPIErrorToStr(ACode: Integer): string;
begin
  Result := 'Unknown Error Code: ' + IntToStr(ACode);
  case ACode of
    MAPI_E_USER_ABORT               : Result := 'User cancelled request';
    MAPI_E_FAILURE                  : Result := 'General MAPI failure';
    MAPI_E_LOGON_FAILURE            : Result := 'Logon failure';
    MAPI_E_DISK_FULL                : Result := 'Disk full';
    MAPI_E_INSUFFICIENT_MEMORY      : Result := 'Insufficient memory';
    MAPI_E_ACCESS_DENIED            : Result := 'Access denied';
    MAPI_E_TOO_MANY_SESSIONS        : Result := 'Too many sessions';
    MAPI_E_TOO_MANY_FILES           : Result := 'Too many files open';
    MAPI_E_TOO_MANY_RECIPIENTS      : Result := 'Too many recipients';
    MAPI_E_ATTACHMENT_NOT_FOUND     : Result := 'Attachment not found';
    MAPI_E_ATTACHMENT_OPEN_FAILURE  : Result := 'Failed to open attachment';
    MAPI_E_ATTACHMENT_WRITE_FAILURE : Result := 'Failed to write attachment';
    MAPI_E_UNKNOWN_RECIPIENT        : Result := 'Unknown recipient';
    MAPI_E_BAD_RECIPTYPE            : Result := 'Invalid recipient type';
    MAPI_E_NO_MESSAGES              : Result := 'No messages';
    MAPI_E_INVALID_MESSAGE          : Result := 'Invalid message';
    MAPI_E_TEXT_TOO_LARGE           : Result := 'Text too large.';
    MAPI_E_INVALID_SESSION          : Result := 'Invalid session';
    MAPI_E_TYPE_NOT_SUPPORTED       : Result := 'Type not supported';
    MAPI_E_AMBIGUOUS_RECIPIENT      : Result := 'Ambiguous recipient';
    MAPI_E_MESSAGE_IN_USE           : Result := 'Message in use';
    MAPI_E_NETWORK_FAILURE          : Result := 'Network failure';
    MAPI_E_INVALID_EDITFIELDS       : Result := 'Invalid edit fields';
    MAPI_E_INVALID_RECIPS           : Result := 'Invalid recipients';
    MAPI_E_NOT_SUPPORTED            : Result := 'Not supported';
  end;
end;

function SendEmail(const AHandle: THandle; const ASenderName, ASenderAddress,
  AReceiverAddress, ASubject, ABody: AnsiString;
  AAttachments: TStrings): Integer;
var
  MapiMessage: TMapiMessage;
  MsgSender, MsgTarget: TMapiRecipDesc;
  //MsgAttachments: IMapiAttachments;
  PReceiverName, PReceiverAddress, PSenderName, PSenderAddress, PSubject, PBody: PAnsiChar;
  I: Integer;
begin
  FillChar(MapiMessage, SizeOf(MapiMessage), 0);
  FillChar(MsgTarget, SizeOf(MsgTarget), 0);
  FillChar(MsgSender, SizeOf(MsgSender), 0);

  //Receiver
  PReceiverAddress := PAnsiChar('SMTP:' + AReceiverAddress);
  PReceiverName := PAnsiChar(AReceiverAddress);
  MsgTarget.lpszAddress := PReceiverAddress;
  MsgTarget.lpszName := PReceiverName;
  MsgTarget.ulRecipClass := MAPI_TO;
  MapiMessage.nRecipCount := 1;
  MapiMessage.lpRecips := @MsgTarget;

  //Sender
  PSenderAddress := PAnsiChar(ASenderAddress);
  PSenderName := PAnsiChar(ASenderName);
  MsgSender.lpszAddress := PSenderAddress;
  MsgSender.lpszName := PSenderName;
  MsgSender.ulRecipClass := MAPI_ORIG;
  MapiMessage.lpOriginator := @MsgSender;

  //Subject
  PSubject := PAnsiChar(ASubject);
  MapiMessage.lpszSubject := PSubject;

  //Body
  PBody := PAnsiChar(ABody);
  MapiMessage.lpszNoteText := PBody;

  //Attachments
  {for I := AAttachments.Count -1 downto 0 do
  begin
    if not FileExists(AAttachments[I]) then
      AAttachments.Delete(I);
  end;

  if AAttachments.Count > 0 then
  begin
    MsgAttachments := TMapiAttachments.Create(AAttachments.Count);
    for I := 0 to AAttachments.Count - 1 do
      MsgAttachments.Add(AAttachments[I]);

    MapiMessage.nFileCount := MsgAttachments.Count;
    MapiMessage.lpFiles := MsgAttachments.MapiFileDesc;
  end; }

  Result := Mapi.MapiSendMail(0, 0, MapiMessage, MAPI_DIALOG or MAPI_LOGON_UI or MAPI_NEW_SESSION, 0);

  if Result <> 0 then
    raise Exception.Create(MAPIErrorToStr(Result));
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  SendEmail(Handle, 'Experts Exchange', 'sender@EE.com', 'receiver@EE.com', 'Hello', 'Test Message', nil);
end;

end.

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:John86a
ID: 35211818
Hi ewan, that's almost it, in fact what you posted is useful, but not really what I need atm.
I just need to execute the executable file of the default client, no need to send mail. I can manually set to the outlook folder, but not every one uses outlook, you see the problem?
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 750 total points
ID: 35211847
I see

Try using mailto

ShellExecute(0,'open','mailto: someone@home.org', nil, nil, 0);  //this should open an available mail client
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 750 total points
ID: 35212629
>   "I just need to execute the executable file of the default client"
If you want to open the default mail client with no address, no subject,...etc then:
uses ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShellExecute(Handle, nil, pChar('mailto:'), nil,nil, SW_SHOWNORMAL);
end;

Open in new window


But if you want to add the Addresses, Subject and Body then:
uses ShellAPI;

procedure TForm1.Button1Click(Sender: TObject);
var
  MailTo, MailCC, Subject,Body:String;
begin
  MailTo := 'email1@Server.Domain';
  MailCC := 'email2@Server.Domain';
  Subject := 'Any subject';
  Body := 'Your message goes here';
  ShellExecute(Handle, nil, pChar('mailto:'+ MailTo+'&CC='+MailCC+'?subject='+Subject+'&body='+Body), nil,nil, SW_SHOWNORMAL);
end;

Open in new window


BTW, which antivirus you are using, or you customers are using? It might seem irrelevant, but it's very relevant.
0
 

Author Closing Comment

by:John86a
ID: 35212670
None of the solutions provided did what I wanted, which was to open the inbox. But it was close enough.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35212852
Why B Grade? It just was not clear. And you could have elaborate on your requirement.
Anyways here is the code:
uses ShellApi, StrUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
  Reg : TRegistry;
  MailClient:String;
begin
  Reg:= TRegistry.Create;
  try
    Reg.RootKey:= HKEY_CLASSES_ROOT;
    Reg.OpenKey('\mailto\shell\open\command\', False);
    MailClient := Reg.ReadString('');
    MailClient:=copy(MailClient,2,posEx('"',MailClient,2)-2);
    Reg.CloseKey;
  finally
    Reg.Free;
  end;
  ShellExecute(Handle,'open',Pchar(MailClient),nil,nil,SW_SHOWNORMAL);
end;

Open in new window

0
 
LVL 24

Expert Comment

by:jimyX
ID: 35212861
To improve it more. Catch if there is no default mail client:
uses ShellApi, StrUtils;

procedure TForm1.Button1Click(Sender: TObject);
var
  Reg : TRegistry;
  MailClient:String;
begin
  Reg:= TRegistry.Create;
  try
    Reg.RootKey:= HKEY_CLASSES_ROOT;
    Reg.OpenKey('\mailto\shell\open\command\', False);
    MailClient := Reg.ReadString('');
    MailClient:=copy(MailClient,2,posEx('"',MailClient,2)-2);
    Reg.CloseKey;
  finally
    Reg.Free;
  end;
  if FileExists(MailClient) then
    ShellExecute(0,'open',Pchar(MailClient),nil,nil,SW_SHOWNORMAL)
  else
    showmessage('No default mail client.');
end;

Open in new window

0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

618 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