Sunday, August 13, 2017

Send Email Using SQL Server

In this article I will explain how to send email using SQL server. For this you need to have SQL server enterprise edition. It uses SMTP server to communicate with user.

Following are step to send Email using SQL Server.

Step 1:

You need to configure Profile and Account in 'Database Mail' under 'Management' node .



Click on 'Configure Database Mail' will open below wizard.











If Profile is configured for first time, click on 'Yes'







Set Profile Name and Description. Click on add button to create Database Mail account.












Click Next.









Click on finish will set up account and profile.





Close wizard. Now you can send a test mail.



You will get email in your Inbox.


Step 2:

After creating mail profile and testing of mail, you can write query for executing mail by simply passing parameters to stored procedure called 'sp_send_dbmail'. This stored procedure is in the msdb database. Following is example.


declare @body1 varchar(4000)
set @body1 = 'This is test mail sent from SQQL 2008  '

EXEC msdb.dbo.sp_send_dbmail
    @profile_name='MailTutorial',
    @recipients='isalmankavish@gmail.com',
    @subject = 'Mail Tutorial',
    @body = @body1,
    @body_format = 'HTML',

Here you have different option such as attaching file in an email. also you can schedule above query to send mail at particular interval of time.

for further study about 'sp_send_dbmail' you can visit to MSDN website.
https://msdn.microsoft.com/en-IN/library/ms190307.aspx

Currency Converter using C#

In many project there is need to convert one currency to other currency specially in transaction application. There are ready made third party tools or Web-API by which we can convert currency, but they are paid. In this particular post i will provide a C# code through which one perform currency conversion. For this i have used yahoo finance currency conversion API which gives you real time currency exchange value.

Here we go.

you just need to pass three parameters to following method and you will get output as converted rate from fromCurrency To toCurrency.

    public string CurrencyConversion(decimal amount, string fromCurrency, string toCurrency)
    {
        string Output = "";
        const string fromCurrency1 = "USD";
        const string toCurrency1 = "INR";
        const double amount1 = 2000;

        // For other currency symbols see http://finance.yahoo.com/currency-converter/
        // Construct URL to query the Yahoo! Finance API

        const string urlPattern = "http://finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1";
        string url = string.Format(urlPattern, fromCurrency1, toCurrency1);

        // Get response as string
        string response = new WebClient().DownloadString(url);

        // Convert string to number
        double exchangeRate =
            double.Parse(response, System.Globalization.CultureInfo.InvariantCulture);

        // Output the result
        Output=(amount1*exchangeRate).ToString();

        return Output;
    }

Thank you.