2017-09-30 1 views
2

Ich sende Benachrichtigungs-E-Mail mit SQL Server, aber manchmal werden keine E-Mails an die Benutzer gesendet.Senden von E-Mail mit sp_send_dbmail() funktioniert manchmal nicht

Hier ist meine SQL-Tabelle, die ich E-Mails speichern, die es den Benutzern

CREATE TABLE [dbo].[EmailNotification](
[Id] [INT] IDENTITY(1,1) NOT NULL, 
[EmailAdress] [NVARCHAR](50) NULL, 
[EmailBody] [NVARCHAR](500) NULL, 
[EmailSubject] [NVARCHAR](250) NULL, 
[Attachment] [NVARCHAR](500) NULL, 
[EmailSent] [BIT] NULL CONSTRAINT [DF_EmailNotification_EmailSent] DEFAULT 
((0)), 
[EmailCreateDate] [DATETIME] NULL CONSTRAINT 
[DF_EmailNotification_EmailCreateDate] DEFAULT (GETDATE()), 
[EmailSentDate] [DATETIME] NULL, 
CONSTRAINT [PK_EmailNotification] PRIMARY KEY CLUSTERED 
([Id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

gesendet wird, und ich habe einen Job erstellt, die alle 1 Minute

CREATE PROCEDURE [dbo].[spSendEmail] 
AS 
BEGIN 
BEGIN TRAN 
DECLARE @id BIGINT 
DECLARE @max_id BIGINT 
DECLARE @query NVARCHAR(1000) 
DECLARE @EmailBody NVARCHAR(1000) 
DECLARE @EmailAdress NVARCHAR(500) 
DECLARE @EmailSubject NVARCHAR(500) 
DECLARE @attachments NVARCHAR(1000) 

if exists (SELECT * FROM dbo.EmailNotification where EmailSent=0) 
begin 
SELECT @id=MIN(id) FROM dbo.EmailNotification where EmailSent=0 


SELECT @EmailAdress=EmailAdress,@EmailBody=EmailBody,@EmailSubject=EmailSubject,@attachments=Attachment 
FROM EmailNotification WHERE id = @id 


exec [msdb].[dbo].[sp_send_dbmail] @profile_name='Notification', 
@[email protected], 
@blind_copy_recipients='example.email.com', 
@[email protected], 
@[email protected], 
@[email protected] 

end 
IF(@@ERROR>0) 
BEGIN 
ROLLBACK 
END 
ELSE 
BEGIN 
UPDATE EmailNotification set EmailSent=1, EmailSentDate=getdate() WHERE [email protected] 
COMMIT 
END 
+1

„Funktioniert nicht ". Meinst du, sie erhalten die E-Mail nicht? Ändern Sie zunächst Ihren Code und notieren Sie sich den Rückgabewert wie unten beschrieben. Dann überprüfen Sie die SQL-Protokolle zu sehen, ob es einen Fehler erwähnt –

Antwort

2

diese Prozedur ausführt Was Sie tun mean: spSendEmail löst sp_send_dbmail nicht aus? sp_send_dbmail wird ausgelöst, tut aber nichts ....?

Bitte setzen Sie den Return-Code von sp_send_dbmail:
0 => OK
<> 0 => Fehler

aufgetreten
DECLARE @result int; 
DECLARE @ErrorNb int; 
EXECUTE @result = exec [msdb].[dbo].[sp_send_dbmail] @profile_name='EDMS email notification', 
@[email protected], 
@blind_copy_recipients='example.email.com', 
@[email protected], 
@[email protected], 
@[email protected] 
SET @ErrorNb = @@ERROR  

IF @result <> 0 
BEGIN 
-- Something goes wrong 
SELECT @result,@ErrorNb 
END 

Sie können auch versuchen verwenden:

BEGIN TRY 
    EXECUTE exec [msdb].[dbo].[sp_send_dbmail] @profile_name='EDMS email notification', 
    @[email protected], 
    @blind_copy_recipients='example.email.com', 
    @[email protected], 
    @[email protected], 
    @[email protected] 
END TRY 
BEGIN CATCH 
    SELECT ERROR_MESSAGE(); 
END CATCH 
+0

Vielen Dank für Ihre Antwort clementakis.Ich werde versuchen, was Sie anbieten – Nurlan

+0

Haben Sie Ihr Problem festgestellt? Hoffe so ^^ – clementakis

+0

Das Problem war maximale Anlage Größe manchmal überschritten :)) – Nurlan

Verwandte Themen