Click here to Skip to main content
15,664,619 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been struggling with this problem for days now i dont know what to do because everyone is saying the code I am using to send an email should work i have even tried different ports from 587 to 25 to 465 I just do not know anymore i have even added an SmtpFailedRecipientsException and still the same problem could someone please help me i need to launch this project im behind schedule.
string UserName="";
string Password="mypassword";

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("");
String report = null;
mail.From = new MailAddress("");
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.
    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())

    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)));

    String [] rfqCount = report.Split(';');
    int ct = 0;
    for (int i = 0; i < rfqCount.Length; i++)
        if (rfqCount[i].Trim() != "")
    String[] emd = rfqCount[ct-1].Trim().Split(',');
    String emailadd = emd[0];

mail.Subject = ReadEmailevent.TypeOfRequest;
mail.Body = ReadEmailevent.Body;

SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential(UserName, Password);
SmtpServer.EnableSsl = true;

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.");
            Console.WriteLine("Failed to deliver message to {0}",
Updated 6-Jan-16 20:51pm
dan!sh 7-Jan-16 2:59am    
On which line are you getting the exception? Please update your question with this and exception details.
Member 11925473 7-Jan-16 3:05am    
which like? it doesnt even get to the exception when it runs it just stops at SmtpServer.Send(mail) and it gives the error unable to send email
dan!sh 7-Jan-16 3:20am    
SmtpClient.Send method can return different kinds of exceptions. Can you handle general Exception and check for specific error?
dan!sh 7-Jan-16 3:23am    
Also perform a telnet test to see if you can connect to Gmail SMTP server. You can find steps here:
Member 11925473 7-Jan-16 3:25am    
InnerException : "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond"}

1 solution

perform a telnet test to see if you can connect to Gmail SMTP server. You can find steps here:
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900