![]() |
|
Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - Baskı Önizleme +- Delphi Can (https://www.delphican.com) +-- Forum: Delphi (https://www.delphican.com/forumdisplay.php?fid=3) +--- Forum: Eğitim (https://www.delphican.com/forumdisplay.php?fid=19) +---- Forum: Makale (https://www.delphican.com/forumdisplay.php?fid=20) +---- Konu Başlığı: Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) (/showthread.php?tid=1734) Sayfalar:
1
2
|
Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - csunguray - 27-12-2017 Daha önce bir başka forumda paylaşmış olduğum makale ve örneği daha bulunabilir olması açısından burada da paylaşmak istiyorum. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) (text, html, gömülü resim) Delphi'de Indy SMTP ile e-mail (e-posta) gönderme konusunda bir örnek hazırladım. Bu örnekte GMail, Yandex, Yahoo gibi servis sağlayıcılarla, text (metin) olarak, html biçiminde ve gömülü resim içeren html biçiminde e-posta gönderme örnekleri de yaptım. Örneği Indy 10 ve Delphi XE2 kullanarak hazırladım. Progam Delphi 7 ve Delphi XE8'de de derleniyor ve çalışıyor. Delphi 7'de unit isimleri önündeki Vcl. ön ekini silmelisiniz. Ayrıca Codepage i utf8 yerine iso8859-9 olarak ayarlarsanız Türkçe harfler doğru çıkacaktır. [Ars=delphi]EPosta.CharSet := 'iso-8859-9';[/ars] Sisteminizde Indy'nin eski sürümü yüklüyse örneği kolaylıkla aşağıdaki bağlantılarda anlatılanlara göre düzenleyebilirsiniz. http://delphi.xcjc.net/viewthread.php?tid=45523 http://www.cyberforum.ru/delphi-networks/thread1108825.html SLL veya TLS ile şifrelenmiş e-postalar göndermek için (ki bu gmail, yahoo ve yandex için zorunludur) OpenSSl kütüphanesine ait libeay32.dll ve ssleay32.dll dosyalarının programın olduğu klasörde veya sistem klasörlerinden birinde olması gerekir. Bu dosyaları http://indy.fulgan.com/SSL/ bağlantısından veya https://slproweb.com/products/Win32OpenSSL.html bağlantısından indirebilirsiniz. Sisteminiz 64 bit olsa bile projenizi 32 bit derliyorsanız (Delphi 7 sadece 32 bit exe ler üretir) ssleay32.dll ve libeay32.dll dosyalarının 32 bit olan sürümünü kullanmalısınız. Umarım Delphi ile e-posta göndermede sorun yaşayan arkadaşlara yardımcı olur. [Ars=delphi] {*******************************************************} { } { Proje: SMTP Test } { Modül: } { Yazar: C. Sunguray } { Son Değ.: } { } {*******************************************************} unit SmtpF; interface uses Windows, Messages, SysUtils, Variants, Classes, IdIOHandler, Forms, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdMessage, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdMessageClient, IdSMTPBase, IdSMTP, Vcl.StdCtrls, Vcl.Controls, Vcl.Dialogs; type TFormSmtpF = class(TForm) SMTP: TIdSMTP; EPosta: TIdMessage; LHandler: TIdSSLIOHandlerSocketOpenSSL; GroupBox2: TGroupBox; GroupBox3: TGroupBox; MemoTestIcerik: TMemo; GroupBox4: TGroupBox; MemoLog: TMemo; Label1: TLabel; EditAlici: TEdit; EditYandex: TButton; EditGoogle: TButton; ButtonYahoo: TButton; Button4: TButton; Label2: TLabel; Label8: TLabel; Label3: TLabel; Label4: TLabel; Label10: TLabel; EditHOST: TEdit; EditUSER: TEdit; EditPASSWORD: TEdit; EditCRYPTION: TComboBox; EditPORT_NO: TComboBox; CheckBoxSifreGoster: TCheckBox; GroupBox1: TGroupBox; MemoAciklama: TMemo; ButtonMetinGonder: TButton; ButtonHtmlGonder: TButton; ButtonGomuluGonder: TButton; procedure ActionTestExecute(Sender: TObject); procedure EPostaInitializeISO(var VHeaderEncoding: Char; var VCharSet: String); procedure SMTPStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string); procedure LHandlerStatusInfo(const AMsg: string); procedure EditYandexClick(Sender: TObject); procedure EditCRYPTIONChange(Sender: TObject); procedure EditGoogleClick(Sender: TObject); procedure ButtonYahooClick(Sender: TObject); procedure CheckBoxSifreGosterClick(Sender: TObject); private { Private declarations } procedure EPostaIcerikHazirla(Sender: TObject); public { Public declarations } end; var FormSmtpF: TFormSmtpF; implementation uses IdText, IdAttachmentFile; {$R *.dfm} procedure TFormSmtpF.ActionTestExecute(Sender: TObject); begin inherited; MemoLog.Lines.Clear; Application.ProcessMessages; MemoLog.Lines.Add('SMTP Test.'); try if SMTP.Connected then SMTP.Disconnect; SMTP.Host := EditHOST.Text; //SMTP.AuthType := satNone; SMTP.AuthType := satDefault; SMTP.Username := EditUSER.Text; SMTP.Password := EditPASSWORD.Text; SMTP.Port := 25; if StrToIntDef(EditPORT_NO.Text, -1) > 0 then SMTP.Port := StrToInt(EditPORT_NO.Text); // SSL ayarları if EditCRYPTION.ItemIndex = 0 then begin SMTP.UseTLS := utNoTLSSupport; SMTP.IOHandler := nil; SMTP.ConnectTimeout := 3000; end else begin MemoLog.Lines.Add('SMTP Test - SSL ayarları yapılıyor.'); LHandler.Destination := SMTP.Host + ':' + IntToStr(SMTP.Port); LHandler.Host := SMTP.Host; LHandler.Port := SMTP.Port; LHandler.DefaultPort := 0; LHandler.SSLOptions.Method := sslvTLSv1; LHandler.SSLOptions.Mode := sslmUnassigned; // sslmClient; LHandler.SSLOptions.VerifyMode := []; LHandler.SSLOptions.VerifyDepth := 0; SMTP.IOHandler := LHandler; case EditCRYPTION.ItemIndex of 1: SMTP.UseTLS := utUseImplicitTLS; // SSL 2: SMTP.UseTLS := utUseExplicitTLS; // SSL/TLS end; SMTP.ConnectTimeout := 10000; end; MemoLog.Lines.Add('SMTP Test - Bağlanılıyor.'); SMTP.Connect; MemoLog.Lines.Add('SMTP Test - Deneme e-postası gönderiliyor.'); EPosta.Clear; EPosta.From.Address := EditUSER.Text; EPosta.From.Name := EditUSER.Text + ' deneme kullanıcısı.'; //EPosta.ReplyTo.EMailAddresses := EditUSER.Text; //EPosta.ReplyTo.Name := EditUSER.Text; with EPosta.Recipients.Add do begin if EditAlici.Text > '' then begin Name := EditAlici.Text; Address := EditAlici.Text; end else begin Name := EditUSER.Text; Address := EditUSER.Text; end; end; EPosta.Subject := 'Deneme mesajı.'; EPostaIcerikHazirla(Sender); SMTP.Send(EPosta); MemoLog.Lines.Add('Bağlantı başarılı. "' + EditAlici.Text + '" adresine bir deneme e-postası gönderilmiştir.'); finally MemoLog.Lines.Add('SMTP Test - Bağlantı sonlandırılıyor.'); if SMTP.Connected then SMTP.Disconnect; end; ShowMessage('E-posta gönderildi.'); end; procedure TFormSmtpF.EPostaIcerikHazirla(Sender: TObject); begin EPosta.CharSet := 'utf-8'; // 'iso-8859-9'; if Sender = ButtonMetinGonder then begin EPosta.ContentType := 'text/plain'; EPosta.Body.Text := MemoTestIcerik.Lines.Text; end else begin if Sender = ButtonHtmlGonder then begin EPosta.ContentType := 'multipart/alternative'; with TIdText.Create(EPosta.MessageParts, nil) do begin Body.Text := '<html><body>' + MemoTestIcerik.Lines.Text + '. <B>KALIN YAZI</B><U>ALTI ÇİZGİLİ YAZI</U>' + '<I>İTALİK YAZI</I></body></html>'; CharSet := 'utf-8'; ContentType := 'text/html'; end; end else begin // İçine resim gömülü HTML EPosta.ContentType := 'multipart/related; type="text/html"'; with TIdText.Create(EPosta.MessageParts, nil) do begin Body.Text := '<html><body>' + MemoTestIcerik.Lines.Text + '. <B>KALIN YAZI</B><U> ALTI ÇİZGİLİ YAZI</U>' + '<I>İTALİK YAZI </I><br />' + '<img src="cid:12345"></body></html>'; CharSet := 'utf-8'; ContentType := 'text/html'; end; // ContentID'nin yukarıdaki cid:12345 teki id ile aynı olmasına dikkat edin. with TIdAttachmentFile.Create(EPosta.MessageParts, ExtractFilePath(Application.ExeName) + 'resim1.jpg') do begin ContentID := '12345'; ContentType := 'image/jpeg'; FileName := 'resim1.jpg'; end; end; end; // Ayarları e-posta olarak göndermek istersek... (* EPosta.Body.Text := 'Deneme mesajı başarı ile gönderildi.' + #13 + #10 + #13 + #10 + 'Host: ' + SMTP.Host + #13 + #10 + 'Port: ' + IntToStr(SMTP.Port) + #13 + #10 + 'Gönd. E-Posta: ' + EditUSER.Text + #13 + #10 + 'User: ' + EditUSER.Text + #13 + #10 + 'Password: ' + EditPASSWORD.Text; *) end; procedure TFormSmtpF.ButtonYahooClick(Sender: TObject); begin EditHOST.Text := 'smtp.mail.yahoo.com'; EditCRYPTION.ItemIndex := 2; // Yani SSL/TLS EditPORT_NO.Text := '587'; end; procedure TFormSmtpF.CheckBoxSifreGosterClick(Sender: TObject); begin if CheckBoxSifreGoster.Checked then EditPASSWORD.PasswordChar := #0 else EditPASSWORD.PasswordChar := '*' end; procedure TFormSmtpF.EditCRYPTIONChange(Sender: TObject); begin case EditCRYPTION.ItemIndex of 0: EditPORT_NO.Text := '587'; // <YOK> 1: EditPORT_NO.Text := '465'; // SSL 2: EditPORT_NO.Text := '587'; // SSL/TLS end; end; procedure TFormSmtpF.EditGoogleClick(Sender: TObject); begin EditHOST.Text := 'smtp.gmail.com'; EditCRYPTION.ItemIndex := 2; // Yani SSL/TLS EditPORT_NO.Text := '587'; end; procedure TFormSmtpF.EditYandexClick(Sender: TObject); begin EditHOST.Text := 'smtp.yandex.com'; // veya smtp.yandex.com.tr EditCRYPTION.ItemIndex := 2; // Yani SSL/TLS EditPORT_NO.Text := '587'; end; procedure TFormSmtpF.EPostaInitializeISO(var VHeaderEncoding: Char; var VCharSet: String); begin inherited; VCHarSet := 'utf-8'; // 'iso-8859-9'; end; procedure TFormSmtpF.LHandlerStatusInfo(const AMsg: string); begin MemoLog.Lines.Add('SSL DURUMU: ' + AMsg); end; procedure TFormSmtpF.SMTPStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string); begin MemoLog.Lines.Add('SMTP DURUMU: ' + AStatusText); end; end.[/ars] DFM içeriği: [Ars=xml] object FormSmtpF: TFormSmtpF Left = 0 Top = 0 Caption = 'SMTP Test' ClientHeight = 549 ClientWidth = 810 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = True DesignSize = ( 810 549) PixelsPerInch = 96 TextHeight = 13 object GroupBox2: TGroupBox Left = 16 Top = 16 Width = 361 Height = 209 Caption = 'Ayarlar' TabOrder = 0 object Label2: TLabel Left = 8 Top = 76 Width = 80 Height = 13 AutoSize = False Caption = 'SMTP Host' Transparent = True end object Label8: TLabel Left = 8 Top = 124 Width = 80 Height = 13 Hint = 'Port no (25, 587, 465)' AutoSize = False Caption = 'Port' ParentShowHint = False ShowHint = True Transparent = True end object Label3: TLabel Left = 8 Top = 148 Width = 80 Height = 13 AutoSize = False Caption = 'User (E-Posta)' Transparent = True end object Label4: TLabel Left = 8 Top = 172 Width = 80 Height = 13 AutoSize = False Caption = #350'ifre' Transparent = True end object Label10: TLabel Left = 8 Top = 100 Width = 80 Height = 13 AutoSize = False Caption = #350'ifreleme' Transparent = True end object EditYandex: TButton Left = 16 Top = 24 Width = 75 Height = 25 Caption = 'Yandex' TabOrder = 0 OnClick = EditYandexClick end object EditGoogle: TButton Left = 96 Top = 24 Width = 75 Height = 25 Caption = 'GMail' TabOrder = 1 OnClick = EditGoogleClick end object ButtonYahoo: TButton Left = 176 Top = 24 Width = 75 Height = 25 Caption = 'Yahoo' TabOrder = 2 OnClick = ButtonYahooClick end object Button4: TButton Left = 256 Top = 24 Width = 75 Height = 25 TabOrder = 3 end object EditHOST: TEdit Left = 96 Top = 72 Width = 233 Height = 21 TabOrder = 4 end object EditUSER: TEdit Left = 96 Top = 144 Width = 233 Height = 21 TabOrder = 7 end object EditPASSWORD: TEdit Left = 96 Top = 168 Width = 169 Height = 21 PasswordChar = '*' TabOrder = 8 end object EditCRYPTION: TComboBox Left = 96 Top = 96 Width = 97 Height = 21 Hint = 'Port no (25, 587, 465)' Style = csDropDownList ItemIndex = 0 ParentShowHint = False ShowHint = True TabOrder = 5 Text = '<YOK>' OnChange = EditCRYPTIONChange Items.Strings = ( '<YOK>' 'SSL' 'SSL/TLS') end object EditPORT_NO: TComboBox Left = 96 Top = 120 Width = 97 Height = 21 Hint = 'Port no (25, 587, 465)' ParentShowHint = False ShowHint = True TabOrder = 6 Text = '587' Items.Strings = ( '25' '465' '587' '2525') end object CheckBoxSifreGoster: TCheckBox Left = 272 Top = 172 Width = 57 Height = 17 Caption = 'G'#246'ster' TabOrder = 9 OnClick = CheckBoxSifreGosterClick end end object GroupBox3: TGroupBox Left = 16 Top = 240 Width = 361 Height = 177 Caption = ' Test ' TabOrder = 1 object Label1: TLabel Left = 16 Top = 108 Width = 80 Height = 13 AutoSize = False Caption = 'Al'#305'c'#305' E-Posta' Transparent = True end object MemoTestIcerik: TMemo Left = 16 Top = 24 Width = 305 Height = 73 Lines.Strings = ( 'Deneme e-postas'#305) TabOrder = 0 end object EditAlici: TEdit Left = 96 Top = 104 Width = 225 Height = 21 TabOrder = 1 end object ButtonMetinGonder: TButton Left = 16 Top = 144 Width = 75 Height = 25 Caption = 'Metin Msj.' TabOrder = 2 OnClick = ActionTestExecute end object ButtonHtmlGonder: TButton Left = 96 Top = 144 Width = 75 Height = 25 Caption = 'HTML' TabOrder = 3 OnClick = ActionTestExecute end object ButtonGomuluGonder: TButton Left = 176 Top = 144 Width = 75 Height = 25 Caption = 'G'#246'm'#252'l'#252' Resim' TabOrder = 4 OnClick = ActionTestExecute end end object GroupBox4: TGroupBox Left = 400 Top = 16 Width = 393 Height = 401 Anchors = [akLeft, akTop, akRight] Caption = 'Log' TabOrder = 2 object MemoLog: TMemo Left = 2 Top = 15 Width = 389 Height = 384 Align = alClient ScrollBars = ssBoth TabOrder = 0 WordWrap = False end end object GroupBox1: TGroupBox Left = 16 Top = 432 Width = 777 Height = 105 Anchors = [akLeft, akTop, akRight, akBottom] Caption = 'A'#231#305'klamalar' TabOrder = 3 object MemoAciklama: TMemo Left = 2 Top = 15 Width = 773 Height = 88 Align = alClient Lines.Strings = ( '1. Al'#305'c'#305' E-Postas'#305'n'#305' bo'#351' b'#305'rakt'#305#287#305'n'#305'z takdirde deneme e-postas'#305'n' + #305' kendi kendinize g'#246'nderir.' '' '2. Yandex ayarlar'#305'nda sadece sonu @yandex.com veya @yandex.com.t' + 'r olan e-postalar'#305' de'#287'il yandex '#252'zerinden kulland'#305#287#305'n'#305'z @bizimsi' + 'rket.com uzant'#305'l'#305' e-' 'postalar'#305'n'#305'z'#305' da kullanabilirsiniz.' '' '3. SSL veya TLS ile '#351'ifreli mesaj g'#246'nderecekseniz System klas'#246'r'#252 + 'n'#252'zde Open SSL k'#252't'#252'phanesinin DLL dosyalar'#305' (libeay32.dll, sslea' + 'y32.dll) bulunmal'#305'd'#305'r. Bu ' 'dosyalar'#305' http://indy.fulgan.com/SSL/ ba'#287'nalt'#305's'#305'ndan indirebilir' + 'siniz.' '' '4. ContentType larla ilgili ayr'#305'nt'#305'l'#305' bilgi ve '#246'rnekleri http://' + 'www.indyproject.org/Sockets/Blogs/RLebeau/2005%5F08%5F17%5FA.en.' + 'aspx ba'#287'lant'#305's'#305'ndan ' 'edinebilirsiniz.') ScrollBars = ssVertical TabOrder = 0 end end object SMTP: TIdSMTP OnStatus = SMTPStatus Host = 'mail.netbilisim.com' SASLMechanisms = <> Username = 'destek@netbilisim.com' Left = 264 Top = 232 end object EPosta: TIdMessage AttachmentEncoding = 'MIME' BccList = <> CCList = <> ContentType = 'text/plain' Encoding = meMIME FromList = < item end> Recipients = <> ReplyTo = <> ConvertPreamble = True OnInitializeISO = EPostaInitializeISO Left = 296 Top = 232 end object LHandler: TIdSSLIOHandlerSocketOpenSSL MaxLineAction = maException Port = 0 DefaultPort = 0 SSLOptions.Mode = sslmUnassigned SSLOptions.VerifyMode = [] SSLOptions.VerifyDepth = 0 OnStatusInfo = LHandlerStatusInfo Left = 328 Top = 232 end end [/ars] KAYNAK KODLAR: [attachment=206] Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - Abdullah ILGAZ - 27-12-2017 Değerli anlatımınız ve paylaşımınız için çok teşekkür ederim. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - frmman - 27-12-2017 Teşekkürler ellerinize sağlık. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - hi_selamlar - 27-12-2017 Teşekkürler elinize sağlık. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - dkadir - 08-04-2018 ellerinize sağlık bir sorum olacaktı.Bu dll dosyalarını kullanmazsak olmuyor ancak exenin yanında gezdirmek yerine exe içinde bulundurabilirmiyiz Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - mrmarman - 08-04-2018 Merhaba. - Buradan verilecek cevabın resmiyetinin gölgesinde şu başlıkta cevabını bulabileceğinizi değerlendiriyorum. - DLL'lerin EXE içine RESOURCE olarak gömülmesi konusuna ilişkin örnek de aynı mesajda yer alıyor. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - dkadir - 09-04-2018 EyvAllah muarrem abi bir çok forumda delphi konusunda çok şey öğrettin bana Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - apachi2006 - 26-11-2018 Teşekkürler harika bir örnek olmuş uygulama açısından. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - Bay_Y - 27-11-2018 Çok teşekkürler elinize sağlık. Delphi'de Indy SMTP ile e-mail gönderme. (GMail, Yandex, Yahoo vb.) - CesuR - 11-03-2019 Emeğinize sağlık. Sanırım gelişen mail teknolojileri yüzünden bu yapı malesef hata vermesine yol açıyor. Örneğin google hesabı ile denediğinizde, google bunu saldırı olarak algılayıp engelledi. Sanırım biraz düzeltmelere ihtiyaç var. Yinede temeldeki yapıyı anlatan güzel bir örnek olmuş. |