Question

Is there a cross-platform way of launching an email client from FireMonkey so that it will work on both Windows and OS/X. Under windows I can do the following:

ShellExecute(0, 'OPEN', 'mailto:somebody@somewhere.com', '', '', SW_ShowDefault);

Is there a cross-platform equivalent to launch an email client (preferably with attachments, recipient, subject, body etc.)?

Was it helpful?

Solution

As far i know there is not a cross-platform way to send a mail, but using the ShellExecute function in the windows side and the NSWorkspace.openURL for the OSX you can write your own implementation.

Try this OSX implementation.

uses
  Macapi.Foundation,
  Macapi.AppKit,
  System.SysUtils;

Procedure SendMail(const Mailto,Subject, Body:string);
var
  URL : NSURL;
  AWorkspace : NSWorkspace;
  encodedSubject,
  encodedTo,
  encodedBody    : NSString;
begin
  //NSString *encodedSubject = [NSString stringWithFormat:@"SUBJECT=%@", [subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  encodedSubject :=NSSTR(Format('SUBJECT=',[Subject])).stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding);
  //NSString *encodedBody = [NSString stringWithFormat:@"BODY=%@", [body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  encodedBody    :=NSSTR(Format('BODY=',[Body])).stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding);
  //NSString *encodedTo = [to stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  encodedTo      :=NSSTR(Mailto).stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding);
  //NSString *encodedURLString = [NSString stringWithFormat:@"mailto:%@?%@&%@", encodedTo, encodedSubject, encodedBody];
  //NSURL *mailtoURL = [NSURL URLWithString:encodedURLString];
  // [[NSWorkspace sharedWorkspace] openURL:mailtoURL];
  URL := TNSURL.Create;
  URL.initWithString(NSSTR(Format('mailto:%s?%s&%s',[ encodedTo.UTF8String, encodedSubject.UTF8String, encodedBody.UTF8String])));
  AWorkspace := TNSWorkspace.Create;
  AWorkspace.openURL(URL);
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top