Puoi usare le funzioni MAPI oppure l'invio diretto tramite il protocollo SMTP.
Sono entrambe soluzioni relativamente semplici e ben documentate.
Unit per MAPI, fa uso di JVCL e JCL:
unit MAPI.SendMail;
interface
uses
SysUtils, Classes, JclMapi;
type
TPrerequisites = class
public
function IsMapiAvailable: Boolean;
function IsClientAvailable: Boolean;
end;
TMAPISendMail = class
private
FAJclEmail: TJclEmail;
FShowDialog: Boolean;
FResolveNames: Boolean;
FPrerequisites: TPrerequisites;
// proxy property getters
function GetMailBody: string;
function GetHTMLBody: Boolean;
function GetMailSubject: string;
// proxy property setters
procedure SetMailBody(const Value: string);
procedure SetHTMLBody(const Value: Boolean);
procedure SetMailSubject(const Value: string);
protected
function DoSendMail: Boolean; virtual;
public
constructor Create;
destructor Destroy; override;
// properties of the wrapper class
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 and functions of the wrapper class
procedure AddRecipient(const Address: string; const Name: string = '');
procedure AddAttachment(const FileName: string);
function SendMail: Boolean;
end;
implementation
{ TMAPISendMail }
constructor TMAPISendMail.Create;
begin
FPrerequisites := TPrerequisites.Create;
FAJclEmail := TJclEmail.Create;
FShowDialog := True;
end;
destructor TMAPISendMail.Destroy;
begin
FreeAndNil(FAJclEmail);
FreeAndNil(FPrerequisites);
inherited;
end;
function TMAPISendMail.DoSendMail: Boolean;
begin
Result := FAJclEmail.Send(FShowDialog);
end;
function TMAPISendMail.SendMail: Boolean;
begin
Result := DoSendMail;
end;
function TMAPISendMail.GetMailBody: string;
begin
Result := FAJclEmail.Body;
end;
procedure TMAPISendMail.SetMailBody(const Value: string);
begin
FAJclEmail.Body := Value;
end;
procedure TMAPISendMail.AddAttachment(const FileName: string);
begin
FAJclEmail.Attachments.Add(FileName);
end;
procedure TMAPISendMail.AddRecipient(const Address, Name: string);
var
LocalName: string;
LocalAddress: string;
begin
LocalAddress := Address;
LocalName := Name;
if FResolveNames then
if not FAJclEmail.ResolveName(LocalName, LocalAddress) then
raise Exception.Create('Could not resolve Recipient name and address!');
FAJclEmail.Recipients.Add(LocalAddress, LocalName);
end;
function TMAPISendMail.GetMailSubject: string;
begin
Result := FAJclEmail.Subject;
end;
procedure TMAPISendMail.SetMailSubject(const Value: string);
begin
FAJclEmail.Subject := Value;
end;
function TMAPISendMail.GetHTMLBody: Boolean;
begin
Result := FAJclEmail.HtmlBody;
end;
procedure TMAPISendMail.SetHTMLBody(const Value: Boolean);
begin
FAJclEmail.HtmlBody := Value;
end;
{ TPrerequisites }
function TPrerequisites.IsClientAvailable: Boolean;
var
SimpleMAPI: TJclSimpleMapi;
begin
SimpleMAPI := TJclSimpleMapi.Create;
try
Result := SimpleMAPI.AnyClientInstalled;
finally
FreeAndNil(SimpleMAPI);
end;
end;
function TPrerequisites.IsMapiAvailable: Boolean;
var
SimpleMAPI: TJclSimpleMapi;
begin
SimpleMAPI := TJclSimpleMapi.Create;
try
Result := SimpleMAPI.SimpleMapiInstalled;
finally
FreeAndNil(SimpleMAPI);
end;
end;
end.
Procedure TFFatture.InviaperEmailEx(Const NomePdf:string);
Var
MAPISendMail : TMAPISendMail;
ep : TEnumParams;
t : TDateTime;
begin
MAPISendMail := TMAPISendMail.Create;
try
ep.Name := NumeroFatturaEx(TTestaFatTipo.Value, TTestaFatAnno.Value, TTestaFatNumero.Value, TTestaFatSerie.Value);
if MAPISendMail.Prerequisites.IsMapiAvailable and MAPISendMail.Prerequisites.IsClientAvailable then begin
if (TTestaFatIntestatario.Value > 0) then
MAPISendMail.AddRecipient(Darc.TAnagrafLookXA1.Value, '');
MAPISendMail.AddAttachment(nomepdf);
MAPISendMail.MailSubject := ep.Name;
MAPISendMail.HTMLBody := False;
MAPISendMail.MailBody := ParseAndFormat(cfgEmailTesto,
TTestaFatTipo.Value,
NumeroFattura(TTestaFatTipo.Value, TTestaFatAnno.Value, TTestaFatNumero.Value, TTestaFatSerie.Value),
TTestaFatData.AsString
);
MAPISendMail.SendMail;
end;
t := Now;
while (Now - t) * 86400 < 3 do //attendo un po' prima di cercare la finestra!
Application.ProcessMessages;
ep.Thunder := 0;
if not EnumWindows(@EnumWindowsCallBack, LParam(@ep)) then begin
if ep.Thunder <> 0 then
SetForegroundWindow(ep.Thunder);
end else begin
Application.MessageBox('Funzione non disponibile. Contatta l''assistenza.', 'Attenzione:', MB_ICONERROR);
end;
except
on E:Exception do Application.MessageBox(PChar(E.Message), 'InviaPerEmail', MB_OK);
end;
FreeAndNil(MAPISendMail);
end; //InviaperEmailEx
Per l'invio diretto ci sono diverse librerie, io uso Synapse.