using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Reflection;
using System.Web;
using System.Net;
using System.IO;
using System.Configuration;
using System.Diagnostics;
namespace framework_library
{
public class communication
{
///
/// Method to Send an Email
///
///
/// Optional From Address display name
/// true is successful
public static bool SendAnEmail(oEmail email, string fromDisplayName = "")
{
//Variable Declaration
bool result = true;
MailAddress MailTo;
MailAddress MailFrom;
MailMessage ObjEmail;
//SmtpClient ObjEmailSender;
try
{
//CVH 2019-02-28 Adding using statement
using (SmtpClient ObjEmailSender = new SmtpClient())
{
ConfigResolver.ConfigureSmtp(ObjEmailSender);
//instantiate values
ObjEmail = new MailMessage();
//ObjEmailSender = new SmtpClient();
//Assign the Subject
ObjEmail.Subject = email.Subject;
//Set Email to HTML
ObjEmail.IsBodyHtml = true;
//Add Body To Email
ObjEmail.Body = email.Body;
//Check if bcc address is provided
if (email.replyToAddress != null && email.replyToAddress != String.Empty)
{
ObjEmail.ReplyTo = new MailAddress(email.replyToAddress);
}
if (email.fromAddress != null && email.fromAddress != String.Empty)
{
/* CVH 2016-09-13 Set from address display name */
//add from Address
MailFrom = new MailAddress(email.fromAddress, fromDisplayName);
ObjEmail.From = MailFrom;
}
//Check if email address is provided
if (email.toAddress != null && email.toAddress != String.Empty)
{
//check email to
if (email.toAddress.Contains(";"))//check for more than one email
{
Array toAddressList = email.toAddress.Split(char.Parse(";"));
//enumerate email addresses
foreach (string addy in toAddressList)
{
MailTo = new MailAddress(addy);
//Add To Address
ObjEmail.To.Add(MailTo);
}
}
else
{
MailTo = new MailAddress(email.toAddress);
//Add To Address
ObjEmail.To.Add(MailTo);
}
}
//Check if cc address is provided
if (email.ccAddress != null && email.ccAddress != String.Empty)
{
MailAddress MailCC;
//check email to
if (email.ccAddress.Contains(";"))//check for more than one email
{
Array ccAddressList = email.ccAddress.Split(char.Parse(";"));
foreach (string addy in ccAddressList)
{
MailCC = new MailAddress(addy);
//Add CC Address
ObjEmail.CC.Add(addy);
}
}
else
{
MailCC = new MailAddress(email.ccAddress);
ObjEmail.CC.Add(MailCC);
}
}
//Check if bcc address is provided
if (email.bccAddress != null && email.bccAddress != String.Empty)
{
MailAddress MailBCC;
//check email to
if (email.bccAddress.Contains(";"))//check for more than one email
{
Array bccAddressList = email.bccAddress.Split(char.Parse(";"));
foreach (string addy in bccAddressList)
{
MailBCC = new MailAddress(addy);
//Add Bcc Address
ObjEmail.Bcc.Add(addy);
}
}
else
{
MailBCC = new MailAddress(email.bccAddress);
ObjEmail.Bcc.Add(MailBCC);
}
}
//Check if there are attachments
if (email.Attachments != null && email.Attachments.Count > 0)
{
//enumerate attachments
foreach (String attachment in email.Attachments)
{
Attachment attach = new Attachment(attachment);
//add attachment
ObjEmail.Attachments.Add(attach);
}
}
try
{
//send the email
ObjEmailSender.Send(ObjEmail);
if (ObjEmail.Attachments != null && ObjEmail.Attachments.Count > 0)
{
ObjEmail.Attachments.Dispose();
}
ObjEmailSender.Dispose();
}
catch (Exception ex)
{
exception.HandleException("communication", MethodBase.GetCurrentMethod().Name, ex, 0, false);
result = false;
}
}
}
catch (Exception ex)
{
exception.HandleException("communication", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["userId"], false);
throw ex;
}
return result;
}
///
/// bool to send an SMS
///
///
///
/// true is successful
public static bool SendSMSMessage(oSMS sms)
{
bool result = false;
bool SendingActive = false;
//assign values
SendingActive = (bool.Parse(ConfigurationManager.AppSettings["SendingActive"]));
string user = ConfigurationManager.AppSettings["GatewayUser"];
string password = ConfigurationManager.AppSettings["GatewayPassword"];
string replyAddress = ConfigurationManager.AppSettings["GatewayReplyEmail"];
string uid = System.Guid.NewGuid().ToString();
int application = int.Parse(ConfigurationManager.AppSettings["GatewayApplication"]);
int deliveryReport = int.Parse(ConfigurationManager.AppSettings["GatewayDeliveryReport"]);
int statusReport = int.Parse(ConfigurationManager.AppSettings["GatewayStatusReport"]);
int allowReply = int.Parse(ConfigurationManager.AppSettings["GatewayAllowReply"]);
string xml = String.Empty;
string url = String.Empty;
string postResult = String.Empty;
string formattedMobile = String.Empty;
try
{
if (SendingActive)
{
//format mobile number
formattedMobile = utils.FormatMobile(sms.Number.Replace(" ", ""), "+27");
//check number is at least 10 digits
if (formattedMobile.Length >= 10)
{
//return xml string
xml = ReturnXMLString(formattedMobile, sms.Body, user, password, replyAddress, uid, application, deliveryReport, statusReport, allowReply);
byte[] data = Encoding.ASCII.GetBytes("xmldata=" + xml);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["GatewayAddress"]);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream());
string responseData = reader.ReadToEnd();
reader.Close();
if (responseData.Contains("OK"))
{
result = true;
}
else //send failed sms report to email
{
oEmail mail = new oEmail();
mail.toAddress = ConfigurationManager.AppSettings["support"];
mail.Subject = "SMS Failed Report - sending to" + formattedMobile;
mail.fromAddress = ConfigurationManager.AppSettings["from"];
mail.Body = responseData + "
" + sms.Body;
communication.SendAnEmail(mail);
}
}
}
}
catch (Exception ex)
{
result = false;
//handle exception
exception.HandleException("communication", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["userId"]);
}
return result;
}
///
/// String to return the xml
///
///
///
/// the xml in a string format
private static string ReturnXMLString(string mobile, string message, string user, string password, string replyEmail, string uid, int application, int deliveryReport, int statusReport, int allowReply)
{
string result = String.Empty;
try
{
//build xml string
result += "";
result += "";
result += "";
result += "";
result += "" + message + "";
result += "";
result += "";
result += "";
result += "";
}
catch (Exception ex)
{
//handle exception
exception.HandleException("communication", MethodBase.GetCurrentMethod().Name, ex, HttpContext.Current.Session["userId"]);
}
return result;
}
}
}