Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Recommend a Mail Component

Author  Topic 

thekiwi
Starting Member

4 Posts

Posted - 2010-07-09 : 00:07:41
Hi everyone

Currently we have a 2008 SQL install (an upgrade to a previous 2000 instance) in which I have been sending EMails using CDO Stored Procedure


--************* Create the CDO.Message Object ************************
EXEC @hr = sp_OACreate 'CDO.Message', @iMsg OUT

--***************Configuring the Message Object ******************
-- This is to configure a remote SMTP server.
-- http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdosys/html/_cdosys_schema_configuration_sendusing.asp
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").Value','2'
-- This is to configure the Server Name or IP address.
-- Replace MailServerName by the name or IP of your SMTP Server.
EXEC @hr = sp_OASetProperty @iMsg, 'Configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").Value', '10.0.0.2'

-- Save the configurations to the message object.
EXEC @hr = sp_OAMethod @iMsg, 'Configuration.Fields.Update', null

-- Set the e-mail parameters.
EXEC @hr = sp_OASetProperty @iMsg, 'To', 'support@mydomani.co.nz'
EXEC @hr = sp_OASetProperty @iMsg, 'From', @EmailFrom
EXEC @hr = sp_OASetProperty @iMsg, 'Subject', @subject
EXEC @hr = sp_OASetProperty @iMsg, 'Cc', @cc
EXEC @hr = sp_OASetProperty @iMsg, 'Bcc', @bcc
-- If you are using HTML e-mail, use 'HTMLBody' instead of 'TextBody'.
if @MailFormat = 0
BEGIN
EXEC @hr = sp_OASetProperty @iMsg, 'TextBody', @message
END
ELSE
BEGIN
EXEC @hr = sp_OASetProperty @iMsg, 'HTMLBody', @message
END

if @attachment IS NOT NULL
begin
EXEC @hr = sp_OAMethod @iMsg, 'AddAttachment', NULL, @attachment
end
EXEC @hr = sp_OAMethod @iMsg, 'Send', NULL
update tbl_mailtable set sent = 'y', datesent = getdate() where mailid = @mailid

-- Do some error handling after each step if you need to.
-- Clean up the objects created.
EXEC @hr = sp_OADestroy @iMsg


This SP collects the Emails to be sent from a "queue" table (tbl_mailtable ) which then gets updated to indicate that the message has been sent.

For the last few years it has worked really well, however after shifting to a new host a small problem of duplicate Emails sometimes being sent has increased. We have one entry in the database, but at the ISP we will get two Emails being processed through their Mail Server. I can only put it down to the load on the mail server resulting in the Database basically resending it.

To overcome this, I thought Id investigate sp_send_dbmail, however this wont work for us, as we often send within the Business Unit Emails with a host of FROM Addresses (eg 43264328@somewhere.com) ... and from what I can find out the FROM address is set by the profile used ... and we would have an unlimited number of FROM addresses over time.

Can anyone suggest a COM object (or anything) which will serve as allowing us to send reliably from the database table to a queue mechanism which relays to the ISP's SMTP Server?

Alternatively any ideas on the duplicates issue? They are definetly individual Emails with diferent ID's in the headers.

Cheers

Craig

[Code]
This is the Mailtable

[MailID] [int] IDENTITY(1,1) NOT NULL,
[idParticipant] [int] NULL,
[idTrainer] [int] NULL,
[idSchool] [int] NULL,
[MailFormat] [int] NULL,
[EmailFrom] [varchar](200) NOT NULL,
[Recipient] [varchar](250) NOT NULL,
[CC] [varchar](200) NULL,
[BCC] [varchar](150) NULL,
[Subject] [varchar](255) NULL,
[Attachment] [varchar](255) NULL,
[Body] [text] NULL,
[DateSubmitted] [datetime] NULL,
[Sent] [char](10) NULL,
[DateSent] [datetime] NULL,
[notes] [text] NULL,
[idTrainerSubmit] [int] NULL,

[/code]

Kristen
Test

22859 Posts

Posted - 2010-07-09 : 03:23:33
I would move the

update tbl_mailtable set sent = 'y', datesent = getdate() where mailid = @mailid

to the start (and I would use tri-state, so set "sent" to "Sending", and then reupdate at the end to change it to "Sent")

That way another task run, which overlapped, could not resend a "Sending" email

I don't know if that is what is happening, or if it is getting duplicated in the SMTP queue or somesuch, but I'd adopt that sort of failsafe anyway.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-07-09 : 03:42:17
Why aren't you using database mail?



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

thekiwi
Starting Member

4 Posts

Posted - 2010-07-09 : 16:56:35
quote:
Originally posted by Peso

Why aren't you using database mail?



N 56°04'39.26"
E 12°55'05.63"



My understanding is that requires profiles to be set to dictate the FROM address and that isn't possible with this situation
Go to Top of Page

thekiwi
Starting Member

4 Posts

Posted - 2010-07-09 : 16:57:36
quote:
Originally posted by Kristen

I would move the

update tbl_mailtable set sent = 'y', datesent = getdate() where mailid = @mailid

to the start (and I would use tri-state, so set "sent" to "Sending", and then reupdate at the end to change it to "Sent")

That way another task run, which overlapped, could not resend a "Sending" email

I don't know if that is what is happening, or if it is getting duplicated in the SMTP queue or somesuch, but I'd adopt that sort of failsafe anyway.



Ahhhh good idea :). Thanks
Go to Top of Page
   

- Advertisement -