Você está na página 1de 7

1.

Mengirim Email dengan Delphi


Delphi - Tips dan Trik Delphi
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
edtSubject: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
edtNamaPengirim: TEdit;
edtEmailPengirim: TEdit;
Label4: TLabel;
edtNamaPenerima: TEdit;
edtEmailPenerima: TEdit;
Label5: TLabel;
MemoMessage: TMemo;
Label6: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses MAPI;

{$R *.dfm}

function SendMail(const Subject, Body, FileName,


SenderName, SenderEMail,
RecipientName, RecipientEMail: string): Integer;
var
Message: TMapiMessage;
lpSender, lpRecipient: TMapiRecipDesc;
FileAttach: TMapiFileDesc;
SM: TFNMapiSendMail;
MAPIModule: HModule;
begin
FillChar(Message, SizeOf(Message), 0);
with Message do
begin
if (Subject <> '') then
lpszSubject := PChar(Subject);
if (Body <> '') then
lpszNoteText := PChar(Body);
if (SenderEmail <> '') then
begin
lpSender.ulRecipClass := MAPI_ORIG;
if (SenderName = '') then
lpSender.lpszName := PChar(SenderEMail)
else
lpSender.lpszName := PChar(SenderName);
lpSender.lpszAddress := PChar(SenderEmail);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := @lpSender;
end;
if (RecipientEmail <> '') then
begin
lpRecipient.ulRecipClass := MAPI_TO;
if (RecipientName = '') then
lpRecipient.lpszName := PChar(RecipientEMail)
else
lpRecipient.lpszName := PChar(RecipientName);
lpRecipient.lpszAddress := PChar(RecipientEmail);
lpRecipient.ulReserved := 0;
lpRecipient.ulEIDSize := 0;
lpRecipient.lpEntryID := nil;
nRecipCount := 1;
lpRecips := @lpRecipient;
end
else lpRecips := nil;
if (FileName = '') then
begin
nFileCount := 0;
lpFiles := nil;
end
else
begin
FillChar(FileAttach, SizeOf(FileAttach), 0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(FileName);
nFileCount := 1;
lpFiles := @FileAttach;
end;
end;
MAPIModule := LoadLibrary(PChar(MAPIDLL));
if MAPIModule = 0 then
Result := -1
else
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
Result := 1;
finally
FreeLibrary(MAPIModule);
end;
if Result <> 0 then
MessageDlg('Gagal mengirim email (' + IntToStr(Result) + ').', mtError, [mbOK], 0);
end;

procedure TForm1.Button1Click(Sender: TObject);


begin
SendMail(edtSubject.Text,
MemoMessage.Text,
'',
edtNamaPengirim.Text, edtEmailPengirim.Text,
edtNamaPenerima.Text, edtEmailPenerima.Text);
end;

end.
2.Kirim email lewat Delphi (new)

seperti pada postingan saya sebelumnya, pastikan Ms office Sudah terinstall dengan baik di
komputer kamu, dah lanjut…

yang harus dibuat adalah sebagai berikut ,perhatikan gambar :

jadi ketika Button 1 di klik, maka akan


mengarahkan langsung kita ke Ms Outlook, perhatikan kodenya :

unit unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls,ShellApi;


type
TForm1 = class(TForm)
//maksudnya menggunakan 1 button dan 1 label
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
//aksi ketika button 1 di klik
procedure TForm1.Button1Click(Sender: TObject);
var
strTujuan, strSubject, strIsi,Param : String;

begin
//tujuan email
strTujuan := ‘invertmotion@Gmail.com’;
//subjek email
strSubject := ‘Subject’;
//isi email
strIsi := ‘Isi Text’;

Param :=’mailto:’ + strTujuan + ‘?subject=’ + strSubject +’&Body=’ +    strIsi;

ShellExecute(Form1.handle,’open’,PChar(Param),nil,nil,SW_SHOWNORMAL);

end;

end.
3.Kirim email HTML Pakai Delphi dan Komponent Indy, dengan
Gambar yang di Embedded di Body

contoh ini untuk mengirim gambar yang di embeded di body email pakai pemorograman delphi
dan indy component.

uses
idMessage;

procedure TForm1.Button1Click(Sender: TObject);


var
html: TStrings;
htmpart, txtpart: TIdText;
bmppart: TIdAttachment;
email: TIdMessage;
filename: string;
begin
filename := ExtractFilePath(Application.ExeName) + ‘us.jpg’;

html := TStringList.Create();
html.Add(‘< html >’);
html.Add(‘< head >’);
html.Add(‘< /head >’);
html.Add(‘< body >< h1 >Hello< /h1 >’);
html.Add(‘< img src=”cid:us.jpg” />’);
html.Add(‘This is a picture of us!< /body >’);
html.Add(‘< /html >’);

email := TIdMessage.Create(nil);
email.From.Text := ‘pengirim@email’;
email.Recipients.EMailAddresses := ‘penerima@email’;
email.Subject := ‘Hello’;
email.ContentType := ‘multipart/mixed’;
email.Body.Assign(html);

txtpart := TIdText.Create(email.MessageParts);
txtpart.ContentType := ‘text/plain’;
txtpart.Body.Text := ”;

htmpart := TIdText.Create(email.MessageParts, html);


htmpart.ContentType := ‘text/html’;

bmppart := TIdAttachment.Create(email.MessageParts, filename);


bmppart.ContentType := ‘image/jpeg’;
bmppart.FileIsTempFile := true;
bmppart.ContentDisposition := ‘inline’;
bmppart.ExtraHeaders.Values['content-id'] := ‘us.jpg’;
bmppart.DisplayName := ‘us.jpg’;

try
idSMTP.Connect();
try
idSMTP.Send(email);
ShowMessage(‘Sent’);
except
on E: Exception do
ShowMessage(‘Failed: ‘ + E.Message);
end;
finally
idSMTP.Disconnect();
email.Free();
html.Free();
end;
end;

Você também pode gostar