string UserName="xhasiwe@gmail.com"; string Password="mypassword"; MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); String report = null; mail.From = new MailAddress("xhasiwe@gmail.com"); string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + @"Data Source= C:\Temp\F1\Docs\Expeditors Project\TestDatabase.accdb"; // Create an OleDbConnection from the provided connection string. using (OleDbConnection connection = new OleDbConnection(connString)) { //List to store information List<String> EmployeeEMailAddress = new List<String>(); // Open a connection to database. connection.Open(); OleDbDataReader reader = null; // Set up a command with the given query and associate this with the current connection. OleDbCommand command = new OleDbCommand("SELECT [EmployeeEMailAddress], [TimeOut] FROM Table1 WHERE [TimeOut] is null", connection); // Read data returned for the query reader = command.ExecuteReader(); while (reader.Read()) { EmployeeEMailAddress.Add(reader["EmployeeEMailAddress"].ToString()); } foreach (string value in EmployeeEMailAddress ) { //If you want to know how many times the elements are repeated //This will return a List of an anonymous type, and each element will have the properties Element and Counter, to retrieve the informations you need. if (value != "\"\"") { var result = EmployeeEMailAddress.GroupBy(item => item) .Select(item => new { Name = item.Key, Count = item.Count() }) .OrderByDescending(item => item.Count) .ThenBy(item => item.Name); report = String.Join(Environment.NewLine, result .Select(item => String.Format("{0} , {1} ;", item.Name, item.Count))); } } connection.Close(); String [] rfqCount = report.Split(';'); int ct = 0; for (int i = 0; i < rfqCount.Length; i++) { if (rfqCount[i].Trim() != "") { ct++; } } String[] emd = rfqCount[ct-1].Trim().Split(','); String emailadd = emd[0]; mail.To.Add(emailadd); } mail.Subject = ReadEmailevent.TypeOfRequest; mail.Body = ReadEmailevent.Body; SmtpServer.Port = 25; SmtpServer.Credentials = new System.Net.NetworkCredential(UserName, Password); SmtpServer.EnableSsl = true; try { SmtpServer.Send(mail); } catch (SmtpFailedRecipientsException ex) { for (int i = 0; i < ex.InnerExceptions.Length; i++) { SmtpStatusCode status = ex.InnerExceptions[i].StatusCode; if (status == SmtpStatusCode.MailboxBusy || status == SmtpStatusCode.MailboxUnavailable) { Console.WriteLine("Delivery failed - retrying in 5 seconds."); System.Threading.Thread.Sleep(5000); SmtpServer.Send(mail); } else { Console.WriteLine("Failed to deliver message to {0}", ex.InnerExceptions[i].FailedRecipient); } } }
perform a telnet test to see if you can connect to Gmail SMTP server. You can find steps here: https://support.google.com/mail/answer/78775?hl=en
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)