Database Mail Fails For SQL Server Agent Jobs / Maintenace plans - Why ?

Database Mail Fails For SQL Server Agent Jobs / Maintenace plans - Why ?

This has been a very common question I have seen relatedt to Database mail and I thought will post here so if you run into this issue, it could be due to one of the following and also have mentioned below how to get around them. Bottom line is datbase mail works fine with SQL Agent jobs and maintenace plans in SP1 on all platforms.

1. If you see this on 64 bit platform and on RTM build, this is a bug that is addressed in SP1.

2. If you see the below error message, it is known issue and this is because of design that requires SQL Agent to restart, if SQL Agent was started before enabling databasemail and setting a new profile. We are evaluating to change this design of not requiring to restart agent for our future release.

Re: 264] An attempt was made to send an email when no email session has been established
This problem will go away after restarting SQL Server Agent in this case.Thanks,

Gops Dwarak, MSFT

[1304 byte] By [GopsDwarak] at [2007-12-19]
# 1

Hello Gops..

I get the same error :- [264] An attempt was made to send an email when no email session has been established.

I tried restarting the SQL Agent as well as even the SQL Server Service. Still does not work. xp_sendmail works well. And the Database mail profile is declared as Public and Default. We are running on 64 bit Windows 2003 r2 with SP1 and 64 bit SQL 2005 Standard Edition Server with sp1.

Any clue on what else to be done / checked?

Regards

N.Raja

N.Raja at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...
# 2
Hello Gops,

it is no solution to my problem. Do you have any ideas for my problem at

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=882761 ?

Thanks
tobias

Tobi123 at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...
# 3

Any updates on this? I'm having the same problem on a Windows 2003 with SQL 2005 SP1.

Thanks,

Andr

AAraújo at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...
# 4

This may be related to the fact that DBMail, by default, shuts itself down after 10 minutes of inactivty. If the mail service isn't running, you won't be able to queue up any mail messages.

There may be a built-in mechanism for addressing this problem, but I found that I can detect this situation and start the mail listener before sending db mail. I use the following in stored procs that need to send email.

You could increase the inactivity period for db mail shutdown from 600 seconds to some larger value. It might not make sense to do so, since shutting it down releases its resources back to teh system; if your SQL server only sneds mail infrequently, it might be better to start the mail listener on demand. Someone with more experience than I might want to add their two cents' worth. :-)

/*********************************************************************/

/* Check the mail engine. Start the service if is is stopped */

/* -- the DB Mail engine shuts down after 10 minutes (of inactivity) */

/* -- this setting can be changed, but we probably don't need to. */

/*********************************************************************/

DECLARE @rc INT

IF NOT EXISTS (SELECT * FROM msdb.sys.service_queues

WHERE name = N'ExternalMailQueue' AND is_receive_enabled = 1)

EXEC @rc = msdb.dbo.sysmail_start_sp

-- TODO: add handling and logging for any engine start issues

Hirc at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...
# 5

Thank you for this.. it is a very reasonable assertion. I have a system that has jobs run on a nightly basis and is mostly inactive during the day. I also have a alert set to send messages every evening as a failsafe to make sure that notifications are working. It was a pain that the alert system would stop working after nearly a month of working, sending notifications every day.

Epikto at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...
# 6

Even with SP1 it doesnt work. (I tried it on both the standard and Enterprise edition)

Is there any other way of doing it without restarting SQL Agent?

If we restart the agent, what other things will be affected by it? Will it affect replication jobs and other scheduled jobs?

Thanks

RJDBA at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...
# 7
I am in option 2 but restarting the SQL Server Agent doesn't help. I get error messages in the Agent log during start up

  1. [392] Using MAPI32.DLL from C:\WINDOWS\SYSTEM32 (version 1.0.2536.0)
  2. [196] Attempting to start mail session using profile ‘agentJobs’...
  3. [260] Unable to start mail session (reason: Unable to logon (reason: MAPI Logon failed); check the mail profile and/or the SQLSERVERAGENT service startup account)

I don't understand why is trying a MAPI Logon when Database mail doesn't use MAPI (one of its theoretical strengths)

Any help welcomed

jmasramon at 2007-9-9 > top of Msdn Tech,SQL Server,SQL Server Tools General...

SQL Server

Site Classified