The following steps can be taken to use aspNetEmail from inside of SQL Server.
1)Make sure database Trustworth is set to on. This is found in the database properties. To set it call:
ALTER DATABASE "database name" SET TRUSTWORTHY ON;.
Here is a real example:
ALTER DATABASE AdventureWorks SET TRUSTWORTHY ON;.
2) Because aspNetEmail depends upon System.Web, you will need to set a reference to it in SQL Server.
This can be done by executing:
CREATE ASSEMBLY SystemWeb from 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Web.dll'
with permission_set = unsafe
Be sure the path to the System.Web.dll matches the path on your server machine.
3)To import aspNetEmail into SQL server, expand the Programmability --> Assemblies folder (under your Database name).
Right-click the Assemblies folder, and select "New Assembly"
For perssion set, select "unrestricted"
For path, browse to the aspNetEmail filesystem location. By default, it is located under c:\program files\advancedintellect\aspNetEmail\
Set the permission set to Unrestricted
4)Verify your SQL Server is CLR enabled by running the following T-SQL statement:
sp_configure 'clr enabled',1
followed by either a server stop/re-start, or executing:
5)Open Visual Studio, and create a SQL database project
6)In the Visual Studio Solution Explorer pane, right-click References, and select "Add Reference".
7)The Add References dialog box appears.
8)Under the SQL Server tab, select aspNetEmail.
9)Choose Project->Add User Defined Function, and name the .cs file anything you like, such as "EmailHelper.cs".
Add the SendEmail() method so your code looks something like:
public partial class UserDefinedFunctions
public static SqlString SendEmail()
SqlString result = new SqlString("success");
EmailMessage msg = new EmailMessage("127.0.0.1");
msg.From = "email@example.com";
msg.To = "firstname.lastname@example.org";
msg.Subject = "My test email from sql server set at: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
msg.Body = "put the contents of your email body here...";
Once that is complete, you can now use your new function like any other User Defined T-SQL function. For example,
This method can easily be modified to accept various parameters (such as recipients) to send emails to different people.