247 lines
9.0 KiB
C#
247 lines
9.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net.Mail;
|
|
using System.Net;
|
|
using OpenPOP.POP3;
|
|
using System.Collections;
|
|
using SafeNetLib;
|
|
using System.Net.Security;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
namespace ConsoleApplication1
|
|
{
|
|
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();
|
|
Console.WriteLine("Got " + count + " emails");
|
|
|
|
for (int i = count; i >= 1; i -= 1)
|
|
{
|
|
OpenPOP.MIMEParser.Message email = popClient.GetMessage(i, false);
|
|
if (email != null)
|
|
{
|
|
|
|
Console.WriteLine("-----------------------------------------------------------------");
|
|
Console.WriteLine("From: " + email.FromEmail);
|
|
if (email.TO.Length >= 0)
|
|
Console.WriteLine("To: " + email.TO[0]);
|
|
Console.WriteLine("Subject: " + email.Subject);
|
|
if (email.MessageBody.Count > 0)
|
|
{
|
|
Console.WriteLine("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 == ""))
|
|
{
|
|
Console.WriteLine("Incorrect email body " + (string)email.MessageBody[email.MessageBody.Count - 1]);
|
|
}
|
|
else if ((ets.id != "") && (ets.text != ""))
|
|
{
|
|
popClient.DeleteMessage(i);
|
|
ret.Add(ets);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("-----------------------------------------------------------------");
|
|
Console.WriteLine("BAD email");
|
|
}
|
|
|
|
}
|
|
|
|
popClient.Disconnect();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("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);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
Console.WriteLine("ID: " + obj.id);
|
|
Console.WriteLine("Text: " + obj.text);
|
|
Console.WriteLine("-----------------------------------------------------------------");
|
|
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;
|
|
//ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
|
|
mailClient.Send(mailMessage);
|
|
Console.WriteLine("------Sending email on " + server + " TO: " + mailMessage.To[0].ToString() + "--------------");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Email port not numeric, error in sendEmail.");
|
|
}
|
|
}
|
|
}
|
|
|
|
public class EmailtoSMS
|
|
{
|
|
public String from;
|
|
public String id;
|
|
public String text;
|
|
|
|
public EmailtoSMS()
|
|
{
|
|
from = "";
|
|
id = "";
|
|
text = "";
|
|
}
|
|
}
|
|
}
|