Social Icons

Pages

Friday, July 20, 2012

Send Email From SQL Database

In this article, I share the script for send the Email using SQL Server.

USE Master
GO
sp_configure 'show advanced options', 1
GO
reconfigure with override
GO
sp_configure 'Database Mail XPs', 1
GO
reconfigure
GO
sp_configure 'show advanced options', 0
GO

-- Create new profile--
EXECUTE msdb.dbo.sysmail_add_profile_sp
@profile_name = 'EmailAlert',
@description = 'SQL EmailAlert '

-- Set the New Profile as the Default--
EXECUTE msdb.dbo.sysmail_add_principalprofile_sp
@profile_name = 'EmailAlert',
@principal_name = 'public',
@is_default = 1 ; -- Make this the default profile
GO

-- created the account--
EXECUTE msdb.dbo.sysmail_add_account_sp
@account_name = 'EmailAlert',
@description = 'my email description',
@email_address = 'test@gmail.com',--Email address
@display_name = 'Email display name',--Email display name
@replyto_address = null,
@mailserver_name = 'smtp.gmail.com',-- mail server
@username = 'test@gmail.com',--Email address
@password = '****',--Password
@port = 587, -- Port
@enable_ssl = true

-- Added account to the profile--
EXECUTE msdb.dbo.sysmail_add_profileaccount_sp
@profile_name = 'EmailAlert',
@account_name = 'EmailAlert',
@sequence_number = 1
GO

 --Send Test mail--
EXECUTE msdb.dbo.sp_send_dbmail
@recipients='test@gmail.com',--recipients email address
@subject = 'Subject',
@body = 'This is a test message sent from the SQL',
@reply_to = 'test@gmail.com'
GO

-- check the database table records--
SELECT *
FROM sysmail_mailitems
GO
SELECT *
FROM sysmail_log
GO
Reference: http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/

Tuesday, July 10, 2012

How to send an Email using C#


using System.Net.Mail;
using System.Net;

void SendEmail()
        {
            try
           {
           MailMessage msg = new MailMessage();
           msg.From = new MailAddress(SenderAddress);
           msg.To.Add(new MailAddress(ReceiverAddress));
           msg.Body = Bodymessage;
           msg.Subject = subject;
           msg.IsBodyHtml = true;
           System.Net.Mail.Attachment attachment;
           attachment = new System.Net.Mail.Attachment(imagepath);
           msg.Attachments.Add(attachment);
           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.Port = 587;
           smtp.Credentials = new NetworkCredential(NetworkCredential mailid, NetworkCredential password);
           smtp.EnableSsl = true;
           smtp.Send(msg);
           }
           catch (Exception)
           {
            MessageBox.Show("Error in sending the mail", "Error");

           }
        }

How to Send SMS using GSM Modem in C#

This article explains in the simple way to send the SMS using GSM Modem in C#.

1.  First, connect the GSM Modem and find the COM port. To do this
  • Click on the Start button in the bottom left corner of your Screen. (shortcut Win+R)
  • Then type "devmgmt.msc", and hit Enter.
  • If Windows asks for your permission to continue, click on Continue.
  • When the device manager shows up, Find the entry that says "Ports (COM & LPT)" and click on the (+) beside it to expand it.
  • The COM port number is shown in parentheses beside the name.
 2.  Create a new Windows forms application and add three textboxes for Mobile number(txtmobile),Port number(txtport) and message(txtmessage).

3.  Add the following namespace.
  using System.IO.Ports;
 4.  Then add a button control(butSend) to your form and write the code in the button_click event.
          SerialPort serialport = new SerialPort();
           try
           {
               int mSpeed = 1;
               serialport.PortName = txtport.Text;
               serialport.BaudRate = 96000;
               serialport.Parity = Parity.None;
               serialport.DataBits = 8;
               serialport.StopBits = StopBits.One;
               serialport.Handshake = Handshake.XOnXOff;
               serialport.DtrEnable = true;
               serialport.RtsEnable = true;
               serialport.NewLine = Environment.NewLine;
             try
                    {
                        serialport.Open();
                    }
                    catch (Exception)
                    {
               MessageBox.Show("Try another Port."+ Environment.NewLine +"Phone not detected or The requested resource is in use.","CONNECTION ERROR",   MessageBoxButtons.OK,MessageBoxIcon.Error);
                     return;
                    }

               serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);
               System.Threading.Thread.Sleep(200);
               serialport.WriteLine("AT+CSCS=GSM" + Environment.NewLine);
               System.Threading.Thread.Sleep(200);
               serialport.WriteLine("AT+CMGS=" + (char)34 + txtmobile.Text
               + (char)34 + Environment.NewLine);
               System.Threading.Thread.Sleep(200);
               serialport.WriteLine(txtmessage. Text + (char)26);
               System.Threading.Thread.Sleep(mSpeed);
               serialport.Close();
           }
           catch (Exception)
           {
               if (serialport.IsOpen)
               serialport.Close(); 
            MessageBox.Show("Couldn't send the SMS.","CONNECTION ERROR",MessageBoxButtons.OK,MessageBoxIcon.Error);
           }
 5.  Run the program
      Enter the
Mobile number,COM Port and message and click on send buton to send the SMS.

Some useful articles:
1. http://codeglobe.blogspot.co.uk/2009/02/sending-sms-in-cnet-using-gsm-modem-and.html
2. http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem