Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
ENVOI DE MAIL SANS PROFIL MAPPY AVEC SQL SERVER 2000
Information sur la source
Description
Suite à de nombreuses questions sur le sujet dans les différents forums, voici une procédure stockée permettant d'envoyer un mail depuis SQL Server 2000 et ce sans profil mappy. Le script procède tout d'abord à la création d'une erreur personnalisée correspondant aux cas d'échec de l'envoi de mail puis à la création de la procédure stockée sp_sendMail basée sur l'objet CDO au travers des procédures styockées du type sp_OACreate ... Si vous exécutez ce script la procédure sera créée dans la base master puisqu'a l'époque où je l'ai écrite elle était destinée à l'administration et la supervision du serveur sur lequel elle était déployée. Dans tous les cas, testez toujours un code avant de le livrer sur un serveur de production.
Source
- --------------------------------------------------------------------------
- -- Script created in November 25 2003 by Arnaud CLERET --
- -- This store procedure allow to send a mail without a mappy profil --
- --------------------------------------------------------------------------
-
- USE master
- GO
-
- DECLARE @WithLogEvent VARCHAR(5)
- DECLARE @ReplaceMessage VARCHAR(7)
-
- SET @WithLogEvent = 'false'
- SET @ReplaceMessage = 'REPLACE'
-
- EXECUTE sp_addmessage @msgnum = 50001, @severity = 16, @msgtext = N'Error during sending mail (Source : %s --- Description : %s).', @lang = 'us_english', @with_log = @WithLogEvent, @replace = @ReplaceMessage
- EXECUTE sp_addmessage @msgnum = 50001, @severity = 16, @msgtext = N'Erreur lors de l''envoie du mail (Source : %1! --- Description : %2!)', @lang = 'French', @with_log = @WithLogEvent, @replace = @ReplaceMessage
- GO
-
- SET QUOTED_IDENTIFIER OFF
- GO
- SET ANSI_NULLS ON
- GO
- CREATE PROCEDURE dbo.sp_sendMail
- (
- @MailFrom VARCHAR(50) = @@SERVERNAME,
- @MailTo VARCHAR(255) = 'mail@demo.com',
- @MailCc VARCHAR(255) = NULL,
- @MailBcc VARCHAR(255) = NULL,
- @MailSubject VARCHAR(500) = NULL,
- @MailBody VARCHAR(8000) = NULL,
- @IsHTMLBody BIT = 0,
- @FilesAttachments VARCHAR(8000) = NULL,
- @FilesAttachmentsDelimiter CHAR(1) = ';',
- @SendUsing CHAR(1) = '2',
- @SMTPServerName VARCHAR(15) = '192.168.0.1',
- @SMTPServerPort VARCHAR(5) = '25'
- )
- AS
- BEGIN
- SET NOCOUNT ON
-
- DECLARE @FilePath VARCHAR(255)
- DECLARE @StatusRetour INT
- DECLARE @CdoObject INT
- DECLARE @ErrorSource NVARCHAR(255)
- DECLARE @ErrorDescription NVARCHAR(500)
-
- EXECUTE @StatusRetour = sp_OACreate 'CDO.Message', @CdoObject OUTPUT
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'From', @MailFrom
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'To', @MailTo
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- IF(@MailCc IS NOT NULL)
- BEGIN
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Cc', @MailCc
- IF (@StatusRetour <> 0) GOTO ErrorHandler
- END
-
- IF(@MailBcc IS NOT NULL)
- BEGIN
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Bcc', @MailBcc
- IF (@StatusRetour <> 0) GOTO ErrorHandler
- END
-
- IF(@MailSubject IS NOT NULL)
- BEGIN
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Subject', @MailSubject
- IF (@StatusRetour <> 0) GOTO ErrorHandler
- END
-
- IF(@MailBody IS NOT NULL)
- BEGIN
- IF(@IsHTMLBody = 0)EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'TextBody', @MailBody
- ELSE EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'HTMLBody', @MailBody
- IF (@StatusRetour <> 0) GOTO ErrorHandler
- END
-
- IF (@FilesAttachments IS NOT NULL)
- BEGIN
- SET @FilesAttachments = LTrim(RTrim(@FilesAttachments))
- WHILE(DATALENGTH(@FilesAttachments) > 0)
- BEGIN
- IF(CHARINDEX(@FilesAttachmentsDelimiter,@FilesAttachments) > 0)
- BEGIN
- SET @FilePath = SUBSTRING(@FilesAttachments,1, CHARINDEX(@FilesAttachmentsDelimiter,@FilesAttachments)-1)
- SET @FilesAttachments = LTrim(Right(@FilesAttachments,DATALENGTH(@FilesAttachments) - DATALENGTH(@FilePath + @FilesAttachmentsDelimiter)))
- EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'AddAttachment', NULL, @FilePath
- END
- ELSE
- BEGIN
- SET @FilePath = @FilesAttachments
- EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'AddAttachment', NULL, @FilePath
- BREAK
- END
- END
- END
-
-
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value', @SendUsing
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @SMTPServerName
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value', @SMTPServerPort
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'Configuration.Fields.Update'
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'Send'
- IF (@StatusRetour <> 0) GOTO ErrorHandler
-
- EXECUTE sp_OADestroy @CdoObject
- RETURN
-
- ErrorHandler:
- EXECUTE @StatusRetour = sp_OAGetErrorInfo @CdoObject, @ErrorSource OUT, @ErrorDescription OUT
- IF (@StatusRetour = 0)RAISERROR (50001, 16, 1, @ErrorSource, @ErrorDescription)
- ELSE RAISERROR('Error in sp_sendMail !',16,1)
- EXECUTE sp_OADestroy @CdoObject
- RETURN
- END
- GO
- SET QUOTED_IDENTIFIER OFF
- GO
- SET ANSI_NULLS ON
- GO
--------------------------------------------------------------------------
-- Script created in November 25 2003 by Arnaud CLERET --
-- This store procedure allow to send a mail without a mappy profil --
--------------------------------------------------------------------------
USE master
GO
DECLARE @WithLogEvent VARCHAR(5)
DECLARE @ReplaceMessage VARCHAR(7)
SET @WithLogEvent = 'false'
SET @ReplaceMessage = 'REPLACE'
EXECUTE sp_addmessage @msgnum = 50001, @severity = 16, @msgtext = N'Error during sending mail (Source : %s --- Description : %s).', @lang = 'us_english', @with_log = @WithLogEvent, @replace = @ReplaceMessage
EXECUTE sp_addmessage @msgnum = 50001, @severity = 16, @msgtext = N'Erreur lors de l''envoie du mail (Source : %1! --- Description : %2!)', @lang = 'French', @with_log = @WithLogEvent, @replace = @ReplaceMessage
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.sp_sendMail
(
@MailFrom VARCHAR(50) = @@SERVERNAME,
@MailTo VARCHAR(255) = 'mail@demo.com',
@MailCc VARCHAR(255) = NULL,
@MailBcc VARCHAR(255) = NULL,
@MailSubject VARCHAR(500) = NULL,
@MailBody VARCHAR(8000) = NULL,
@IsHTMLBody BIT = 0,
@FilesAttachments VARCHAR(8000) = NULL,
@FilesAttachmentsDelimiter CHAR(1) = ';',
@SendUsing CHAR(1) = '2',
@SMTPServerName VARCHAR(15) = '192.168.0.1',
@SMTPServerPort VARCHAR(5) = '25'
)
AS
BEGIN
SET NOCOUNT ON
DECLARE @FilePath VARCHAR(255)
DECLARE @StatusRetour INT
DECLARE @CdoObject INT
DECLARE @ErrorSource NVARCHAR(255)
DECLARE @ErrorDescription NVARCHAR(500)
EXECUTE @StatusRetour = sp_OACreate 'CDO.Message', @CdoObject OUTPUT
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'From', @MailFrom
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'To', @MailTo
IF (@StatusRetour <> 0) GOTO ErrorHandler
IF(@MailCc IS NOT NULL)
BEGIN
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Cc', @MailCc
IF (@StatusRetour <> 0) GOTO ErrorHandler
END
IF(@MailBcc IS NOT NULL)
BEGIN
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Bcc', @MailBcc
IF (@StatusRetour <> 0) GOTO ErrorHandler
END
IF(@MailSubject IS NOT NULL)
BEGIN
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Subject', @MailSubject
IF (@StatusRetour <> 0) GOTO ErrorHandler
END
IF(@MailBody IS NOT NULL)
BEGIN
IF(@IsHTMLBody = 0)EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'TextBody', @MailBody
ELSE EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'HTMLBody', @MailBody
IF (@StatusRetour <> 0) GOTO ErrorHandler
END
IF (@FilesAttachments IS NOT NULL)
BEGIN
SET @FilesAttachments = LTrim(RTrim(@FilesAttachments))
WHILE(DATALENGTH(@FilesAttachments) > 0)
BEGIN
IF(CHARINDEX(@FilesAttachmentsDelimiter,@FilesAttachments) > 0)
BEGIN
SET @FilePath = SUBSTRING(@FilesAttachments,1, CHARINDEX(@FilesAttachmentsDelimiter,@FilesAttachments)-1)
SET @FilesAttachments = LTrim(Right(@FilesAttachments,DATALENGTH(@FilesAttachments) - DATALENGTH(@FilePath + @FilesAttachmentsDelimiter)))
EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'AddAttachment', NULL, @FilePath
END
ELSE
BEGIN
SET @FilePath = @FilesAttachments
EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'AddAttachment', NULL, @FilePath
BREAK
END
END
END
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value', @SendUsing
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', @SMTPServerName
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE @StatusRetour = sp_OASetProperty @CdoObject, 'Configuration.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport").Value', @SMTPServerPort
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'Configuration.Fields.Update'
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE @StatusRetour = sp_OAMethod @CdoObject, 'Send'
IF (@StatusRetour <> 0) GOTO ErrorHandler
EXECUTE sp_OADestroy @CdoObject
RETURN
ErrorHandler:
EXECUTE @StatusRetour = sp_OAGetErrorInfo @CdoObject, @ErrorSource OUT, @ErrorDescription OUT
IF (@StatusRetour = 0)RAISERROR (50001, 16, 1, @ErrorSource, @ErrorDescription)
ELSE RAISERROR('Error in sp_sendMail !',16,1)
EXECUTE sp_OADestroy @CdoObject
RETURN
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
Conclusion
La procédure permet de l'appeler selon de nombreux paramètres, pas tous obligatoires et possédant des valeurs par défaut selon si vous souhaitez y inclure une pièce jointe, des destinataires en CC ...
L'appel de la procédure peut se faire de la manière suivante : EXECUTE master.dbo.sp_sendMail @MailFrom = 'mailFrom@demo.com', @MailTo = 'mailTo@demo.com', @MailSubject = 'Alerte', @MailBody = 'Corp du message'
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Historique
- 25 mars 2006 14:04:47 :
- Quelques petites corrections d'orthographe sur le post :)
- 26 mars 2006 14:59:40 :
- Ajout d'un exemple d'appel de la procédure stockée d'envoi de mail.
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | | | | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|