views:

85

answers:

1

I have configured Database Mail for sending newsletter emails from sql database. I have a stored procedure that selects the emails of the subscribers and send them the mails like this:

EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'ProfileName',
@recipients = @email,
@subject = @thesubject,
@body = @themessage,
@body_format = 'HTML' ;

The account used for sending is working fine coz when I sent the test mail after configuring the Database Mail it was sent successfully but the problem is sending more than one mail.

Currently there are 50 emails in the table after executing the stored procedure I checked the msdb tables sysmails_event_log, sysmails_faileditems and sysmails_sentitems, I found that only the 1st 12 emails were sent and the rest failed and the error message in the event_log was:

The mail could not be sent to the recipients because of the mail server failure. (Sending Mail using Account 1 (2009-12-01T16:55:11). Exception Message: Cannot send mails to mail server. (The operation has timed out.). )

I checked the emails no. 13 and laters and I didn't find anything wrong with them, so I tried executing the procedure again, this time it sent to the 1st 14 email and the rest failed with the same error so I changed the procedure and made it select the mails starting from mail no. 15 (as if it will send from record 15 to 50) and tested it, this time it sent to the 1st 13 email, I selected from 27 to 50 and executed, again it sent to the 1st 12.

I don't understand what's going on here what's this behavior of sending mails only in the range of 12-14 mails at a time.

Can anyone help me on this?

Thanks in advance

+1  A: 

Where is the SMTP server located? Have you checked to see if you can fire more than 12 e-mails at a time through that SMTP server without SQL Server being involved at all? It looks like the problem is not SQL Server but some kind of policy on the SMTP server. There may be some kind of abuse prevention on the firewall or other network appliances in between the database server and the SMTP server as well. Have you considered setting up SMTP services on the database server, then setting up Database Mail so that it uses the local SMTP server instead of going over the network (and potentially running into the policy or policies alluded to above)? Of course you should set up SMTP securely, making sure that only connections from the local machine are allowed.

Aaron Bertrand
The SMTP server is located on the same domain of the database server and the SMTP server can fire more than 12 emails at a time with and without SQL Server being involved and I don't think there's any kind of abuse prevention between the database server and the SMTP server because before using Database Mail I was using XPSMTP extended procedure for sending mails from SQL and it was working fine and all emails were delivered, I only switched to Database Mail coz I needed to send a large message body as nvarchar(max) and the xpsmtp only allowed nvarchar(4000).
Yasmine
So I thought the problem might be with the Database Mail and not the SMTP server since everything was fine before switching to Database Mail. And as for the solution of using the local SMTP Server I want this to be the last solution to go for.Thanks
Yasmine
This is the extended procedure I used before Database Mailhttp://www.sqldev.net/xp/xpsmtp.htm
Yasmine
I've used Gert's procedure with great success in the past as well, including on a 2005 server where we had a different problem with Database Mail (though I don't bother trying to send huge e-mails like you do). So, with the extended procedure you get more than 12 e-mails, but with Database Mail you don't? Have you tried a different SMTP server on the domain? What is wrong with putting an SMTP server on the local box?
Aaron Bertrand
Am not sending huge emails it's a html newsletter to be sent to 50 to 100 people and it's size is greater than nvarchar(4000) it may reach 7000 or 8000 so when I was using the xpsmtp extended procedure the members received the newsletter but was truncated. And with the extended procedure the whole mail list was delieverd not only subsets of 12 emails at a time like it is with Database Mail.
Yasmine
We don't have another SMTP server on the same domain to try, the current one is working fine except with Database Mail and as for the local SMTP server we don't prefer using it as it's extremely hackable and we don't want to put extra load on the server running the SQL
Yasmine
I'm just suggesting these as alternatives to at least determine whether the problem is your SMTP server or database mail. It does not take much to set up SMTP temporarily on another server. (SMTP is only as hackable as your server/network - if you can't trust that...) The database mail component is pretty much hands off (it's internal and uses service broker), so if you determine the SMTP server is not the problem, you can either rip it down and set it up again from scratch, or you can open a support case with MS. But I really think the problem is the SMTP server given the error message.
Aaron Bertrand
Ok i'll give it a try
Yasmine
You really think that the problem is with the SMTP server even though all mails were delieverd when I was using the extended procedure?
Yasmine
Well, the error message is coming from the SMTP server, so yes.
Aaron Bertrand
I aggree with Aaron, this problem is in the SMTP server not in the dbmail infrastructure (and why my opinion matters is because I worked fairly close to the db mail feature when was developed).
Remus Rusanu
Ok, I tried sending using the local SMTP server and all mails were sent successfully, that's a great thing but now am really confused why does when running extended procedure using our SMTP Server which is on the same domain the SMTP Server had no problem sending the mails but with Database Mail + our SMTP server it didn't work,why did it accept mails coming from the extended procedure and only allowed subsets of 12 emails at a time from Database Mail?
Yasmine
Anyways I'll stay now with the local SMTP solution until hopefully one day I may discover what's with sending 12 emails at a time issue.Thanks a lot for your help.
Yasmine
Don't know, maybe check with your network / SMTP admin. It could have something to do with the way Database Mail works diferently... it queues mail rather than firing them directly. This is supposed to help with concurrency but if they are going out as a set instead maybe the SMTP server has some "per-second" prevention. Again, this is definitely from the SMTP side. Are you using Windows SMTP services, or some other SMTP server? If you don't have an SMTP admin, I'm sure you can get some help with your SMTP server, maybe you should check out serverfault.com.
Aaron Bertrand
Another thing you might want to try is checking the SMTP authentication method for the Database Mail account you are using. I was setting up an account today and almost left it at "Anonymous authentication" and my e-mail would have failed if I hadn't noticed and changed it to Windows Auth. Yours would fail outright if it was a permission issue, but maybe there is a quota policy in place that you are hitting. Hard to guess more without knowing more details about your setup.
Aaron Bertrand