我有一个用于管理业务的 Windows 窗体程序。我将所有客户的详细信息保存在数据库链接公司中。电子邮件地址。我有一个按钮,可以让我向邮件列表中的每个人发送一封电子邮件(下面的代码)。但是在发送了几封带有未指明错误的电子邮件后它崩溃了。我不知道为什么?我需要它从 CustomerID40- CustomerID1425 立即向所有客户发送相同的电子邮件。我们将不胜感激。抱歉,如果代码困惑。谢谢。 错误:
System.Data.OleDb.OleDbException
HResult=0x80004005
Message=Unspecified error
Source=System.Data
StackTrace:
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
at CourseworkDatabase.clsDBConnector.Connect() in C:\Users\rep\OneDrive\Desktop\Repairs\ Repairs_Database\CourseworkDatabase\CourseworkDatabase\clsDBConnector.cs:line 18
代码:
private void button6_Click_1(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure to send email to all customers?(check code)", "Warning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
DoButtonTask();
}
}
private void DoButtonTask()
{
for (int i = 40; i < 1425; i++) //40-1425 is customerID's
{
clsDBConnector dbConnector1 = new clsDBConnector();
OleDbDataReader dr1;
string sqlStr1;
dbConnector1.Connect();
sqlStr1 = " SELECT CustomerID, DateAdded, FullName, PhoneNumber, EmailAddress, SendOffersByEmail" +
" FROM Customer" +
" WHERE (CustomerID = " + i + ")";
dr1 = dbConnector1.DoSQL(sqlStr1);
string name = "";
string CustomerID = "";
string email = "";
string SendOffersBy_Email = "";
DateTime date = DateTime.Now;
while (dr1.Read())
{
CustomerID = CustomerID + Convert.ToString(dr1[0]);
name = name + dr1[2];
email = email + Convert.ToString(dr1[4]);
SendOffersBy_Email = SendOffersBy_Email + Convert.ToString(dr1[5]);
}
if (email == "na" || email == "na@na.co.uk" || email == "na@na.com" || email == "")
{
//MessageBox.Show("Customer " + CustomerID + " does not have an email linked.");
}
else
{
if (SendOffersBy_Email == "yes" || SendOffersBy_Email == "Yes")
{
try
{
SendEmail(i, email, name);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
//MessageBox.Show("Customer " + CustomerID + " does not accept emails.");
}
}
}
MessageBox.Show("Emails sent.");
}
private void SendEmail(int i, string email, string name)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
mail.From = new MailAddress("myemail");
mail.To.Add(email);
mail.Subject = "PRICE DROP!! Have your iPhone repaired today.";
mail.Body = "Hi " + name + ",\n" +
"Our iPhone 7 and iPhone 8 series screen prices have now dropped in price!" +
" Reply to this email to have your iPhone booked in for repair today - the prices may go back up! " +
" All of our repairs come with our 6 months warranty and we can come to you." +
"\n\nKind regards,\n Your Mobile Phone & Tablet Repair Specialist." +
"\n\nTel: \nWebsite: \nEmail: " +
"\nFacebook: " +
"\n\n\n Don't want to recieve offers anymore? Just reply to this email to let us know and we will take you off our mailing system.";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
mail.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
请您参考如下方法:
代码有明显改进:
for (int i = 40; i < 1425; i++) //40-1425 is customerID's
{
clsDBConnector dbConnector1 = new clsDBConnector();
OleDbDataReader dr1;
string sqlStr1;
dbConnector1.Connect();
...
}
此代码每次迭代都会打开您的数据库,这会增加开销,并且您的防病毒软件可能每次都在检查文件。
我建议将连接移到循环外(和关闭),这样它就变成了
clsDBConnector dbConnector1 = new clsDBConnector();
OleDbDataReader dr1;
string sqlStr1;
dbConnector1.Connect();
for (int i = 40; i < 1425; i++) //40-1425 is customerID's
{
// process data
}
dbConnector1.Close();
