Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to queue emails sent using CDO.Message object

Posted on 2008-10-16
6
Medium Priority
?
459 Views
Last Modified: 2013-11-05
Hi there

I need to send email notifications from my stored procedure and am using the cdosys object CDO.Message to do this.

My problem is that I will need to cycle round and send anywhere up to 100 emails within my procedure as the procedure itself deletes/updates records and then needs to tell the relevant user these changes affect.  So is it reasonable to just build and send a new email every time or is there some facility for queuing mails as they are created and then just sending them once at the end?  I am obviously concerned that emailing each user in turn will be too expensive timewise.

Cheers
Sam
0
Comment
Question by:Samm1502
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 22732022
Use DatabaseMail that come with sql server... this enqueue the email requests that you need through service broker.
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 2000 total points
ID: 22733045
If you are using 2005 and/or 2008 then sp_send_dbmail is much more friently than the "old" way...




declare @CC varchar(100)
declare @BCC varchar(100)
Declare @MailID int
Declare @hr int
Declare @SenderName varchar(100)
Declare @SenderAddress varchar(100)
Declare @RecipientName varchar(100)
Declare @RecipientAddress varchar(100)
Declare @Subject varchar(200)
Declare @Body varchar(8000)
Declare @MailServer varchar(100)
 
set @MailServer = 'localhost'
set @SenderName='Mark Wills'
set @SenderAddress='mark.wills@email.address' 
set @RecipientName = 'Mark Wills' 
set @RecipientAddress = 'mark.wills@email.address'
set @Subject='SQL Mail Test'
set @body='Hello, this is a test email from SQL Server'
 
set @CC  = null
set @BCC  = null
 
-- new way:
 
EXEC msdb.dbo.sp_send_dbmail
    @recipients=@recipientaddress,
    @profile_name=N'MRW_SQL_Mail',
    @body=@body ;
 
-- cdo / old way:
 
EXEC @hr = sp_OACreate 'CDO.Message', @MailID OUT
EXEC @hr = sp_OASetProperty @MailID, 'From',@senderaddress
EXEC @hr = sp_OASetProperty @MailID, 'TextBody', @Body
EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCC
EXEC @hr = sp_OASetProperty @MailID, 'CC', @CC
EXEC @hr = sp_OASetProperty @MailID, 'Subject', @Subject
EXEC @hr = sp_OASetProperty @MailID, 'To', @recipientAddress
EXEC @hr = sp_OAMethod @MailID, 'Send', NULL
EXEC @hr = sp_OADestroy @MailID
 
-- more advanced new way with query
 
EXEC msdb.dbo.sp_send_dbmail
     @recipients=@recipientAddress,
     @body=@body,
     @subject=@subject,
     @profile_name='MRW_SQL_mail',
     @query = 'select name, address, phone from mrwtemp..tbl_customer_email_list',
     @execute_query_database = 'mrwtemp',
     @attach_query_result_as_file = 1,                    -- change to zero to include in body
     @query_attachment_filename = 'your_customer_list.txt'

Open in new window

0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22733075
Oh, forgot the important bit... Using the new way, you can run a query very easily - so long as all your security is OK (need to add mail user to SQL logins), so, could easily "stockpile" the list of changes and then run a query...
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Samm1502
ID: 22739697
Thanks Mark. Does the new way have a means of queueing the mails sent out becaue I am not sending a list of updates to one user but a single update to many users so want to make sure I don't have to wait for each mail to be sent before the next one can be processed.

Cheers
Sam
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22739735
Not exactly...  

Still need to formulate a query for the list of updates, but, you will need to loop through each recipient if they are to get their "own" updates. They are seperate parameters so need to match the changes (ie query) for that recipient.

But if the changes (ie query) are "global" to all users, then can simply put all the users into the recipient list (seperate by semicolons).
0
 

Author Closing Comment

by:Samm1502
ID: 31506664
Sorry thought I had closed this one off already!  Many thanks.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question