我只需要幫助。基本上,我正在創建一個窗口應用程序,向我們的客戶發送批量電子郵件。 「email」和「attachment」字段來自數據庫。附件字段僅包含文件所在的路徑,代碼正在工作,但不會收到5封電子郵件,我收到15封電子郵件。在數據庫中發送包含附件路徑的批量電子郵件
注:我的數據庫只包含5條記錄,所以我應該僅僅接收5封電子郵件有附件:
你能幫我請,謝謝!
這裏是我的代碼:
string email;
string attachment;
ArrayList emailList = new ArrayList();
ArrayList attachList = new ArrayList();
private static readonly Logger log = new _EventLogger();
private void btnSend_Click(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmdgetEmail = new SqlCommand("Select EMAIL, PATH from MEMBERREQUIREMENTS WHERE STATUS=0", conn);
SqlDataReader getEmail = cmdgetEmail.ExecuteReader();
//count = 0;
while (getEmail.Read())
{
//count++;
//email = getEmail.GetValue(i).ToString();
//emailList.Add(email);
//i = i + 1 - 1;
email = getEmail.GetString(0);
emailList.Add(email);
attachment = getEmail.GetString(1);
attachList.Add(attachment);
}
getEmail.Close();
conn.Close();
sendMail();
}
private void sendMail()
{
string from="[email protected]";
foreach (string sendTo in emailList)
{
foreach (string sendAttachments in attachList)
{
MailMessage mail = new MailMessage();
mail.To.Add(sendTo);
mail.From = new MailAddress(from, "Company Name'", Encoding.UTF8);
mail.Subject = subject;
mail.Body = msgBodyHead + msgBodyHead2 + msgDate + msgGreet + msgBody + msgAdobe + msgAssistance + msgCompliment + msgfooter;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
mail.Attachments.Add(new Attachment(sendAttachments));
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(from, "password");
client.Host = "192.167.89.0";
client.EnableSsl = false;
try
{
progress();
client.Send(mail);
}
catch (Exception ex)
{
ProgressBar1.Visible = false;
timer1.Enabled = false;
Exception excpt = ex;
string errorMessage = string.Empty;
while (excpt != null)
{
errorMessage += excpt.ToString(); excpt = excpt.InnerException;
log.Error("Email - LMS Application Error", ex);
lblError.Text = "There was an error occured while processing your request.\n Please see Event Viewer for more details.";
lblError.ForeColor = System.Drawing.Color.Red;
}
}
}
}
}
注意'[代碼]'和'[/代碼]'僞標籤毫無意義。格式化代碼的最佳方式是在每行前面加上四個空格。如果您選擇所有代碼,然後點擊編輯器中的「{}」按鈕,它就會正常工作。 – sarnold 2012-03-21 00:19:27
抱歉,wysywig編輯器在我們的防火牆上被阻止,所以我無法看到它。 – Dhenn 2012-03-21 00:20:33
我相信現在好了 – Dhenn 2012-03-21 00:22:26