private void btSend_Click(object sender, RoutedEventArgs e) { MainWindow main = this.Owner as MainWindow; if (tbAdress.Text != string.Empty && tbMessage.Text != string.Empty && tbSubject.Text != string.Empty) { string adressesText = tbAdress.Text.Replace(" ", string.Empty); string[] recievers = adressesText.Split(','); foreach (string reciever in recievers) { Letter letter = new Letter(this.mailSender.Id, tbSubject.Text, tbMessage.Text, this.mailSender.Adress, tbAdress.Text, "Outbox", DateTime.Now); letter.SetId(); this.SendLetterSMTP(this.mailSender, letter); Letter.AddLetterToDB(this.mailSender, letter); } this.Close(); main.FillProfilesListFull(); } }
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; }
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(); }