title: Sending SMS with GSM Modem from SQL Server 2008 R2
body: <p>I want to send SMS using GSM modem's AT-Commands from SQL Server 2008, so I wrote a Visual C# SQL CLR Database Project and a stored procedure.</p>
  
  <p>But when I execute that stored procedure I received this error:</p>
  
  <blockquote>
    <p>Request for the permission of type
    'System.Security.Permissions.SecurityPermission, mscorlib,
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
    failed.</p>
  </blockquote>
  
  <p>The source codes of stored procedure and the class for sending sms are these:</p>
  
  <pre><code>[Microsoft.SqlServer.Server.SqlProcedure]
  public static void SendSMS2(string phone, string message)
  {
      SMSManagement.SM2S ss = new SMSManagement.SM2S();
      ss.SendMessage(phone, message);
  }
  
  public void SendMessage(string PhoneNumber, string Message)
  {
      try
      {
          SerialPort port = new SerialPort();
  
          port.PortName = "COM5";                
          port.BaudRate = 115200;                  
          port.DataBits = 8;                
          port.StopBits = StopBits.One;     
          port.Parity = Parity.None;        
          port.ReadTimeout = 300;           
          port.WriteTimeout = 300;          
  
          // Error occurred in here.
          port.Open();
          port.DtrEnable = true;
          port.RtsEnable = true;
  
          SMS sms = new SMS();
          sms.Direction = SMSDirection.Submited;
          sms.PhoneNumber = PhoneNumber;
          sms.ValidityPeriod = new TimeSpan(4, 0, 0, 0);
          sms.Message = Message;
  
          Message = sms.Compose(SMS.SMSEncoding.UCS2);
  
          ExecCommand(port, "AT", 300, "No phone connected");
          ExecCommand(port, "AT+CMGF=0", 300, "Failed to set pdu format.");
          ExecCommand(port, "AT+CMGS=1", 300, "Failed to set message length.");
          string command = Message + char.ConvertFromUtf32(26);
          ExecCommand(port, command, 6000, "Failed to send message"); 
  
          port.Close();
      }
      catch (Exception ex)
      {
          SqlContext.Pipe.Send(ex.Message);
      }
  }
  </code></pre>
  
  <p>The version of Visual Studio and SQL Server is 2010 SP1 and 2008 R2.
  The CLR project framework is 2.0 .</p>