154 lines
6.6 KiB
Plaintext
154 lines
6.6 KiB
Plaintext
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net.Mail;
|
|
using System.Collections;
|
|
using MySql.Data.MySqlClient;
|
|
using SafeNetLib;
|
|
|
|
namespace ConnectPlus_SOC
|
|
{
|
|
class EmailHandler
|
|
{
|
|
//emai stuff
|
|
private EmailServerSSL sslServer;
|
|
private static System.Threading.Timer tCheckMail;
|
|
private string MyConString;
|
|
private string gatewayID;
|
|
|
|
public EmailHandler(string p_dbConStr, string p_gatewayID)
|
|
{
|
|
MyConString = p_dbConStr;
|
|
gatewayID = p_gatewayID;
|
|
|
|
//emai server ini
|
|
if (ConnectPlus_GW.cfg.enableEmailService)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "Turning on EmailService on " + ConnectPlus_GW.cfg.user);
|
|
sslServer = new EmailServerSSL(ConnectPlus_GW.cfg.pop3Server, ConnectPlus_GW.cfg.pop3Port, ConnectPlus_GW.cfg.user, ConnectPlus_GW.cfg.psw);
|
|
}
|
|
else
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "EmailService disabled!!!");
|
|
|
|
//start email timer... refresh time 1 min
|
|
if (ConnectPlus_GW.cfg.enableEmailService)
|
|
{
|
|
tCheckMail = new System.Threading.Timer(CheckMAil, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 1, 0));
|
|
}
|
|
}
|
|
#region email
|
|
|
|
private void CheckMAil(Object state)
|
|
{
|
|
// This method is executed by a thread pool thread
|
|
try
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Email, new string[] { "Checking mail at:" + DateTime.Now.ToString(), "For " + ConnectPlus_GW.cfg.user.ToString() + " on " + ConnectPlus_GW.cfg.smtpServer.ToString() });
|
|
ArrayList EmailList = sslServer.getEmails();
|
|
|
|
foreach (EmailtoSMS obj in EmailList)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Email, new string[] {"-----START OK----",
|
|
"From: " + obj.from,
|
|
"ID: " + obj.id,
|
|
"Text: " + obj.text});
|
|
|
|
try
|
|
{
|
|
obj.text = obj.text.Replace("'", "`");
|
|
obj.text = obj.text.Replace("#", " ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "Error on converting email:" + ex.ToString());
|
|
}
|
|
|
|
|
|
if (obj.text.Length > 140 - 3 - obj.from.Length)
|
|
obj.text = obj.text.Remove(140 - 3 - obj.from.Length) + " [" + obj.from + "]";
|
|
else
|
|
obj.text = obj.text + " [" + obj.from + "]";
|
|
//SendSMS(obj.id, obj.text, -1); // don't wait for confirmation
|
|
InsertSMSinDB(obj.text, obj.id);
|
|
//LOGS.LOG(ConnectPlus_GW.cfg.gw_id + obj.id + " Email sms:" + obj.text);
|
|
}
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "Email check done!!!");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Error on CheckMAil:" + ex.ToString());
|
|
}
|
|
}
|
|
|
|
//sms-> email
|
|
public static void sendMail(string mailAdr, string mailBody, string radioID)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "Sending mail to " + mailAdr + " from radioId " + radioID.ToString());
|
|
try
|
|
{
|
|
MailMessage message = new MailMessage();
|
|
message.From = new MailAddress(ConnectPlus_GW.cfg.user);
|
|
message.To.Add(mailAdr);
|
|
message.Subject = "RadioID: " + radioID.ToString() + " Subject:" + (mailBody.Length < 25 ? mailBody : (mailBody.Remove(25) + "..."));
|
|
message.Body = "You received a message from radio id " + radioID.ToString() +
|
|
"\n------------------------------\n" +
|
|
mailBody +
|
|
"\n------------------------------\n ";
|
|
message.IsBodyHtml = true;
|
|
|
|
EmailServerSSL.sendEmail(ConnectPlus_GW.cfg.smtpServer, ConnectPlus_GW.cfg.smtpPort, ConnectPlus_GW.cfg.user, ConnectPlus_GW.cfg.psw, message, ConnectPlus_GW.cfg.sslState);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Exception in sendMail: "+ex.ToString());
|
|
//Console.WriteLine(e.ToString());
|
|
}
|
|
}
|
|
|
|
//insert SMS in DB for further processing
|
|
private bool InsertSMSinDB(string body, string p_radioID)
|
|
{
|
|
try
|
|
{
|
|
int suDBid = 0;
|
|
lock (SN_Queues.ht_SUInfo.SyncRoot)
|
|
{
|
|
if (SN_Queues.ht_SUInfo != null)
|
|
{
|
|
if (SN_Queues.ht_SUInfo.ContainsKey(p_radioID))
|
|
suDBid = ((SUinfo)SN_Queues.ht_SUInfo[p_radioID]).DBid;
|
|
}
|
|
}
|
|
if (suDBid == 0)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.Debug, "Unit with IMEI:" + p_radioID + " not found in DB!!!!");
|
|
//LOGS.LOG("Unit with IMEI:" + p_radioID + " not found in DB!!!!");
|
|
return false;
|
|
}
|
|
|
|
MySqlConnection connection = new MySqlConnection(MyConString);
|
|
MySqlConnection connectionSet = new MySqlConnection(MyConString);
|
|
string query = "insert into text_message (message_content,unit_id,date_created,time_sent_or_received,message_type,is_sent) values ('" + body +
|
|
"'," + suDBid +
|
|
",'" + DateTime.Now.ToUniversalTime().ToString("yyyy:MM:dd HH:mm:ss") +
|
|
"','" + DateTime.Now.ToUniversalTime().ToString("yyyy:MM:dd HH:mm:ss") +
|
|
"','OUT',0)";
|
|
MySqlCommand setSent = new MySqlCommand(query, connectionSet);
|
|
|
|
connectionSet.Open();
|
|
setSent.ExecuteNonQuery();
|
|
connectionSet.Close();
|
|
Utils.ConsWrite(DebugMSG_Type.Email, "SMS added to DB : ID " + suDBid + " body:" + body);
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Error InsertSMSinDB in database by EmailHandler");
|
|
Utils.ConsWrite(DebugMSG_Type.always, e.Message);
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|