Friday 27 January 2017

How to send SMS using C# and ASP.NET

We can send SMS to any phone no and anywhere in the world using Twilio.
what is Twilio?

Twilio allows to make and receive phone calls and send and receive text messages using its APIs. Programmers can implement Twilio APIs in their code and create all sort of applications involving phone calls and SMS. It’s services are accessed over HTTP and are billed based on usage. It can be used easily in our application for creating features like SMS authentication, OTP, SMS marketing and more.

STEP 1:- Create your account in Twilio to get API credentials.
STEP 2: – Reference the Twilio.Api.dll file in your website.
For adding the reference .We need to install nuget Package in Visual Studio.
After install nuget package.
click on Tool-->Nuget Package Manager-->package Manager Console.->Type ‘Install-Package Twilio’ and press enter key..
-------------------Default.aspx---------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function msg() {
            alert("Thank you for Showing Your interest");
        }

    </script>
    <style type="text/css">
        #text {
            width: 300px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <label>First Name</label></td>
                <td>
                    <input type="text" id="txtfName" required="required" class="text" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Last Name</label></td>
                <td>
                    <input type="text" id="txlName" class="text" required="required" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Email ID</label></td>
                <td>
                    <input type="email" id="txt" required="required" class="text" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Phone</label></td>
                <td>
                    <input type="tel" id="txtphone" required="required" class="text" runat="server" /></td>
            </tr>
            <tr>
                <td>
                    <label>Your Requirement </label>
                </td>
                <td>
                    <textarea rows="2" id="txtReq" class="text" runat="server">

</textarea></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="btnsend" runat="server" Text="Send" OnClick="btnsend_Click" />
                </td>
            </tr>

        </table>

    </form>
</body>
</html>
---------------------------------------------------Default.aspx.cs----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Twilio;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnsend_Click(object sender, EventArgs e)
    {
        string number = txtphone.Value.Trim();
        string msg = txtReq.Value.Trim().ToString();
        string AccountSid = "******";
        string AuthToken = "***********";
        var twilio = new TwilioRestClient(AccountSid, AuthToken);
        var message = twilio.SendMessage("From number", number, msg, ""); 
       
        ClientScript.RegisterStartupScript(this.GetType(), "Alert", "msg()", true);

    }
}

Note:- 1)Add using Twilio in code behind .
2)For Account Sid,AuthToken and From number we need to register your self on https://www.twilio.com/.


How to get logged in User's Security Roles using Java Script in dynamic CRM 365.

 function GetloggedUser () {     var roles = Xrm.Utility.getGlobalContext().userSettings.roles;      if (roles === null) return false;      ...