更新时间:2022-10-17 23:23:46
执行telnet测试以查看如果您可以连接到Gmail SMTP服务器。您可以在此处找到以下步骤:https: // support.google.com/mail/answer/78775?hl=en
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="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