Upload
rmo1006
View
242
Download
0
Embed Size (px)
Citation preview
7/25/2019 Configuring Database Mail in SQL Server
1/17
Confguring Database Mail in SQLServer
Kareem.S,30 Dec 2013 CPOL 116K 1
!"6# $23 votes%
&ate t'is( vote 1 vote 2 vote 3 vote ! vote )'is is an enter*rise solution +or sening -ails +ro- t'e SQL Server atabaseengine to SM)P servers"
.ntrouction
)'is is an enter*rise solution +or sening -ails +ro- t'e SQL Server atabaseengine to SM)P servers" SQL Server atabase a**lications can co--unicate /it'users t'roug' an e-ail sste-" .t *rovies +eatures lie scalabilit, securit, anreliabilit"
.t uses an SM)P server to sen -ail" SQL Server 2000 su**orts SQL Mail, /'ic'su**orts MP. *rofles to sen e-ail instea o+ an SM)P server" SQL Mail reuires aMP.4co-*liant -ail server $Microso+t 5c'ange Server% an a MP. client$Microso+t Outloo%"
7e can sen a tet -essage, uer result, fle as attac'-ent" )'e atabase -ailcan be use to noti+ users or a-inistrators regaring events raise in SQLServer" 8or ea-*le, i+ an auto-ation *rocess lie re*lication, atabase -irroring+ails or t'ere are latenc relate *roble-s t'en SQL Server can use t'is +eature tonoti+ t'e a-inistrators or o*erators"
Points to &e-e-ber Lie SQL Mail, atabase -ail oesn9t reuire a MP. : a co-*liant -ail
server lie Outloo 5*ress or etene *rogra--ing inter+ace" ;etter *er+or-ance" .-*act o+ sening -ails to SM)P servers b SQL Server
is reuce as t'is tas is i-*le-ente b an eternal *rocess initiate bt'e DatabaseMail.exefle"
7ors fne in a cluster base environ-ent"
http://www.codeproject.com/script/Membership/View.aspx?mid=9535886http://www.codeproject.com/script/Membership/View.aspx?mid=9535886http://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Serverhttp://www.codeproject.com/info/cpol10.aspxhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Serverhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Serverhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Serverhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Serverhttp://www.codeproject.com/Articles/485124/Configuring-Database-Mail-in-SQL-Serverhttp://www.codeproject.com/script/Membership/View.aspx?mid=95358867/25/2019 Configuring Database Mail in SQL Server
2/17
6!4bit su**ort"
Database -ail confguration in+or-ation is -aintaine inan MSDBatabase"
Onl -e-bers o+ SysAdminan DatabaseMailUserRole atabase roleo+MSD; can sen -ails b e+ault"
llo/s sening -essages in i
7/25/2019 Configuring Database Mail in SQL Server
3/17
Figure 1 (Source: BOL) Database Mail Architecture
How it works?
When a run time error occurs due to an automated tas! li!e bac!u"s#re"lication etc database engine raise the error and same in$ormation is
submitted to Database Mail engine# then database mail engine %illsubmit the mail to SM&' Serer using mail*D and 'ass%ord mentioned in"ro+le, At the last SM&' Serer sends mail to reci"ients,
rror --. DB ngine --. DB Mail ngine --. SM&' Serer --. /eci"ients
FAQ: o! to enable a Service Broker in MSDB"
=ie Co* Coe
>S5 ?-aster@AOL)5& D);S5 ?MSD;@ S5) 5B;L5;&OK5& 7.)= BO7.)
AO
MSDB tables related to Database Mail
1" sysmail_pro#le( Consists o+ all t'e *rofles in+or-ation"2" sysmail_acco$nt( Consists o+ SM)P server accounts in+or-ation"3" Sysmail_server( Consists o+ SM)P server etails"!" Sysmail_allitems( Mail sent status" .+ t'e sentstatus is 1 t'en success,
ot'er/ise +aile"
7/25/2019 Configuring Database Mail in SQL Server
4/17
" Sysmail_lo%( )o c'ec t'e errors raise b Database Mail +eature"6" Sysmail_con#%$ration( Consists o+ sste- *ara-eter etails"
Steps to con#%$re
1" 5nable t'e b -ail +eature at server level
"=ie Co* Coe
s*confgure Database Mail EPs,1reconfgure
2" 5nable service broer in t'e MSD; atabase"=ie Co* Coe
>S5 ?-aster@AOL)5& D);S5 ?MSD;@ S5) 5B;L5;&OK5& 7.)= BO7.)AO
3" Confgure -ail *rofle $*rofle is a collection o+ accounts%"!" SM)P account$s%"" Mae t'e *rofle *rivate or *ublic"
Private *rofle can be use b(
o sysadmin-e-bers ano databasemail$serrole-e-bers o+ MSD;6" Set *ara-eters"F" Sen t'e -ail"
&'ample: (on#%$rin% Database Mail
1" Ao to ObGect 5*lorer2" Manage-ent3" &ig't clic on Database Mail an select HConfgure Database MailI as +ollo/s
7/25/2019 Configuring Database Mail in SQL Server
5/17
!" Bet" Select HSetu* Database Mail b *er+or-ing t'e +ollo/ing tassI as +ollo/s
6" BetF" 5nter *rofle na-e J HSQL ProfleI an escri*tion as +ollo/s
7/25/2019 Configuring Database Mail in SQL Server
6/17
" Clic on HI button an enter t'e +ollo/ing etails" l/as use our o/ne-ail .D" Aenerall /e 'ave to use 'ere t'e co-*an e-ail i" 7e 'ave to raise aticet to t'e -ail server a-in tea- to get t'e +ollo/ing etails"
o Se*arate e-ail i +or SQL Server $)'is is 8ro- 5-ail .D%o SM)P server na-eo Port nu-bero SSL +eature s'oul be enable or isable"
=ere . a- using - *ersonal e-ail i" .n ;asic ut'entication o*tion enter t'esa-e e-ail .D along /it' t'e vali *ass/or o+ t'e e-ail .D"
7/25/2019 Configuring Database Mail in SQL Server
7/17
#" OK 10" Bet11" >ner Manage Profle Securit o*tion -ae t'e *rofle as *ublic b selecting
c'ecbo an e+ault as +ollo/s
12" Bet13" cce*t t'e e+ault settings +or Sste- Para-eters as +ollo/s
1!" Bet1" 8inis'
7/25/2019 Configuring Database Mail in SQL Server
8/17
16" Close"
)bservations
=ie Co* Coe
>se -sbAo44Ste*1( ari+ing t'e ne/ *rofle select +ro- sysmail_profle44Ste*2( eri+ing accounts select +ro- sysmail_acco$nt44Ste*3( )o c'ec t'e accounts o+ a *rofle select +ro- sysmail_pro#leacco$nt
/'ere pro#le_idJ344Ste*!( )o is*la -ail server etailsselect +ro- sysmail_server
1F" 7e 'ave confgure atabase -ail +eature success+ull" Lets test t'e -ail+eature as +ollo/s"
1" Ao to ObGect 5*lorer 4N Manage-ent 4N rig't clic on Database Mail 4NSen )est 5-ail
7/25/2019 Configuring Database Mail in SQL Server
9/17
1#" 5nter t'e +ollo/ing etails" ou can use reuire values"o Select Profle na-e( SQLProfleo )o( as"slserverg-ail"co- $n 5-ail .D4 generall it s'oul be
a-inistrators grou* e-ail .D%o SubGect( =i, ;acu* lerto ;o( ;acu* o+ -aster atabase /as generate success+ull"
20" Sen )est 5-ail 4N OK21" eri+ing /'et'er t'e -ail /as sen success+ull or not" )ae ne/ uer an
run t'e +ollo/ing co--an an c'ec sentstatus colu-n value +or our -ail as
+ollo/s
mailto:[email protected]:[email protected]7/25/2019 Configuring Database Mail in SQL Server
10/17
22" C'ec our -ail bo ou can fn ne/ 5-ail +ro- SQL Server"
Sendin% Mail $sin% S*_S&+D_DBMA,-
7e can sen t'e -ail *rogra--aticall +ro- an store *roceure, Gob or batc'fle using s*senb-ail sste- *roceure" ;e+ore sening t'e -ail /e can c'ect'e reuire *ara-eters b vie/ing t'e efnition o+ store *roceure"
7e 'ave to *ass t'e +ollo/ing *ara-eters to t'e above store *roceure"
Proflena-e $7e can -ention t'e above *rofle na-e /'ic' /e 'avecreate%
&eci*ients $7e can -ention -ulti*le reci*ients b se*arating /it'
srcJ'tt*(RR///"coe*roGect"co-Rscri*tR8oru-sR.-agesRs-ile/in"gi+ RN SubGect
;o
7/25/2019 Configuring Database Mail in SQL Server
11/17
5a-*le 1( Sening -ail using t'e s*senb-ail store*roceure
Ste* 1(
Ste*2( eri+ing using ss-ailallite-s" =ere c'ec t'e sentstatus colu-n o+ last
5-ail"
=ie Co* Coe
use -sbgoselect +ro- ss-ailallite-s
5a-*le 2( Sening -ail using s*senb-ail store*roceure /'ic' 'as uer result as -essage"
Ste* 1( Sening -ail $. a- using e-* table *resent in )est atabase%
=ie Co* Coe
5E5Cmsdb"bo"s*senb-ail*roflena-eJ SQLProfle,recipientsJ s-"-c*g-ail"co-,/$eryJ S5L5C) CO>B)$% as Bo5-*ls 8&OM
)est"bo"e-*,s$b0ectJ Bo o+ 5-*loees 7oring
Ste* 2( eri+ing using ss-ailallite-s" =ere c'ec t'e sentstatus colu-n o+ last5-ail"
=ie Co* Coeusemsdbgoselect +ro-ss-ailallite-s
7/25/2019 Configuring Database Mail in SQL Server
12/17
Confguring Database Mail >sing )4SQLScri*t
=ie Co* Coe
--Enabling Database Mails*confgure s'o/ avance o*tions,1reconfgure go s*confgure Database Mail EPs,1 reconfgure
=ie Co* Coe
--Creating a Profle5E5C>)5-sb"bo"ss-aila*rofles**roflena-eJ SQLProfle,escri*tionJ Mail Service +or SQL Server
=ie Co* Coe
-- Create a Mail account or gmail. !e "a#e to use our company mail account.5E5C>)5-sb"bo"ss-ailaaccounts*accountna-eJ SQL5-ailccount,e-ailaressJ oure-ailg-ail"co-,-ailserverna-eJ s-t*"g-ail"co-,*ortJF,enablesslJ1,userna-eJoure-ail,*ass/orJ5-aili *ass/or
=ie Co* Coe
-- $%%ing t"e account to t"e profle5E5C>)5-sb"bo"ss-aila*rofleaccounts**roflena-eJ SQLProfle,accountna-eJ SQL5-ailccount,
seuencenu-berJ1 =ie Co* Coe
-- &ranting access to t"e profle to t"e DatabaseMail'ser(ole o M)D*5E5C>)5-sb"bo"ss-aila*rinci*al*rofles**roflena-eJ SQLProfle,*rinci*aliJ 0,ise+aultJ 1
=ie Co* Coe
--)en%ing +est Mail5E5C-sb"bo"s*senb-ail*roflena-eJ SQLProfle,reci*ientsJ oure-aili"co-,boJ Database Mail )esting""",subGectJ Databas Mail +ro- SQL Server
=ie Co* Coe
--,eriying c"eck status columnselect +ro-ss-ailallite-s
Su--ar
7/25/2019 Configuring Database Mail in SQL Server
13/17
Database Mail +eature /as introuce in SQL Server 200 version, /'ic' can beuse to noti+ t'e a-inistratorsRo*erators" .t *rovies better *er+or-ance as /ellas cluster a/are +eature"
M. Scri*t e ConfguraciTn
445nabling Database Mails*confgures'o/ avance o*tions,1reconfgure go s*confgureDatabase Mail EPs,1reconfgure
44Creating a Profle5E5C>)5-sb"bo"ss-aila*rofles**roflena-eJSQLProfle,escri*tionJMail Service +or SQL Server
44 Create a Mail account +or g-ail" 7e 'ave to use our co-*an -ail account"5E5C>)5-sb"bo"ss-ailaaccounts*accountna-eJCaGaCristal,e-ailaressJcaGacristalsclg-ail"co-,-ailserverna-eJs-t*"g-ail"co-,*ortJF,enablesslJ1,userna-eJcaGacristalsclg-ail"co-,*ass/orJccristalscl
44 ing t'e account to t'e *rofle5E5C>)5-sb"bo"ss-aila*rofleaccounts**roflena-eJSQLProfle,
accountna-eJCaGa Cristal,seuencenu-berJ1
44 Aranting access to t'e *rofle to t'e DatabaseMail>ser&ole o+ MSD;5E5C>)5-sb"bo"ss-aila*rinci*al*rofles**roflena-eJSQLProfle,*rinci*aliJ0,ise+aultJ1
44Sening )est Mail5E5C-sb"bo"s*senb-ail*roflena-eJSQLProfle,reci*ientsJr-artineU1006*roig"net"-,
boJDatabase Mail )esting""",subGectJDatabas Mail +ro- SQL Server
44Sening )est Mail5E5C-sb"bo"s*senb-ail*roflena-eJSQLProfle,reci*ientsJr-artineU1006infnitu--ail"co-,boJDatabase Mail )esting""",subGectJDatabas Mail +ro- SQL Server
7/25/2019 Configuring Database Mail in SQL Server
14/17
44eri+ing, c'ec status colu-nselect+ro--sb"bo"ss-ailallite-s
-- Email Querry--
DECLARE @Body varchar(max)
declare @TableHead varchar(max)
declare @TableTail varchar(max)
declare @mailitemid a! i"t
declare @!tatu!#!$ a! varchar(max)
declare @Error a! varchar(max)
declare @%ote a! varchar(max)
&et %oCou"t '"
!et @mailitemid "ull!et @!tatu!#!$ "ull
!et @Error "ull
!et @%ote "ull
&et @TableTail *+,table+,body+,html*
--HT#L layout--
&et @TableHead *+html+head* .
*+H/ !tyle0color1 23333330HEADER '4 TABLE+,H/* .
*+!tyle* .
*td 5border1 !olid blac6 /7x7addi"$-le8t197x7addi"$-ri$ht197x7addi"$-to71/7x7addi"$-bottom1/7x8o"t-
!i:e1;7tcolor1Blac6< * .
*+,!tyle* .
*+,head* .*+body+table cell7addi"$3 cell!7aci"$3 border3* .
*+tr b$color24=AC9D*.
*+td ali$"ce"ter+b%ame o8 colum"+,b+,td* .
*+td ali$"ce"ter+b%ame o8 colum"+,b+,td* .
*+td ali$"ce"ter+b%ame o8 colum"+,b+,td* .
*+td ali$"ce"ter+b%ame o8 colum"+,b+,td* .
*+td ali$"ce"ter+b%ame o8 colum"+,b+,td+,tr*
--&elect i"8ormatio" 8or the Re7ort--
&elect @Body (&elect
Colum" A! >TD?
Colum" A! >TD?
Colum" A! >TD?Colum" A! >TD?
Colum" A! >TD?
4R'# >DB?>dbo?>Table?
here -co"ditio"-
(hatever you a"t to do el!e )
4or #L ra(*tr*) Eleme"t!)
7/25/2019 Configuring Database Mail in SQL Server
15/17
-- Re7lace the e"tity code! a"d ro "umber!
&et @Body Re7lace(@Body *x333* !7ace(/))
&et @Body Re7lace(@Body *x33D* **)
&et @Body Re7lace(@Body *+tr+TRRo/+,TRRo* *+tr b$color2C=C444*)
&et @Body Re7lace(@Body *+TRRo3+,TRRo* **)
&et @Body @TableHead . @Body . @TableTail
-- retur" out7ut--
&elect @Body
EEC m!dbdbo!7!e"ddbmail
@7ro8ile"ame email* +--Thi! i! the mail accou"t to !e"t 8rom
@mailitemid @mailitemid out
@reci7ie"t!*blah@blahco:a*
@!ubFect *!ubFect Email*
@body @Body
@body8ormat *HT#L*
Ghy "ot u!i"$ C&&Exam7le1
DECLARE @body#!$ "varchar(max)DECLARE @!ubFect "varchar(max)DECLARE @tableHT#L "varchar(max)
&ET @!ubFect *Query Re!ult! i" HT#L ith C&&*
&ET @tableHT#L %*+!tyle ty7e0text,c!!02box-table58o"t-8amily1 0Lucida &a"! I"icode0 0Lucida Jra"de0 &a"!-&eri88o"t-!i:e1 /7xtext-ali$"1 ce"terborder-colla7!e1 colla7!eborder-to71 K7x !olid 2;ba88/border-bottom1 K7x !olid 2;ba88/Ty7e?**
td >Cate$ory? **td C'%MERT(MARCHAR(3)>&tartDate?/3) **td C'%MERT(MARCHAR(3)>E"dDate?/3)4R'# >Adve"tureGor6!?>&ale!?>&7ecial'88er?'RDER BN >&7ecial'88erD?4'R #L OATH(*tr*) TNOE) A& %MARCHAR(#A) ) .%*+,table*
EEC m!dbdbo!7!e"ddbmail @reci7ie"t!*A"y#ailNouGa"t@&Pl!Coolcom*@!ubFect @!ubFect@body @tableHT#L@body8ormat *HT#L*
ENVIAR IMAGEN
declare@body1 varchar(4000)set@body1 =' Embedded Logo Example
+y,eader
7/25/2019 Configuring Database Mail in SQL Server
17/17
'E-E.msdb/dbo/spse!ddbmail
@pro*ile!ame='+ye*a%lt Email$ro*ile' @recipie!ts='loell@somedomai!/!et' @s%b2ect ='3l 5006 email test' @body =@body1
@body*ormat =',7+L' @8%ery ='3ELE.7 top 9 : *rom sysob2ects here xtype='';''' @8%eryres%ltheader =0 @excl%de8%eryo%tp%t =1 @appe!d8%eryerror =1 @attach8%eryres%ltas*ile =1 @8%eryattachme!t*ile!ame ='res%lts/txt' @8%eryres%lt!opaddi!g =1 @*ileattachme!ts ='.s8lserverce!trallogo/gi*'