How to send SMS in asp.net :
Samples:
public String SendSms(string Message,string MobileNo)
{
String result = "";
if (ConfigurationManager.AppSettings["SmsFlag"] == "1")
{
DataView dvSmbcDetails = new DataView();
dvSmbcDetails = objUser.GetSmsDetails();
string SmsUserName = dvSmbcDetails[0]["SmsUserName"].ToString();
string FromEmail = ConfigurationManager.AppSettings["FromEmail"];
string SmsPassword = dvSmbcDetails[0]["SmsPassword"].ToString();
string SmsFrom = dvSmbcDetails[0]["SmsFrom"].ToString();
string SmsFlag = ConfigurationManager.AppSettings["SmsMode"];
String strPost = "uname=" + SmsUserName + "&pword=" + SmsPassword + "&message=" + Message + "&from=" + SmsFrom + "&selectednums=" + MobileNo + "&info=" + SmsFlag + "";
StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("http://www.txtlocal.com/sendsmspost.php");
//add Proxy info if required
//objRequest.Proxy = WebProxy.GetDefaultProxy();
//objRequest.Proxy.Credentials = CredentialCache.DefaultCredentials; //uses logged on user
//objRequest.Proxy.Credentials = new System.Net.NetworkCredential("UserName", "Password"); // Alternative - specify the user and password to use
objRequest.Method = "POST";
objRequest.ContentLength = Encoding.UTF8.GetByteCount(strPost);
objRequest.ContentType = "application/XXXXXX";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally
{
myWriter.Close();
}
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
string[] str = result.Split('=');
string credits = str[str.Length - 1].ToString();
credits = credits.Substring(0, 3);
// TestMode=0<br>MessageReceived=Dear Tim, The details you entered for eligiblity check for FSM for your childrens Dayton,Nyle,Ameyal were not found in FSM database<br>MessageCount=1<br>From=XXX<br>CreditsAvailable=128.8<br>MessageLength=1<br>NumberContacts=1<br>CreditsRequired=0.9<br>CreditsRemaining=127.9
// string[] lines = Regex.Split(result, "<br>");
// string[] AvailableCredit = lines[8].Split('=');
// string[] AvailableCredit1 = AvailableCredit[1].Split('.');
int BalanceAmount = Convert.ToInt32(credits);
if (BalanceAmount <= 150)
{
LoginUser objLoginUser = new LoginUser();
DataView dvGetAdminMailId = new DataView();
dvGetAdminMailId = objLoginUser.GetAdminMailId();
if (dvGetAdminMailId.Table.Rows.Count != 0)
{
for (int k = 0; k < dvGetAdminMailId.Table.Rows.Count; k++)
{
StringBuilder sbSubject = new StringBuilder();
StringBuilder sbBody = new StringBuilder();
sbSubject.Append("SMS credit status");
sbBody.Append(" <div> Your SMS credit less than:" + BalanceAmount + " amount <div>");
sbBody.Append("<br/>");
sbBody.Append(" <div> Thank you, </div>");
sbBody.Append(" <div> XXXX</div>");
string ToEmail = dvGetAdminMailId[k]["EmailId"].ToString();
SendMail(FromEmail, ToEmail, sbSubject.ToString(), sbBody.ToString());
}
}
}
}
return result;
}
//send email method was posted in my next blog
http://dotnet--solutions.blogspot.in/2012/12/send-email-in-aspnet-c.html
Thanks for sharing this blog. We have now started providing bulk SMS API C# India that is very powerful and easy to integrate into your own software/application/website. As it saves your lot of valuable time in logging our interface again and again.
ReplyDelete