简体   繁体   中英

Can I use SQL Database Mail to send an HTML page as a message via its URL, the same way you can with the CDO.Message object in VBScript?

I'm working on a project that uses an SQL trigger to fire off emails when certain fields are updated, and the emails need to be in HTML format with complex design, something that would be very difficult to pull off using the @body_format = 'HTML' command. As it stands, the only articles I've found have been about formatting the body of the message using HTML in the SQL code, but I'm looking to reference a URL and send the corresponding page as an HTML body.

I know that there's a way in VBScript to generate an HTML page and send it as a message by referencing its URL by using the .CreateHTMLBody command on a CDO.Message object, and that's the kind of functionality I'm looking for in SQL.

I'm using Microsoft SQL Server Management Studio.

Short answer no.... Long answer yes :)

Ways to do it.

1) If you build your page on the web then just store the html in a table and look it up. If it needs to change all of the time build the html in a template manner that you put some crazy text in certain locations ( @@FIRSTNAME@@ ) that you will REPLACE with your dynamic content and all of a sudden you are good to go and not too much difficult handling of the html.

2) Build a CLR with your VB code. this is more complex but on a plus side you can also send your message directly from here through an SMTP gateway and I have had issues where DB mail has retried and sent way too many messages to public users so I don't use it for heavy emailing at all!

3) Build a SSIS package and place your VB script inside a script task to generate the email, create a SQL agent job, then use tSQL to execute that job.

4) Call your VBScript directly xp_CmdShell or some other method straight from the trigger. This one is definitely the scariest impact from a security perspective but if you lock down access and only allow for certain user(s) it is not so so bad...... Here is an article that I haven't read in full but looks to be very exhaustive on the subject. https://www.simple-talk.com/sql/database-administration/the-posh-dba-solutions-using-powershell-and-sql-server/

Added benefit to using methods > 1. You can create a threaded application to send several thousand messages very quickly if needed! I literally send 2,000 messages from my DB server to Exchange server in under a minute because of a threaded clr.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM