Click here to Skip to main content
15,886,873 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.
C#
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);
        }
    }
}
Posted
Updated 6-Jan-16 20:51pm
v3
Comments
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: https://support.google.com/mail/answer/78775?hl=en
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 74.125.20.109:587"}

1 solution

C#
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
 
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