public static void CheckForNewLettersIMAP(Profile user, int letterNum) { Letter newLetter = new Letter(); newLetter.To = user.Adress; Authenticate(user); ImapRequest("$ SELECT INBOX\r\n"); ImapRequest("$ STATUS INBOX (MESSAGES)\r\n"); List<string> letterHeader = GetHeaderOfLetter(letterNum); foreach (string line in letterHeader) { if (line.Contains("From: ")) { newLetter.From = line.Substring(6); MessageBox.Show(line.Substring(6)); } if (line.Contains("Date: ")) { MessageBox.Show(line.Substring(6)); } if (line.Contains("Subject: ")) { newLetter.Subject = line.Substring(9); MessageBox.Show(line.Substring(9)); } } Authenticate(user); ImapRequest("$ SELECT INBOX\r\n"); newLetter.Body = GetTextOfLetter(letterNum); }
private void byAccept_Click(object sender, RoutedEventArgs e) { MainWindow main = this.Owner as MainWindow; if (tbAdress.Text != string.Empty && tbPassword.Password != string.Empty && tbProfileName.Text != string.Empty && cbServer.Text != string.Empty) { Profile newUser = new Profile(tbProfileName.Text, tbAdress.Text, tbPassword.Password, cbServer.Text); if (cbServer.Text == "mail.ru") { newUser.SmtpPort = 25; newUser.PopPort = 995; } if (cbServer.Text == "gmail.com") { newUser.SmtpPort = 587; newUser.PopPort = 995; } if (cbServer.Text == "yandex.ru") { newUser.SmtpPort = 25; newUser.PopPort = 995; } newUser.SetId(); newUser.LastTimeChecked = DateTime.Now; Profile.DB_Add(newUser); main.profilesList.Add(newUser); main.tvMain.Items.Refresh(); this.Close(); } }
public override SslStream Authenticate(Profile user) { ImapConsole console = new ImapConsole(); SslStream ssl = console.SetSSLConnectionAndReturn(user); console.SendCommand(new ImapAuthenticate(user)); console.ExecuteCommand(); return ssl; }
public WindowSendMessage(Profile prof, Point location, double height, double width, string letterText, string recievers) { this.InitializeComponent(); this.mailSender = prof; this.Left = (width / 2) - (this.Width / 2) + location.X; this.Top = (height / 2) - (this.Height / 2) + location.Y; tbMessage.Text = letterText; tbAdress.Text = recievers; }
public sealed override object InitiateProtocol(Profile user, string request) { SslStream ssl = this.Authenticate(user); int flag = this.SendRequestToServer(ssl, request); object result = this.PullDataFromServer(ssl, flag); this.LogOut(ssl); return result; }
public void SendIt(Profile sender) { // SmtpClient Smtp = new SmtpClient("smtp.mail.ru", 25); // SmtpClient Smtp = new SmtpClient("smtp.yandex.ru", 25); // SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587); SmtpClient smtp = new SmtpClient("smtp." + sender.Server, sender.SmtpPort); // Сервер и порт smtp.Credentials = new System.Net.NetworkCredential(sender.Adress, sender.Password); // Логин и пароль ////Формирование письма MailMessage message = new MailMessage(); message.From = new MailAddress(sender.Adress); message.To.Add(new MailAddress(letterItem.To)); message.Subject = letterItem.Subject; message.Body = letterItem.Body; smtp.EnableSsl = true; smtp.Send(message); }
public void SendLetterSMTP(Profile sender, Letter letter) { SmtpClient smtp = new SmtpClient("smtp." + sender.Server, sender.SmtpPort); smtp.Credentials = new System.Net.NetworkCredential(sender.Adress, sender.Password); MailMessage message = new MailMessage(); message.From = new MailAddress(sender.Adress); message.To.Add(new MailAddress(letter.To)); message.Subject = letter.Subject; message.Body = letter.Body; smtp.EnableSsl = true; foreach (ExtendedAttachment attachmentExt in lbAttachments.Items) { message.Attachments.Add(attachmentExt.AttachedObject); } smtp.Send(message); }
public static void DB_Add(Profile user) { LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); cmd.CommandText = "insert into Profiles (Id, Name, Adress,Password, LastTimeChecked, Server, SmtpPort, PopPort)" + " values (@id, @name, @adr, @pas, @ltc, @s, @smtp, @pop)"; cmd.Parameters.AddWithValue("@id", user.Id); cmd.Parameters.AddWithValue("@name", user.Name); cmd.Parameters.AddWithValue("@adr", user.Adress); cmd.Parameters.AddWithValue("@pas", user.Password); cmd.Parameters.AddWithValue("@ltc", user.LastTimeChecked); cmd.Parameters.AddWithValue("@s", user.Server); cmd.Parameters.AddWithValue("@smtp", user.SmtpPort); cmd.Parameters.AddWithValue("@pop", user.PopPort); cmd.ExecuteNonQuery(); sqlconnectionClass.CloseConnection(); }
public override void Fill(List<ProxyLetter> list, Profile user) { list.Clear(); SqlConnection _connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Gleb\Desktop\GEMC\GEMC\EMCdataBase.mdf;Integrated Security=True;"); SqlCommand cmd = new SqlCommand(); SqlDataReader dr; cmd.Connection = _connection; _connection.Open(); cmd.CommandText = "select * from Mail where ProfileId='" + user.Id + "' and AdressTo='" + user.Adress + "'"; dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { ProxyLetter proxy = new ProxyLetter(dr[0].ToString(), dr[2].ToString(), Convert.ToDateTime(dr[7])); proxy.Interlocutor = dr[4].ToString(); list.Add(proxy); } } _connection.Close(); }
public static ProxyList GetSended(Profile user) { ProxyList proxies = new ProxyList("Отправленные", user.Id); LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); cmd.CommandText = "select * from Mail where ProfileId='" + user.Id + "' and Category='Outbox'"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { ProxyLetter proxy = new ProxyLetter(dr[0].ToString(), dr[2].ToString(), Convert.ToDateTime(dr[7])); proxy.Interlocutor = dr[5].ToString(); proxies.ProxyMailList.Add(proxy); } } sqlconnectionClass.CloseConnection(); return proxies; }
public override SslStream Authenticate(Profile user) { DateTime lastCheck = user.LastTimeChecked; TcpClient tcpclient = new TcpClient(); tcpclient.Connect("pop." + user.Server, user.PopPort); System.Net.Security.SslStream sslstream = new SslStream(tcpclient.GetStream()); sslstream.AuthenticateAsClient("pop." + user.Server); System.IO.StreamWriter sw = new StreamWriter(sslstream); if (user.Server == "gmail.com") { sw.WriteLine("USER recent:" + user.Adress); // Google требует особого входа } else { sw.WriteLine("USER " + user.Adress); } sw.Flush(); sw.WriteLine("PASS " + user.Password); sw.Flush(); return sslstream; }
public Letter CheckLetterByIMAP(Profile user, int letterNum) { Letter newLetter = new Letter(); newLetter.SetId(); newLetter.ProfileId = user.Id; newLetter.To = user.Adress; newLetter.Category = "Inbox"; bool subjectFound = false; bool dateFound = false; bool fromFound = false; ImapConsole console = new ImapConsole(); console.SetSSLConnection(user); console.SendCommand(new ImapAuthenticate(user)); console.ExecuteCommand(); this.ImapRequest("$ SELECT INBOX\r\n"); console.SendCommand(new ImapGetHeaderOfLetter(letterNum)); List<string> letterHeader = (List<string>)console.ExecuteCommand(); foreach (string line in letterHeader) { if (!fromFound && line.Contains("From: ")) { string thisLine = line; if (line.Contains("<")) { thisLine = line.Substring(line.IndexOf('<') + 1, line.IndexOf('>') - line.IndexOf('<') - 1); newLetter.From = thisLine; } else { newLetter.From = thisLine.Substring(6); } fromFound = true; } if (!dateFound && line.Length > 6 && line.Substring(0, 6) == "Date: ") { string thisLine = line; if (line.Contains("+")) { thisLine = line.Substring(0, line.IndexOf('+')); } else if (line.Contains("-")) { thisLine = line.Substring(0, line.IndexOf('-')); } newLetter.SendingTime = Convert.ToDateTime(thisLine.Substring(6)); dateFound = true; } if (!subjectFound && line.Contains("Subject: ")) { newLetter.Subject = line.Substring(9).Replace("\r", string.Empty); if (newLetter.Subject.Substring(0, 5) == "=?utf") { newLetter.Subject = newLetter.Subject.Substring(10, newLetter.Subject.Length - 10); interpreterContext iC = new interpreterContext(newLetter.Subject); DefaultExpression ex = new DefaultExpression(); ex.Interpret(iC); newLetter.Subject = ex.UseEncoding(iC); } subjectFound = true; } if (subjectFound && dateFound && fromFound) { break; } } console.SetSSLConnection(user); console.SendCommand(new ImapAuthenticate(user)); console.ExecuteCommand(); this.ImapRequest("$ SELECT INBOX\r\n"); console.SendCommand(new ImapGetTextOfLetter(letterNum)); newLetter.Body = (string)console.ExecuteCommand(); return newLetter; }
private static void Authenticate(Profile user) { tcpc = new TcpClient("imap." + user.Server, 993); ssl = new System.Net.Security.SslStream(tcpc.GetStream()); ssl.AuthenticateAsClient("imap." + user.Server); ImapRequest("$ LOGIN " + user.Adress + " " + user.Password + "\r\n"); }
public static void AddLetterToDB(Profile user, Letter letter) { LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); cmd.CommandText = "insert into Mail (Id, ProfileId, Subject,Body, AdressFrom, AdressTo, Category, Time)" + " values (@id, @pid, @s, @b, @af, @at, @c, @t)"; letter.SetId(); cmd.Parameters.AddWithValue("@id", letter.Id); cmd.Parameters.AddWithValue("@pid", user.Id); cmd.Parameters.AddWithValue("@s", letter.Subject); cmd.Parameters.AddWithValue("@b", letter.Body); cmd.Parameters.AddWithValue("@af", letter.From); cmd.Parameters.AddWithValue("@at", letter.To); cmd.Parameters.AddWithValue("@c", letter.Category); cmd.Parameters.AddWithValue("@t", letter.SendingTime); cmd.ExecuteNonQuery(); sqlconnectionClass.CloseConnection(); }
public List<Letter> PullFreshLetters(Profile user) { List<Letter> newLettersList = new List<Letter>(); DateTime start = DateTime.Now; int totalLetterNumber = this.GetTotalLetterNum(user); for (int i = totalLetterNumber; i > 0; i--) { try { Letter newLetter = this.CheckLetterByIMAP(user, i); // if (newLetter.SendingTime > user.LastTimeChecked) if (newLetter.SendingTime > new DateTime(2015, 10, 10)) { newLettersList.Add(newLetter); } else { break; } } catch { } } // user.LastTimeChecked = DateTime.Now; // Profile.DB_Update(user); return newLettersList; }
public int GetTotalLetterNum(Profile user) { int result = 0; ImapConsole console = new ImapConsole(); console.SetSSLConnection(user); console.SendCommand(new ImapAuthenticate(user)); console.ExecuteCommand(); this.ImapRequest("$ SELECT INBOX\r\n"); string answer = this.ImapRequest("$ STATUS INBOX (MESSAGES)\r\n"); foreach (string line in answer.Split('\n')) { if (line.Contains("EXISTS")) { result = Convert.ToInt32(line.Substring(2, line.IndexOf('E') - 2)); this.ImapRequest("$ LOGOUT\r\n"); } } return result; }
public static Profile DB_GetByID(string id) { Profile prof = new Profile(); LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); cmd.CommandText = "select * from Profiles where Id='" + id + "'"; SqlDataReader dr = cmd.ExecuteReader(); sqlconnectionClass.CloseConnection(); if (dr.HasRows) { while (dr.Read()) { Profile user = new Profile(); user.Id = dr[0].ToString(); user.Name = dr[1].ToString(); user.Adress = dr[2].ToString(); user.Password = dr[3].ToString(); user.LastTimeChecked = Convert.ToDateTime(dr[4]); user.Server = dr[5].ToString(); user.SmtpPort = Convert.ToInt32(dr[6]); user.PopPort = Convert.ToInt32(dr[7]); prof = user; } } return prof; }
public static void DB_Delete(Profile user) { LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); cmd.CommandText = @"delete from Profiles where Id='" + user.Id + "'"; cmd.ExecuteNonQuery(); cmd.CommandText = @"delete from Mail where ProfileId='" + user.Id + "'"; cmd.ExecuteNonQuery(); sqlconnectionClass.CloseConnection(); }
public static void DB_UpdateTime(Profile user) { LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); cmd.CommandText = @"Update Profiles SET LastTimeChecked = (@UE) where Id='" + user.Id + "'"; cmd.Parameters.AddWithValue("@UE", user.LastTimeChecked); cmd.ExecuteNonQuery(); sqlconnectionClass.CloseConnection(); }
public static List<Profile> DB_Load() { LocalSQLConnection sqlconnectionClass = new LocalSQLConnection(); SqlCommand cmd = sqlconnectionClass.DeployConnectionAndCommand(); List<Profile> profileList = new List<Profile>(); cmd.CommandText = "select * from Profiles"; SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Profile user = new Profile(); user.Id = dr[0].ToString(); user.Name = dr[1].ToString(); user.Adress = dr[2].ToString(); user.Password = dr[3].ToString(); user.LastTimeChecked = Convert.ToDateTime(dr[4]); user.Server = dr[5].ToString(); user.SmtpPort = Convert.ToInt32(dr[6]); user.PopPort = Convert.ToInt32(dr[7]); profileList.Add(user); } } sqlconnectionClass.CloseConnection(); return profileList; }
public abstract SslStream Authenticate(Profile user);
public SslStream SetSSLConnectionAndReturn(Profile user) { _tcpc = new TcpClient("imap." + user.Server, 993); _ssl = new System.Net.Security.SslStream(_tcpc.GetStream()); return _ssl; }
public abstract void Fill(List<ProxyLetter> list, Profile user);