Jump to content

ekli mail gönderim


Recommended Posts

merhabalar

normal memo.text içindeki yazıyı  mail gönderimde sıkıntı yok ancak

ek olarak pdf de göndermek iştiyorum bunun için nasıl bir yol izlemeliyim.

  memo.text içindeki yazıyı için yazılan mail gönderme kodum:

procedure Gmail(username, password, totarget, subject, body: string);

var
  DATA: TIdMessage;
  SMTP: TIdSMTP;

begin




  SMTP := TIdSMTP.Create(nil);
  DATA := TIdMessage.Create(nil);
  SMTP.Host := 'mail.sunucuadıvar.com.tr';
  SMTP.Port := 587;
  SMTP.username := username;
  SMTP.password := password;


  DATA.From.Address := username;
  DATA.Recipients.EMailAddresses := totarget;
  DATA.subject := subject;
  DATA.body.text := body;



  SMTP.Connect;
  SMTP.Send(DATA);
  SMTP.Disconnect;
   DATA.Clear;


end;

procedure TMainForm.UniButton1Click(Sender: TObject);
begin
  mail_username := edtUSERNAME.text;
  mail_password := edtpassword.text;
  mail_to := edtto.text;
  mail_subject := edtsubject.text;
  mail_body := mmo1.text;
  //mail_body := a.pdf;

      Gmail(mail_username, mail_password, mail_to, mail_subject, mail_body);
     ShowMessage('Mail Gönderildi...');


end;

 

Link to post
Share on other sites

Ekteki modülü inceleyin işinize yarayacaktır.

Ek göndermek için bu fonsiyonları kullanabilirsiniz:

    procedure AttachHtmlFile(const AName, AFileName, AContentType, AContentID: string; ATempFile: Boolean = False);
    procedure AttachTempFile(const AName, AFileName, AContentType: string);
    procedure AttachFile(const AName, AFileName, AContentType: string);
 

Örnek kullanım:

  if UniServerModule.ConfigINI.SMTP.ConfigExists then
  begin
    EmailSenderThread := TEmailSenderThread.Create(True);
    with EmailSenderThread.SSLEmail do
    begin
      edToEmail       := TicketContract.email;
      edSubject       := BRAND_NAME + ' – I dettagli della tua prenotazione';
      edHTMLBody.Text := BodyStr;
      edUserName      := UniServerModule.ConfigINI.SMTP.UserName;
      edPassword      := UniServerModule.ConfigINI.SMTP.Password;
      edSenderEmail   := UniServerModule.ConfigINI.SMTP.SenderEmail;
      edSenderName    := UniServerModule.ConfigINI.SMTP.SenderName;
      edSMTPServer    := UniServerModule.ConfigINI.SMTP.Server;
      edSMTPPort      := UniServerModule.ConfigINI.SMTP.Port;

      if TicketContract.VisitType <> vtRemote then
        AttachHtmlFile(
          'ticket.png',
          TPath.Combine(UniServerModule.ConfigINI.QRCodeSaveDir, SysUtils.LowerCase(TicketContract.ticketId)+'.png'),
          'image/png',
          'ticket.png'
        );
 

EmailSenderThread.Start;

 

SecureMailClient.pas

Link to post
Share on other sites
procedure AttachHtmlFile(const AName, AFileName, AContentType, AContentID: string; ATempFile: Boolean = False);
    procedure AttachTempFile(const AName, AFileName, AContentType: string);
    procedure AttachFile(const AName, AFileName, AContentType: string);

bunların altındaki detaylar nelerdir pek anlayamadım 

Link to post
Share on other sites
1 hour ago, ibrahim aydın said:

bunların altındaki detaylar nelerdir pek anlayamadım

Tüm kaynak kod SecureMailClient.pas içindedir, detayları görebilirsiniz.

AttachFile('pdfdosyasiadi', 'c:\zzzzzzzz\zzz\pdfdosyasi.pdf', 'application/pdf');

Edited by Mehmet Emin
AttachFile usage
Link to post
Share on other sites

unit SecureMailClient;

interface

uses
  IdMessage, Classes, IdSMTP, IdText, IdAttachment, IdAttachmentFile,
  IdMessageParts, IdMessageBuilder, System.SysUtils, System.IOUtils,
  System.Contnrs;

const
  SMTP_PORT_EXPLICIT_TLS = 587;

type
  TSSLEmailAttachFile = class
    Name: string;
    FileName: string;
    ContentType: string;
    ContentID: string;
    TempFile: Boolean;
    HtmlRelated: Boolean;
  end;

  TSSLEmail = class(TObject)
  private
    IdMessage: TIdMessage;
    SMTP: TIdSMTP;

    FedDelaySeconds: Integer;
    FedHTMLBody: TStrings;
    FedBody: TStrings;
    FedSMTPPort: Integer;
    FedToEmail: string;
    FedSubject: string;
    FedSMTPServer: string;
    FedCCEmail: string;
    FedPassword: string;
    FedBCCEmail: string;
    FedSenderName: string;
    FedUserName: string;
    FedPriority: TIdMessagePriority;
    FedSenderEmail: string;
    FedSSLConnection: Boolean;
    FAttachFiles: TObjectList;

    // Getter / Setter
    procedure SetBody(const Value: TStrings);
    procedure SetHTMLBody(const Value: TStrings);

    procedure Init;
    procedure InitMailMessage;
    procedure InitSASL;
    procedure AddSSLHandler;

  public
    constructor Create; overload;
    constructor Create(const ASMTPServer: string; const ASMTPPort: Integer; const AUserName, APassword: string); overload;
    destructor Destroy; override;
    procedure SendEmail;
    procedure AttachHtmlFile(const AName, AFileName, AContentType, AContentID: string; ATempFile: Boolean = False);
    procedure AttachTempFile(const AName, AFileName, AContentType: string);
    procedure AttachFile(const AName, AFileName, AContentType: string);

    // Properties
    property edDelaySeconds: Integer read FedDelaySeconds write FedDelaySeconds;
    property edHTMLBody: TStrings read FedHTMLBody write SetHTMLBody;
    property edBCCEmail: string read FedBCCEmail write FedBCCEmail;
    property edBody: TStrings read FedBody write SetBody;
    property edCCEmail: string read FedCCEmail write FedCCEmail;
    property edPassword: string read FedPassword write FedPassword;
    property edPriority: TIdMessagePriority read FedPriority write FedPriority;
    property edSenderEmail: string read FedSenderEmail write FedSenderEmail;
    property edSenderName: string read FedSenderName write FedSenderName;
    property edSMTPServer: string read FedSMTPServer write FedSMTPServer;
    property edSMTPPort: Integer read FedSMTPPort write FedSMTPPort;
    property edSSLConnection: Boolean read FedSSLConnection write FedSSLConnection;
    property edToEmail: string read FedToEmail write FedToEmail;
    property edUserName: string read FedUserName write FedUserName;
    property edSubject: string read FedSubject write FedSubject;
  end;

  TEmailSenderThread = class(TThread)
  private
    FSSLEmail: TSSLEmail;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean); reintroduce;
    destructor Destroy; override;
    property SSLEmail: TSSLEmail read FSSLEmail;
  end;

implementation

uses
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdMessageClient, IdSMTPBase, IdBaseComponent, IdIOHandler,
  IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdSASLLogin,
  IdSASL_CRAM_SHA1, IdSASL, IdSASLUserPass, IdSASL_CRAMBase, IdSASL_CRAM_MD5,
  IdSASLSKey, IdSASLPlain, IdSASLOTP, IdSASLExternal, IdSASLDigest,
  IdSASLAnonymous, IdUserPassProvider;

constructor TSSLEmail.Create;
begin
  inherited;
  Init;
  FedBody := TStringList.Create;
  FedHTMLBody := TStringList.Create;
  FAttachFiles := TObjectList.Create;
end;

procedure TSSLEmail.Init;
begin
  edSSLConnection := True;
  edPriority := TIdMessagePriority.mpNormal;
end;

procedure TSSLEmail.AttachHtmlFile(const AName, AFileName, AContentType, AContentID: string; ATempFile: Boolean);
var
  LAttachFile: TSSLEmailAttachFile;
begin
  LAttachFile := TSSLEmailAttachFile.Create;
  with LAttachFile do
  begin
    Name        := AName;
    FileName    := AFileName;
    ContentType := AContentType;
    ContentID   := AContentID;
    TempFile    := ATempFile;
    HtmlRelated := True;
  end;
  FAttachFiles.Add(LAttachFile);
end;

procedure TSSLEmail.AttachTempFile(const AName, AFileName, AContentType: string);
var
  LAttachFile: TSSLEmailAttachFile;
begin
  LAttachFile := TSSLEmailAttachFile.Create;
  with LAttachFile do
  begin
    Name        := AName;
    FileName    := AFileName;
    ContentType := AContentType;
    ContentID   := '';
    TempFile    := True;
    HtmlRelated := False;
  end;
  FAttachFiles.Add(LAttachFile);
end;

procedure TSSLEmail.AttachFile(const AName, AFileName, AContentType: string);
var
  LAttachFile: TSSLEmailAttachFile;
begin
  LAttachFile := TSSLEmailAttachFile.Create;
  with LAttachFile do
  begin
    Name        := AName;
    FileName    := AFileName;
    ContentType := AContentType;
    ContentID   := '';
    TempFile    := False;
    HtmlRelated := False;
  end;
  FAttachFiles.Add(LAttachFile);
end;

constructor TSSLEmail.Create(const ASMTPServer: string;
  const ASMTPPort: Integer; const AUserName, APassword: string);
begin
  Create;

  edSMTPServer := ASMTPServer;
  edSMTPPort := ASMTPPort;
  edUserName := AUserName;
  edPassword := APassword;
end;

destructor TSSLEmail.Destroy;
begin
  FAttachFiles.Free;
  edHTMLBody.Free;
  edBody.Free;
  inherited;
end;

// Setter / Getter -----------------------------------------------------------

procedure TSSLEmail.SetBody(const Value: TStrings);
begin
  FedBody.Assign(Value);
end;

procedure TSSLEmail.SetHTMLBody(const Value: TStrings);
begin
  FedHTMLBody.Assign(Value);
end;

// Send the mail -------------------------------------------------------------

procedure TSSLEmail.SendEmail;
var
  I: Integer;
  LAttachFile: TSSLEmailAttachFile;
begin
  IdMessage := TIdMessage.Create;
  try
    InitMailMessage;

    SMTP := TIdSMTP.Create;
    try
      if edSSLConnection then
      begin
        AddSSLHandler;

        if edSMTPPort = SMTP_PORT_EXPLICIT_TLS then
          SMTP.UseTLS := utUseExplicitTLS
        else
          SMTP.UseTLS := utUseImplicitTLS;
      end;

      if (edUserName<>'') or (edPassword<>'') then
      begin
        SMTP.AuthType := satSASL;
        InitSASL;
      end
      else
      begin
        SMTP.AuthType := satNone;
      end;

      SMTP.Host := edSMTPServer;
      SMTP.Port := edSMTPPort;
      SMTP.ConnectTimeout := 30000;
      SMTP.UseEHLO := True;
      SMTP.Connect;

      try
        SMTP.Send(IdMessage);
        for I := 0 to FAttachFiles.Count -1 do
        begin
          LAttachFile := TSSLEmailAttachFile(FAttachFiles[I]);
          if LAttachFile.TempFile then
          begin
            if TFile.Exists(LAttachFile.FileName) then
              TFile.Delete(LAttachFile.FileName);
          end;
        end;
      finally
        SMTP.Disconnect;
      end;
    finally
      SMTP.Free;
    end;
  finally
    IdMessage.Free;
  end;
end;

// Prepare the mail ----------------------------------------------------------

procedure TSSLEmail.InitMailMessage;
var
  I: Integer;
  FMessageBuilder: TIdMessageBuilderHtml;
  FAttachment: TIdMessageBuilderAttachment;
  LAttachFile: TSSLEmailAttachFile;
begin
  FMessageBuilder := TIdMessageBuilderHtml.Create;
  try
    if edBody.Text <> '' then FMessageBuilder.PlainText.Text := edBody.Text;
    if edHTMLBody.Text <> '' then FMessageBuilder.Html.Text := edHTMLBody.Text;
    IdMessage.Sender.Text := edSenderEMail;
    IdMessage.From.Name := edSenderName;
    IdMessage.From.Address := edSenderEMail;
    IdMessage.ReplyTo.EMailAddresses := edSenderEmail;
    IdMessage.Recipients.EMailAddresses := edToEmail;
    IdMessage.Subject := edSubject;
    IdMessage.Priority := edPriority;
    IdMessage.CCList.EMailAddresses := edCCEMail;
    IdMessage.ReceiptRecipient.Text := '';
    IdMessage.BccList.EMailAddresses := edBCCEMail;

    for I := 0 to FAttachFiles.Count -1 do
    begin
      LAttachFile := TSSLEmailAttachFile(FAttachFiles[I]);
      if LAttachFile.HtmlRelated then
        FAttachment := FMessageBuilder.HtmlFiles.Add(LAttachFile.FileName, LAttachFile.ContentID)
      else
        FAttachment := FMessageBuilder.Attachments.Add(LAttachFile.FileName, LAttachFile.ContentID);
      FAttachment.Name := LAttachFile.Name;
      FAttachment.ContentType := LAttachFile.ContentType;
    end;
    FMessageBuilder.FillMessage(IdMessage);

    for I := 0 to IdMessage.MessageParts.Count-1 do
    begin
      if IdMessage.MessageParts[I].PartType = mptAttachment then
      begin
        IdMessage.MessageParts[I].FileName    := IdMessage.MessageParts[I].Name;
        IdMessage.MessageParts[I].ContentType := IdMessage.MessageParts[I].ContentType;
        IdMessage.MessageParts[I].ContentID   := IdMessage.MessageParts[I].ContentID;
      end;
    end;
    IdMessage.CharSet := 'UTF8';
  finally
    FMessageBuilder.Free;
  end;
end;

procedure TSSLEmail.AddSSLHandler;
var
  SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
  SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(SMTP);
  SSLHandler.SSLOptions.Method := sslvSSLv23;
  SSLHandler.SSLOptions.Mode := sslmClient;
  SSLHandler.SSLOptions.VerifyMode := [];
  SSLHandler.SSLOptions.VerifyDepth := 0;
  SMTP.IOHandler := SSLHandler;
end;

procedure TSSLEmail.InitSASL;
var
  IdUserPassProvider: TIdUserPassProvider;
  IdSASLCRAMMD5: TIdSASLCRAMMD5;
  IdSASLCRAMSHA1: TIdSASLCRAMSHA1;
  IdSASLPlain: TIdSASLPlain;
  IdSASLLogin: TIdSASLLogin;
  IdSASLSKey: TIdSASLSKey;
  IdSASLOTP: TIdSASLOTP;
  IdSASLAnonymous: TIdSASLAnonymous;
  IdSASLExternal: TIdSASLExternal;
begin
  IdUserPassProvider := TIdUserPassProvider.Create(SMTP);
  IdUserPassProvider.Username := edUserName;
  IdUserPassProvider.Password:= edPassword;

  IdSASLCRAMSHA1 := TIdSASLCRAMSHA1.Create(SMTP);
  IdSASLCRAMSHA1.UserPassProvider := IdUserPassProvider;
  IdSASLCRAMMD5 := TIdSASLCRAMMD5.Create(SMTP);
  IdSASLCRAMMD5.UserPassProvider := IdUserPassProvider;
  IdSASLSKey := TIdSASLSKey.Create(SMTP);
  IdSASLSKey.UserPassProvider := IdUserPassProvider;
  IdSASLOTP := TIdSASLOTP.Create(SMTP);
  IdSASLOTP.UserPassProvider := IdUserPassProvider;
  IdSASLAnonymous := TIdSASLAnonymous.Create(SMTP);
  IdSASLExternal := TIdSASLExternal.Create(SMTP);
  IdSASLLogin := TIdSASLLogin.Create(SMTP);
  IdSASLLogin.UserPassProvider := IdUserPassProvider;
  IdSASLPlain := TIdSASLPlain.Create(SMTP);
  IdSASLPlain.UserPassProvider := IdUserPassProvider;

  SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMSHA1;
  SMTP.SASLMechanisms.Add.SASL := IdSASLCRAMMD5;
  SMTP.SASLMechanisms.Add.SASL := IdSASLSKey;
  SMTP.SASLMechanisms.Add.SASL := IdSASLOTP;
  SMTP.SASLMechanisms.Add.SASL := IdSASLAnonymous;
  SMTP.SASLMechanisms.Add.SASL := IdSASLExternal;
  SMTP.SASLMechanisms.Add.SASL := IdSASLLogin;
  SMTP.SASLMechanisms.Add.SASL := IdSASLPlain;
end;

{ TEmailSenderThread }

constructor TEmailSenderThread.Create(CreateSuspended: Boolean);
begin
  inherited Create(CreateSuspended);
  FreeOnTerminate := True;
  FSSLEmail := TSSLEmail.Create;
end;

destructor TEmailSenderThread.Destroy;
begin
  FSSLEmail.Free;
  inherited;
end;

procedure TEmailSenderThread.Execute;
begin
  Sleep(FSSLEmail.FedDelaySeconds*1000);
  if not Terminated then
    FSSLEmail.SendEmail;
end;

end.

Link to post
Share on other sites

Rica ederim.

Ekli mail gönderimi haricinde, AttachHtmlFile foksiyonu da aşağıdaki şekilde kullanılabilir:

HTML formatındaki içeriğe örneğin bir logo gömmek istersek.

    BodyStr :=
      '<html><head></head>'+
      '<body>'+
      '<p><img src="cid:ticket.png"></p>'+
      '</body>'+
      '</html>

        AttachHtmlFile(
          'ticket.png',
          '\xxxxxxxxxx\xxxxxxxxx\ticket.png',
          'image/png',
          'ticket.png');

//AContentID parametresi HTML kodu içerisinde cid:ticket.png olarak referans veriliyor.

procedure TSSLEmail.AttachHtmlFile(const AName, AFileName, AContentType, AContentID: string; ATempFile: Boolean);

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...