Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.18 KB | None | 0 0
  1. CREATE PROCEDURE [dbo].[usp_SendTextEmail]  @ServerAddr nvarchar(128),
  2. @From nvarchar(128),
  3. @To nvarchar(1024),
  4. @Subject nvarchar(256),
  5. @Bodytext nvarchar(max) = 'This is a test text email from MS SQL server, do not reply.',
  6. @User nvarchar(128) = '',
  7. @Password nvarchar(128) = '',
  8. @SSLConnection int = 0,
  9. @ServerPort int = 25
  10. AS
  11. DECLARE @hr int
  12. DECLARE @oSmtp int
  13. DECLARE @result int
  14. DECLARE @description nvarchar(255)
  15. EXEC @hr = sp_OACreate 'EASendMailObj.Mail',@oSmtp OUT
  16. If @hr <> 0
  17. BEGIN
  18.     PRINT 'Please make sure you have EASendMail Component installed!'
  19.     EXEC @hr = sp_OAGetErrorInfo @oSmtp, NULL, @description OUT
  20.     IF @hr = 0
  21.     BEGIN
  22.         PRINT @description
  23.     END
  24.     RETURN
  25. End
  26. EXEC @hr = sp_OASetProperty @oSmtp, 'LicenseCode', 'TryIt'
  27. EXEC @hr = sp_OASetProperty @oSmtp, 'ServerAddr', @ServerAddr
  28. EXEC @hr = sp_OASetProperty @oSmtp, 'ServerPort', @ServerPort
  29. EXEC @hr = sp_OASetProperty @oSmtp, 'UserName', @User
  30. EXEC @hr = sp_OASetProperty @oSmtp, 'Password', @Password
  31. EXEC @hr = sp_OASetProperty @oSmtp, 'FromAddr', @From
  32. EXEC @hr = sp_OAMethod @oSmtp, 'AddRecipientEx', NULL,  @To, 0
  33. EXEC @hr = sp_OASetProperty @oSmtp, 'Subject', @Subject
  34. EXEC @hr = sp_OASetProperty @oSmtp, 'BodyText', @BodyText
  35. If @SSLConnection > 0
  36. BEGIN
  37.     EXEC @hr = sp_OAMethod @oSmtp, 'SSL_init', NULL
  38. END
  39. /* you can also add an attachment like this */
  40. /*EXEC @hr = sp_OAMethod @oSmtp, 'AddAttachment', @result OUT, 'd:\test.jpg'*/
  41. /*If @result <> 0 */
  42. /*BEGIN*/
  43. /*   EXEC @hr = sp_OAMethod @oSmtp, 'GetLastErrDescription', @description OUT*/
  44. /*    PRINT 'failed to add attachment with the following error:'*/
  45. /*    PRINT @description*/
  46. /*END*/
  47. PRINT 'Start to send email ...'
  48. EXEC @hr = sp_OAMethod @oSmtp, 'SendMail', @result OUT
  49. If @hr <> 0
  50. BEGIN
  51.     EXEC @hr = sp_OAGetErrorInfo @oSmtp, NULL, @description OUT
  52.     IF @hr = 0
  53.     BEGIN
  54.         PRINT @description
  55.     END
  56.     RETURN
  57. End
  58. If @result <> 0
  59. BEGIN
  60.     EXEC @hr = sp_OAMethod @oSmtp, 'GetLastErrDescription', @description OUT
  61.     PRINT 'failed to send email with the following error:'
  62.     PRINT @description
  63. END
  64. ELSE
  65. BEGIN
  66.     PRINT 'Email was sent successfully!'
  67. END
  68. EXEC @hr = sp_OADestroy @oSmtp
  69. Go
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement