Showing posts with label Setup. Show all posts
Showing posts with label Setup. Show all posts

Thursday, February 6, 2014

Utilizing smtp4dev with SQL Server Database Mail for Testing

SQL Server has database mail, a solution which would allow us to send e-mail from our database server. It allows us to send either e-mail in html or text format to users. It’s great because we can use it to send query results to users (or ourselves). We can also configure SQL Server Agent to use database mail to send alerts to operator.

Database mail utilizes SMTP server to send the e-mails. In some cases, we might not have a readily available SMTP server in our environment (like in my home lab environment) or we might be block from using the SMTP server from our development environment for security reason. In those situations, I’ve been using smtp4dev. It is a neat little utility that would allow you to have a dummy SMTP server on your local computer. It is lightweight and also no installation is needed. When we run the utility, it would sit on the system tray and listen to port 25 (default SMTP port). It will receive the e-mail that get send to its way, without sending the e-mail to its final destination (the e-mail recipients). It is pretty handy when we need to run some tests.

Setting up database mail to work with smtp4dev is pretty simple.

  • Download smtp4dev from http://smtp4dev.codeplex.com.
  • Extract the file and save it on your system (assuming that it is your development system and it has the SQL Server).
  • Run smtp4dev. Notes: If you have Windows Firewall turned on, you might get a “Windows Security Alert” that ask you if you want to allow smtp4dev to communicate with either the private network, public network or both. Since normally I run smtp4dev on my development system with also has SQL Server that I want to have database mail turn on, I would just hit the cancel button.
  • If database mail in SQL Server has not been enable, we can enable it by using the following SQL script:
exec sp_configure 'show advanced', 1;
go
reconfigure;
go
exec sp_configure 'Database Mail XPs', 1;
go
reconfigure;
go
  • We then need to set up database mail profile, account and then associate the profile with the account in SQL Server. To do that, we can use the following script (based on the “Database Mail Simple Configuration Template”):
DECLARE @profile_name sysname,
@account_name sysname,
@SMTP_servername sysname,
@email_address NVARCHAR(128),
@display_name NVARCHAR(128),
@error_display NVARCHAR(500);

-- Profile name. Replace with the name for your profile
SET @profile_name = 'SMTP4Dev';

-- Account information. Replace with the information for your account.

SET @account_name = 'SMTP4Dev Account';
SET @SMTP_servername = 'localhost'; --- Since the smtp4dev is on local system.
SET @email_address = 'john.doe@email.com';
SET @display_name = 'John Doe';

-- Verify the specified account and profile do not already exist.
IF EXISTS (SELECT * FROM msdb.dbo.sysmail_profile WHERE name = @profile_name)
BEGIN
SET @error_display = 'The specified Database Mail profile (' + @profile_name + ') already exists.';
RAISERROR(@error_display, 16, 1);
GOTO done;
END;

IF EXISTS (SELECT * FROM msdb.dbo.sysmail_account WHERE name = @account_name )
BEGIN
SET @error_display = 'The specified Database Mail account (' + @account_name + ') already exists.';
RAISERROR(@error_display, 16, 1) ;
GOTO done;
END;

-- Start a transaction before adding the account and the profile
BEGIN TRANSACTION ;

DECLARE @rv INT;

-- Add the account
EXECUTE @rv=msdb.dbo.sysmail_add_account_sp
@account_name = @account_name,
@email_address = @email_address,
@display_name = @display_name,
@mailserver_name = @SMTP_servername;

IF @rv<>0
BEGIN
SET @error_display = 'Failed to create the specified Database Mail account (' + @account_name + ').';
RAISERROR(@error_display, 16, 1) ;
GOTO done;
END

-- Add the profile
EXECUTE @rv=msdb.dbo.sysmail_add_profile_sp
@profile_name = @profile_name ;

IF @rv<>0
BEGIN
SET @error_display = 'Failed to create the specified Database Mail profile (' + @profile_name + ').';
RAISERROR(@error_display, 16, 1);
ROLLBACK TRANSACTION;
GOTO done;
END;

-- Associate the account with the profile.
EXECUTE @rv=msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = @profile_name,
@account_name = @account_name,
@sequence_number = 1 ;

IF @rv<>0
BEGIN
SET @error_display = 'Failed to associate the speficied profile with the specified account (' + @account_name + ').';
RAISERROR(@error_display, 16, 1) ;
ROLLBACK TRANSACTION;
GOTO done;
END;

COMMIT TRANSACTION;

done:

GO
  • Now the moment of truth, we can test to see if it works by trying to send an e-mail using sp_send_dbmail
exec msdb..sp_send_dbmail @profile_name = 'SMTP4Dev',
@recipients = 'jane.doe@email.com',
@subject = 'Test E-mail',
@body = 'This is a test email.';

If everything works as plan hopefully on your tray notification, you would see the following message:

image

And if you open smtp4dev, you should see the following:

image

To view the e-mail, you can click on the “View” or “Inspect” button.

Some caveats that I’ve noticed:

  • I have Microsoft Office 2013 installed on my system, which includes Outlook 2013. For some reason, when I click the “View” button, I would get an error message “The attempted operation failed. An object could not be found.” and it just would not open the e-mail (if I try to open it using Outlook 2013). It is ok if I use Windows Live Mail 2012. I would normally use the “Inspect” button instead.
  • If you exit out smtp4dev, the e-mails that you have on the Messages tab will get deleted.

Cleaning Up

If you want to remove the database mail from your SQL Server, first you want to remove the profile association with account, the profile and the account. You can do that by using the following script:

DECLARE @profile_name sysname,
@account_name sysname,
@error_display nvarchar(500);

-- Profile name. Replace with the name for your profile
SET @profile_name = 'SMTP4Dev';

-- Account information. Replace with the information for your account.
SET @account_name = 'SMTP4Dev Account';

IF NOT EXISTS (SELECT * FROM msdb.dbo.sysmail_profile WHERE name = @profile_name)
BEGIN
SET @error_display = 'The specified Database Mail profile (' + @profile_name + ') does not exists.';
RAISERROR(@error_display, 16, 1);
GOTO done;
END

IF NOT EXISTS (SELECT * FROM msdb.dbo.sysmail_account WHERE name = @account_name)
BEGIN
SET @error_display = 'The specified Database Mail account (' + @account_name + ') does not exists.';
RAISERROR(@error_display, 16, 1);
GOTO done;
END

-- Start a transaction before deleting the profile account, profile and account.
BEGIN TRANSACTION ;

DECLARE @rv INT;

EXEC @rv=msdb.dbo.sysmail_delete_profileaccount_sp
@profile_name = @profile_name,
@account_name = @account_name;

IF @rv<>0
BEGIN
SET @error_display = 'Failed to delete the profile with the specified account (' + @account_name + ').';
RAISERROR(@error_display, 16, 1) ;
ROLLBACK TRANSACTION;
GOTO done;
END;

EXEC @rv=msdb.dbo.sysmail_delete_profile_sp
@profile_name = @profile_name;

IF @rv<>0
BEGIN
SET @error_display = 'Failed to delete the database mail profile (' + @profile_name + ').';
RAISERROR(@error_display, 16, 1) ;
ROLLBACK TRANSACTION;
GOTO done;
END;

EXEC @rv=msdb.dbo.sysmail_delete_account_sp
@account_name = @account_name;
IF @rv<>0
BEGIN
SET @error_display = 'Failed to delete the database mail account (' + @account_name + ').';
RAISERROR(@error_display, 16, 1) ;
ROLLBACK TRANSACTION;
GOTO done;
END;

COMMIT TRANSACTION;

done:

GO

Then after that, you can disabled the database mail by using the following script:

exec sp_configure 'Database Mail XPs', 0;
go
reconfigure;
go
exec sp_configure 'show advanced', 0
go
reconfigure;
go

Saturday, November 17, 2012

Installing 32-bit SQL Server on 64-bit Windows (x64)

Looking at the hardware and software requirements for SQL Server 2008, 2008 R2 and 2012 from Microsoft site, we should be able to install 32-bit version of SQL Server onto 64-bit (x64) Windows operating system.

If you have installation media or ISO image that contains both 32-bit and 64-bit version of SQL Server 2008, 2008 R2 or 2012, here’s how you can install the 32-bit SQL Server on 64-bit Windows (x64). When you insert the installation media, or mount the ISO image, in some cases you might need to double click on the setup.exe, you will be presented with the SQL Server Installation Center. Click on the Options link on the left.

image

You should then be able to select to install the 32-bit (x86) of the SQL Server.

image

You can kick off the 32-bit installation process, by running the following command:

setup.exe /action="install" /x86=true

As a matter of fact, you can use the X86=true in the command line or answer file if you want to have unattended installation of SQL Server 2008, 2008 R2 or 2012.

Once it has been installed, when you check the SQL Server version using:

SELECT @@VERSION

For SQL Server 2012 installation, you should get something like the following:

Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86)Feb 10 2012 19:13:17Copyright (c) Microsoft CorporationEnterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) (WOW64)

Noticed that it is WOW64 (Windows 32-bit on Windows 64-bit).

Saturday, May 5, 2012

SQL Server 2012: Merged Product Updates (Slipstream)

Yesterday I blogged on the Product Updates feature in SQL Server 2012, that blog entry can be found here. In that blog entry, I talked about firing the SQL Server 2012 setup along with the Product Updates parameters option from the command line. But what if we want to automatically run the setup with the Product Update feature every time we run the SQL Server 2012 setup? This article will show how to create a new SQL Server 2012 distribution media that will run the SQL Server 2012 setup and SQL Server 2012 Cumulative Update 1.

Here’re the steps:

  • Create a new folder. For this example, I create a new folder called SQL2K12 on C:\Setup
  • Copy the folders and files from the SQL Server 2012 media onto C:\Setup\SQL2K12 folder.
  • Create a new folder called Updates on C:\Setup\SQL2K12 folder.
  • Download the SQL Server 2012 Cumulative Updates 1 from Microsoft. You might want to download the 446573_intl_i386_zip.exe (SQLServer2012_RTM_CU1_kb2679368_11_0_2316_x86) and 446572_intl_x64_zip.exe (SQLServer2012_RTM_CU1_kb2679368_11_0_2316_x64 ).
  • Run both files to extract the update files. When it asks for the extract location, specify C:\Setup\SQL2K12\Updates as the location. Once you’ve done with this steps, you should have SQLServer2012-KB2679368-x86.exe and SQLServer2012-KB2679368-x64.exe on C:\Setup\SQL2K12\Updates folder.

image

  • Go to C:\Setup\SQL2K12\x64 folder. Locate and edit (open in notepad) the DefaultSetup.ini file. On this file, you can specify the parameters that you want to have when running the setup.exe. To enable the Product Updates feature and have it get the update from the Updates folder, you can put the following on the DefaultSetup.ini file (once the file has been updated, save and close it) :

;SQL Server 2012 Configuration File
[OPTIONS]

UpdateEnabled=TRUE
UpdateSource=.\Updates

Notes: With the above DefaultSetup.ini file, when you run the Setup.exe, it will present you with the SQL Server Installation Center screen.

image

Now, say that you want to bypass the SQL Server Installation Center screen and go right ahead to the installation process, you can add the parameter Action=Install. So your DefaultSetup.ini file would look like the following:

;SQL Server 2012 Configuration File
[OPTIONS]

Action=Install

UpdateEnabled=TRUE
UpdateSource=.\Updates

  • Do the same thing to the DefaultSetup.ini file on C:\Setup\SQL2K12\x86.

Testing

You are done, so when you run the Setup.exe and do the SQL Server 2012 install, after the Setup Support Rules screen, you should see:

image

Then again on the Ready to Install page, you should see the following:

image

After the installation process, when you check for the installed SQL Server 2012 version, you should see version 11.0.2316.0.

[image%255B3%255D.png]