public List <MsgSummary> RetrieveEmails(ProfileMailServer mailserver) { List <MsgSummary> msgTodo = new List <MsgSummary>(); //This will Check the Mail against the Profile MailServer and the MessageType from the Profile. try { msgTodo = MailModule.GetMail(mailserver.Hostname, mailserver.Port, mailserver.SSL, mailserver.UserName, mailserver.Password); if (msgTodo.Count == 0) { return(null); } } catch (Exception ex) { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Error Retrieving Email for " + mailserver.Hostname + ": - " + ex.Message, Color.Red); // MailModule.sendMsg("", Globals.AlertsTo, "Error Retrieving Email for " + dr["P_USERNAME"].ToString(), "Error Retrieving Email for " + dr["P_USERNAME"].ToString() + ": " + ex.Message); } return(msgTodo); }
public void ProcessJobs() { Program.AddRTBText(rtbStatus, "Processing Jobs"); // Load Mailbox List foreach (DataRow row in dsProfiles.Tables["Mailbox"].Rows) { //Create list of profiles that are attached to the Current Mailbox DataRow[] drRule = dsProfiles.Tables["Rule"].Select("MailBox_id = " + row["Mailbox_id"]); ProfileMailServer mserver = new ProfileMailServer(); mserver.Hostname = row["PopServer"].ToString(); mserver.Port = int.Parse((row["PopPort"].ToString())); mserver.UserName = row["PopUsername"].ToString(); mserver.Password = row["PopPassword"].ToString(); mserver.SSL = row["PopSSL"].ToString(); mserver.AddressTo = row["AddressTo"].ToString(); //Create list of Messages from Current mailbox. Program.AddRTBText(rtbStatus, String.Format("{0:g} ", DateTime.Now) + " Connecting to " + mserver.Hostname + " for mailbox : " + mserver.AddressTo); List <MsgSummary> msgList = RetrieveEmails(mserver); if (msgList == null) { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " No Email found. Moving to next Profile.", Color.Gray); } else { foreach (MsgSummary msg in msgList) { if (msg.MsgFile != null) { foreach (MsgAtt att in msg.MsgFile) { bool ruleFound = false; foreach (DataRow rule in drRule) { if (!ruleFound) { //Loop through each profile to find valid rule ProfileMessage pMessage = new ProfileMessage(); pMessage.AddressFrom = rule["AddressFrom"].ToString(); pMessage.Subject = rule["Subject"].ToString(); pMessage.Action = rule["PopAction"].ToString(); pMessage.FTPServer = rule["FtpServer"].ToString(); pMessage.FTPFolder = rule["FtpFolder"].ToString(); pMessage.FTPPass = rule["FtpPassWord"].ToString(); pMessage.FTPPort = rule["FtpPort"].ToString(); pMessage.FTPUser = rule["FtpUserName"].ToString(); pMessage.ProfilePrinter = rule["PrintQueue"].ToString(); if (string.IsNullOrEmpty(rule["FtpSecure"].ToString())) { pMessage.Secure = false; } else { pMessage.Secure = bool.Parse(rule["FtpSecure"].ToString()); } if (MsgValid(msg, att, pMessage.AddressFrom, pMessage.Subject, "")) { switch (pMessage.Action) { case "FTP": Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Establishing FTP Connection for profile :" + pMessage.ProfileName, Color.Black); SessionOptions sessionOptions = new SessionOptions { HostName = pMessage.FTPServer, PortNumber = int.Parse(pMessage.FTPPort), UserName = pMessage.FTPUser, Password = pMessage.FTPPass }; if (pMessage.Secure) { sessionOptions.Protocol = Protocol.Sftp; } else { sessionOptions.Protocol = Protocol.Ftp; } try { TransferOperationResult tr; using (Session session = new Session()) { session.Open(sessionOptions); if (session.Opened) { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Connected to " + pMessage.FTPServer + ". Sending file :" + att.AttFilename, Color.Black); TransferOptions tOptions = new TransferOptions(); tOptions.OverwriteMode = OverwriteMode.Overwrite; tr = session.PutFiles(att.AttFilename.FullName, pMessage.FTPFolder, true, tOptions); Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Results: ", Color.Black); if (tr.IsSuccess) { MailModule.DeleteMessage(mserver.Hostname, mserver.Port, mserver.SSL, mserver.UserName, mserver.Password, msg.MsgId); } else { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Transfer Result is invalid: ", Color.Black); } } else { } } } catch (SessionRemoteException ex) { MessageBox.Show("FTP Server Error: " + ex.Message, "FTP Server Test", MessageBoxButtons.OK, MessageBoxIcon.Information); } break; case "Print": bool filePrinted = false; string fileType = att.AttFilename.Extension; Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Now Printing " + att.AttFilename.FullName, Color.Black); filePrinted = PrintDoc(att.AttFilename.FullName, pMessage.ProfilePrinter); if (filePrinted) { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Printed Ok.", Color.Black); MailModule.DeleteMessage(mserver.Hostname, mserver.Port, mserver.SSL, mserver.UserName, mserver.Password, msg.MsgId); } else { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Printing Failed !!", Color.Red); } break; } ruleFound = true; } //Process message against Profile Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Checking: " + pMessage.ProfileName, Color.Black); } } if (!ruleFound) { Program.AddRTBText(rtbStatus, string.Format("{0:g} ", DateTime.Now) + " Invalid email found. Deleting message. From : " + msg.MsgFrom + " Subject: " + msg.MsgSubject, Color.Red);; MailModule.DeleteMessage(mserver.Hostname, mserver.Port, mserver.SSL, mserver.UserName, mserver.Password, msg.MsgId); ruleFound = true; } } //Looping through messages (and Attachments) } } } // PrintJobs(mserver.ProfilePrinter); } }