Link to home
Start Free TrialLog in
Avatar of mburk1968
mburk1968Flag for United States of America

asked on

Issue sending to multiple addresses using sp_send_dbmail SQL 2012

I have the following script which for some reason is only sending the email to me.  Me being "me@email.com" Why am I unable to send to others in my domain?

DECLARE @Style CHAR(12) ,
    @BodyMessage NVARCHAR(MAX);



SELECT  @BodyMessage = N'There were no Royalty changes posted';
SELECT  @Style = T2.style
FROM    KLL_Cust.dbo.Report_RoyaltyCache T1
        FULL JOIN dbo.zzxstylr T2 ON T1.division = T2.division
                                 AND T1.style = T2.style
WHERE   T1.roy_cls <> T2.roy_cls
        OR T1.royalty <> T2.royalty;
IF @Style > = 0
    BEGIN
        EXEC msdb.dbo.sp_start_job @job_name = 'D612C861-179A-4090-94F3-19532FAC7581';
    END;
ELSE
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail @recipients = 'person1@email.com; me@email.com; otherguy@email.com',
            @subject = 'Royalty Change Report', @body = @BodyMessage;
    END;
Avatar of mburk1968
mburk1968
Flag of United States of America image

ASKER

I should also mention that the script does not generate any errors however no mater what position I place my email address in I am the only one who receives it.
Avatar of Brian Crowe
It is either an issue with your profile definition or your permissions on the SMTP server that you are using.  I notice you aren't selecting a profile for sp_send_dbmail.

[ @profile_name = ] 'profile_name'
Is the name of the profile to send the message from. The profile_name is of type sysname, with a default of NULL. The profile_name must be the name of an existing Database Mail profile. When no profile_name is specified, sp_send_dbmail uses the default private profile for the current user. If the user does not have a default private profile, sp_send_dbmail uses the default public profile for the msdb database. If the user does not have a default private profile and there is no default public profile for the database, @profile_name must be specified.
This is what I have now.

principal_id      principal_name      profile_id      profile_name                      is_default
2                      guest                      1                      KLNYBCDB01 Mail      0
2                      guest                      2                       DB Mail                              1

DECLARE @Style CHAR(12) ,
    @BodyMessage NVARCHAR(MAX);



SELECT  @BodyMessage = N'There were no Royalty changes posted';
SELECT  @Style = T2.style
FROM    KLL_Cust.dbo.Report_RoyaltyCache T1
        FULL JOIN dbo.zzxstylr T2 ON T1.division = T2.division
                                     AND T1.style = T2.style
WHERE   T1.roy_cls <> T2.roy_cls
        OR T1.royalty <> T2.royalty;
IF @Style > = 0
    BEGIN
        EXEC msdb.dbo.sp_start_job @job_name = 'D612C861-179A-4090-94F3-19532FAC7581';
    END;
ELSE
    BEGIN
        EXEC msdb.dbo.sp_send_dbmail @recipients = 'person1@email.com',
            @copy_recipients = 'me@email.com',
            @profile_name = 'DB Mail', @subject = 'Royalty Change Report',
            @body = @BodyMessage;

    END;


I'm still the only one who receives the mail.
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a good question. Would it be permissions for the database server to relay emails? Or the creator? In this case me?
Thank you. It was the permissions for the outgoing SMTP server. You really pointed me in the right direction with all of your post however this was the icing on the cake.

Now I can send data driven reports using SQL Standard.

Thank You