242 lines
8.9 KiB
Plaintext
242 lines
8.9 KiB
Plaintext
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
using OpenPOP.POP3;
|
|
using System.Collections;
|
|
using SafeNetLib;
|
|
|
|
namespace MotoTRBO_SOC
|
|
{
|
|
public class EmailServerSSL
|
|
{
|
|
private POPClient popClient;
|
|
private string serverIP;
|
|
private int port;
|
|
private string loginNAme;
|
|
private string password;
|
|
|
|
//constructor
|
|
public EmailServerSSL(string _serverIP, string _port, string _logName, string _pass)
|
|
{
|
|
bool result = Int32.TryParse(_port, out port);
|
|
if (!result)
|
|
{
|
|
Console.WriteLine("Email port not numeric, error in EmailServerSSL constructor.");
|
|
return;
|
|
}
|
|
|
|
serverIP = _serverIP;
|
|
loginNAme = _logName;
|
|
password = _pass;
|
|
|
|
popClient = new POPClient();
|
|
popClient.AuthenticationBegan += new EventHandler(popClient_AuthenticationBegan);
|
|
popClient.AuthenticationFinished += new EventHandler(popClient_AuthenticationFinished);
|
|
popClient.CommunicationBegan += new EventHandler(popClient_CommunicationBegan);
|
|
popClient.CommunicationOccured += new EventHandler(popClient_CommunicationOccured);
|
|
popClient.CommunicationLost += new EventHandler(popClient_CommunicationLost);
|
|
popClient.MessageTransferBegan += new EventHandler(popClient_MessageTransferBegan);
|
|
popClient.MessageTransferFinished += new EventHandler(popClient_MessageTransferFinished);
|
|
}
|
|
#region popCLient events
|
|
private void AddEvent(string strEvent)
|
|
{
|
|
/*
|
|
lstEvents.Items.Add(strEvent);
|
|
lstEvents.SelectedIndex = lstEvents.Items.Count - 1;
|
|
*/
|
|
//Console.WriteLine("popClient event = " +strEvent);
|
|
}
|
|
|
|
private void popClient_CommunicationBegan(object sender, EventArgs e)
|
|
{
|
|
AddEvent("CommunicationBegan");
|
|
}
|
|
|
|
private void popClient_CommunicationOccured(object sender, EventArgs e)
|
|
{
|
|
AddEvent("CommunicationOccured");
|
|
}
|
|
|
|
private void popClient_AuthenticationBegan(object sender, EventArgs e)
|
|
{
|
|
AddEvent("AuthenticationBegan");
|
|
}
|
|
|
|
private void popClient_AuthenticationFinished(object sender, EventArgs e)
|
|
{
|
|
AddEvent("AuthenticationFinished");
|
|
}
|
|
|
|
private void popClient_MessageTransferBegan(object sender, EventArgs e)
|
|
{
|
|
AddEvent("MessageTransferBegan");
|
|
}
|
|
|
|
private void popClient_MessageTransferFinished(object sender, EventArgs e)
|
|
{
|
|
AddEvent("MessageTransferFinished");
|
|
}
|
|
|
|
private void popClient_CommunicationLost(object sender, EventArgs e)
|
|
{
|
|
AddEvent("CommunicationLost");
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
public ArrayList getEmails()
|
|
{
|
|
ArrayList ret = new ArrayList();
|
|
try
|
|
{
|
|
popClient.IsUsingSsl = true;
|
|
popClient.Disconnect();
|
|
popClient.Connect(serverIP, port);
|
|
popClient.Authenticate(loginNAme, password);
|
|
|
|
|
|
int count = popClient.GetMessageCount();
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Got " + count + " emails");
|
|
|
|
for (int i = count; i >= 1; i -= 1)
|
|
{
|
|
OpenPOP.MIMEParser.Message email = popClient.GetMessage(i, false);
|
|
if (email != null)
|
|
{
|
|
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "-----------------------------------------------------------------");
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "From: " + email.FromEmail);
|
|
if (email.TO.Length >= 0)
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "To: " + email.TO[0]);
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Subject: " + email.Subject);
|
|
if (email.MessageBody.Count > 0)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Text: " + (string)email.MessageBody[email.MessageBody.Count - 1]);
|
|
}
|
|
|
|
EmailtoSMS ets = processMessage(email.Subject, (string)email.MessageBody[email.MessageBody.Count - 1], email.FromEmail);
|
|
if ((ets.id != "") && (ets.text == ""))
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Incorrect email body " + (string)email.MessageBody[email.MessageBody.Count - 1]);
|
|
}
|
|
else if ((ets.id != "") && (ets.text != ""))
|
|
{
|
|
popClient.DeleteMessage(i);
|
|
ret.Add(ets);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "-----------------------------------------------------------------");
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "BAD email");
|
|
}
|
|
|
|
}
|
|
|
|
popClient.Disconnect();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "getEmails() from EmailServerSSL Exception: " + ex.Message + ex.ToString());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
private EmailtoSMS processMessage(string subj, string body, string from)
|
|
{
|
|
EmailtoSMS obj = new EmailtoSMS();
|
|
obj.from = from;
|
|
String s = subj.ToUpper();
|
|
if (s.Contains("ID[") && subj.Contains("]"))
|
|
{
|
|
obj.id = s;
|
|
obj.id = obj.id.Remove(0, s.IndexOf("ID[") + 3);
|
|
obj.id = obj.id.Remove(obj.id.IndexOf("]"));
|
|
}
|
|
|
|
if (obj.id.Length > 0)
|
|
{ // ID prezent in subject
|
|
s = body.ToUpper();
|
|
// SM.Debug("Email text:" + s.ToString() + " " + s.Contains("BODY[") + " " + body.Contains("]"));
|
|
if (s.Contains("BODY[") && body.Contains("]"))
|
|
{
|
|
obj.text = body;
|
|
obj.text = obj.text.Remove(0, s.IndexOf("BODY[") + 5);
|
|
obj.text = obj.text.Remove(obj.text.IndexOf("]"));
|
|
if (obj.text.Length > 139)
|
|
obj.text = obj.text.Remove(140);
|
|
}
|
|
}
|
|
else
|
|
{ //check for id in email body
|
|
s = body.ToUpper();
|
|
if (s.Contains("ID[") && s.Contains("]"))
|
|
{
|
|
obj.id = body;
|
|
obj.id = obj.id.Remove(0, s.IndexOf("ID[") + 3);
|
|
obj.id = obj.id.Remove(obj.id.IndexOf("]"));
|
|
}
|
|
|
|
if (obj.id.Length > 0)
|
|
{
|
|
s = body.ToUpper();
|
|
//SM.Debug("Email text2:" + s.ToString() + " " + s.Contains("BODY[") + " " + body.Contains("]"));
|
|
if (s.Contains("BODY[") && s.Contains("]"))
|
|
{
|
|
obj.text = body;
|
|
obj.text = obj.text.Remove(0, s.IndexOf("BODY[") + 5);
|
|
obj.text = obj.text.Remove(obj.text.IndexOf("]"));
|
|
if (obj.text.Length > 139)
|
|
obj.text = obj.text.Remove(140);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "ID: " + obj.id);
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Text: " + obj.text);
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "-----------------------------------------------------------------");
|
|
return obj;
|
|
}
|
|
|
|
public static void sendEmail(string server, string _port, string user, string pass, MailMessage mailMessage, Boolean enableSSL)
|
|
{
|
|
int port = 0;
|
|
bool result = Int32.TryParse(_port, out port);
|
|
if (result)
|
|
{
|
|
SmtpClient mailClient = new SmtpClient();
|
|
mailClient.Host = server;
|
|
mailClient.Port = port;
|
|
NetworkCredential cred = new NetworkCredential(user, pass);
|
|
mailClient.Credentials = cred;
|
|
mailClient.EnableSsl = enableSSL;
|
|
mailClient.Send(mailMessage);
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "------Sending email on " + server + " TO: " + mailMessage.To[0].ToString() + "--------------");
|
|
}
|
|
else
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.DB, "Email port not numeric, error in sendEmail.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public class EmailtoSMS
|
|
{
|
|
public String from;
|
|
public String id;
|
|
public String text;
|
|
|
|
public EmailtoSMS()
|
|
{
|
|
from = "";
|
|
id = "";
|
|
text = "";
|
|
}
|
|
}
|
|
}
|