This shows you the differences between two versions of the page.
— |
public:howto:smtpsend [2007/11/30 14:30] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== How To Use SMTP with TLS ====== | ||
+ | <code delphi> | ||
+ | uses | ||
+ | blcksock, smtpsend, pop3send, ssl_openssl, | ||
+ | //... | ||
+ | |||
+ | type | ||
+ | ESMTP = class (Exception); | ||
+ | //... | ||
+ | |||
+ | // e-mail template (subject and other headers+CRLF+CRLF+e-mail body) | ||
+ | // in file sFileName | ||
+ | procedure MailSend(const sSmtpHost, sSmtpPort, sSmtpUser, sSmtpPasswd, sFrom, sTo, sFileName: AnsiString); | ||
+ | var | ||
+ | smtp: TSMTPSend; | ||
+ | msg_lines: TStringList; | ||
+ | begin | ||
+ | msg_lines := TStringList.Create; | ||
+ | smtp := TSMTPSend.Create; | ||
+ | try | ||
+ | msg_lines.LoadFromFile(sFileName); | ||
+ | msg_lines.Insert(0, 'From: ' + sFrom); | ||
+ | msg_lines.Insert(1, 'To: ' + sTo); | ||
+ | |||
+ | smtp.UserName := sSmtpUser; | ||
+ | smtp.Password := sSmtpPasswd; | ||
+ | |||
+ | smtp.TargetHost := sSmtpHost; | ||
+ | smtp.TargetPort := sSmtpPort; | ||
+ | |||
+ | AddToLog('SMTP Login'); | ||
+ | if not smtp.Login() then | ||
+ | raise ESMTP.Create('SMTP ERROR: Login:' + smtp.EnhCodeString); | ||
+ | AddToLog('SMTP StartTLS'); | ||
+ | if not smtp.StartTLS() then | ||
+ | raise ESMTP.Create('SMTP ERROR: StartTLS:' + smtp.EnhCodeString); | ||
+ | |||
+ | AddToLog('SMTP Mail'); | ||
+ | if not smtp.MailFrom(sFrom, Length(sFrom)) then | ||
+ | raise ESMTP.Create('SMTP ERROR: MailFrom:' + smtp.EnhCodeString); | ||
+ | if not smtp.MailTo(sTo) then | ||
+ | raise ESMTP.Create('SMTP ERROR: MailTo:' + smtp.EnhCodeString); | ||
+ | if not smtp.MailData(msg_lines) then | ||
+ | raise ESMTP.Create('SMTP ERROR: MailData:' + smtp.EnhCodeString); | ||
+ | |||
+ | AddToLog('SMTP Logout'); | ||
+ | if not smtp.Logout() then | ||
+ | raise ESMTP.Create('SMTP ERROR: Logout:' + smtp.EnhCodeString); | ||
+ | AddToLog('OK!'); | ||
+ | finally | ||
+ | msg_lines.Free; | ||
+ | smtp.Free; | ||
+ | end; | ||
+ | end; | ||
+ | |||
+ | </code> |