Advertisement
Guest User

Untitled

a guest
Jun 7th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. function SendEmail(sendTo, subject, body: string;
  2. attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser,
  3. smtpPass: string; tls: TIdUseTLS; respoderPara: string): boolean;
  4. var
  5. smtp: TIdSmtp;
  6. ssl: TIdSSLIOHandlerSocketOpenSSL;
  7. msg: TIdMessage;
  8. i: Integer;
  9. replyTo: TIdEMailAddressItem;
  10. IdAttachmentFile : TIdAttachmentFile;
  11. begin
  12. smtp:=TIdSmtp.Create(nil);
  13. ssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  14. msg:=TIdMessage.Create(nil);
  15. try
  16.  
  17. try
  18. smtp.Host := smtpHost;
  19. smtp.Port := smtpPort;
  20. smtp.Username := smtpUser;
  21. smtp.Password := smtpPass;
  22.  
  23.  
  24. //smtp.OnConnected :=IdSMTP1Connected;
  25. //smtp.OnDisconnected :=IdSMTP1Disconnected;
  26. //smtp.OnFailedRecipient :=IdSMTP1FailedRecipient;
  27. //smtp.OnStatus :=IdSMTP1Status;
  28. //smtp.OnTLSNotAvailable :=IdSMTP1TLSNotAvailable;
  29. //smtp.OnWork :=IdSMTP1Work;
  30.  
  31. if not (tls=utNoTLSSupport) then
  32. begin
  33. ssl.Destination := smtpHost + ':' + IntToStr(smtpPort);
  34. ssl.Host := smtpHost;
  35. ssl.Port := smtpPort;
  36. ssl.SSLOptions.Method := sslvTLSv1;
  37.  
  38. //ssl.OnStatusInfo:=IdSSLIOHandlerSocketOpenSSL1StatusInfo;
  39. //ssl.OnGetPassword:=IdSSLIOHandlerSocketOpenSSL1GetPassword;
  40. //ssl.OnStatus:=IdSSLIOHandlerSocketOpenSSL1Status;
  41.  
  42. smtp.IOHandler := ssl;
  43. smtp.UseTLS := tls;
  44. end;
  45.  
  46. msg.Recipients.EMailAddresses := sendTo;
  47. msg.Subject := subject;
  48. msg.ContentType := 'text/html';
  49. msg.Body.Text := body;
  50.  
  51. if respoderPara <> '' then
  52. begin
  53. replyTo := msg.ReplyTo.Add;
  54. replyTo.Name := 'SYNS - Contato';
  55. replyTo.Address := respoderPara;
  56. end;
  57.  
  58. if(Assigned(attachFiles)) then
  59. begin
  60. for i := 0 to attachFiles.Count - 1 do
  61. begin
  62. if FileExists(attachFiles[i]) then
  63. begin
  64. IdAttachmentFile := TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]); //Incluindo o anexo na mensagem
  65. IdAttachmentFile.ContentType := 'application/pdf;'; //Informando o tipo MIME do anexo. IMPORTANTE! Colocar o tipo MIME + ; (ponto-e-vírgula)
  66. IdAttachmentFile.FileName := ExtractFileName(attachFiles[i]); //Nome do arquivo
  67. //TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]);
  68. end;
  69. end;
  70. end;
  71.  
  72. smtp.Connect;
  73. smtp.Send(msg);
  74. smtp.Disconnect;
  75.  
  76. result:=true;
  77. finally
  78. msg.Free;
  79. ssl.Free;
  80. smtp.Free;
  81. end;
  82. except
  83. result:=false;
  84. end;
  85.  
  86. end;
  87.  
  88. function SendEmail(sendTo, subject, body: string;
  89. attachFiles: TStringList; smtpHost: string; smtpPort: Integer; smtpUser,
  90. smtpPass: string; tls: TIdUseTLS; respoderPara: string): boolean;
  91. var
  92. smtp: TIdSmtp;
  93. ssl: TIdSSLIOHandlerSocketOpenSSL;
  94. msg: TIdMessage;
  95. text: TIdText;
  96. i: Integer;
  97. replyTo: TIdEMailAddressItem;
  98. IdAttachmentFile : TIdAttachmentFile;
  99. begin
  100. smtp:=TIdSmtp.Create(nil);
  101. ssl:=TIdSSLIOHandlerSocketOpenSSL.Create(nil);
  102. msg:=TIdMessage.Create(nil);
  103. try
  104.  
  105. try
  106. smtp.Host := smtpHost;
  107. smtp.Port := smtpPort;
  108. smtp.Username := smtpUser;
  109. smtp.Password := smtpPass;
  110.  
  111. if not (tls=utNoTLSSupport) then
  112. begin
  113. ssl.Destination := smtpHost + ':' + IntToStr(smtpPort);
  114. ssl.Host := smtpHost;
  115. ssl.Port := smtpPort;
  116. ssl.SSLOptions.Method := sslvTLSv1;
  117.  
  118. smtp.IOHandler := ssl;
  119. smtp.UseTLS := tls;
  120. end;
  121.  
  122. msg.Recipients.EMailAddresses := sendTo;
  123. msg.Subject := subject;
  124.  
  125. msg.MessageParts.Clear();
  126. msg.ContentType := 'multipart/mixed';
  127.  
  128. text := TIdText.Create (msg.MessageParts, Nil);
  129. text.ContentType := 'text/html';
  130. text.CharSet := 'ISO-8859-1';
  131. text.Body.Text := body;
  132.  
  133. if respoderPara <> '' then
  134. begin
  135. replyTo := msg.ReplyTo.Add;
  136. replyTo.Name := 'SYNS - Contato';
  137. replyTo.Address := respoderPara;
  138. end;
  139.  
  140. if(Assigned(attachFiles)) then
  141. begin
  142. for i := 0 to attachFiles.Count - 1 do
  143. begin
  144. if FileExists(attachFiles[i]) then
  145. begin
  146. IdAttachmentFile := TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]); //Incluindo o anexo na mensagem
  147. IdAttachmentFile.ContentType := 'application/pdf;'; //Informando o tipo MIME do anexo. IMPORTANTE! Colocar o tipo MIME + ; (ponto-e-vírgula)
  148. IdAttachmentFile.FileName := ExtractFileName(attachFiles[i]); //Nome do arquivo
  149. IdAttachmentFile.ContentDisposition := 'inline';
  150. IdAttachmentFile.ContentTransfer := 'base64';
  151. //TIdAttachmentFile.Create(msg.MessageParts, attachFiles[i]);
  152. end;
  153. end;
  154. end;
  155.  
  156. smtp.Connect;
  157. smtp.Send(msg);
  158. smtp.Disconnect;
  159.  
  160. result:=true;
  161. finally
  162. msg.Free;
  163. ssl.Free;
  164. smtp.Free;
  165. end;
  166. except
  167. result:=false;
  168. end;
  169.  
  170. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement