gpt4 book ai didi

c# - 在数据库中发送带有附件路径的批量电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:23 25 4
gpt4 key购买 nike

我只是需要帮助。基本上我正在创建一个向我们的客户发送批量电子邮件的 Windows 应用程序。 “电子邮件”和“附件”字段来自数据库。附件字段仅包含文件所在的路径,代码可以正常工作,但我没有收到 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="myemail@email.com";
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;
}
}

}
}
}

最佳答案

看起来您正在从 MEMBERREQUIREMENTS 表中提取一堆记录,其中包含一个电子邮件地址和一个附件路径,并构建一个电子邮件地址和附件路径列表。

然后您将迭代这些发送,为地址和附件的每个组合发送一封电子邮件。我猜这不是你想要做的。我想要么:

  • 每个收件人应该只收到他们记录中列出的附件。在这种情况下,您不想遍历每个地址的附件,或者可能
  • 每个收件人都应收到每个附件,但附件应列在单独的表中,而不是存储在 MEMBERREQUIREMENTS 表中每个地址的记录中。

更新:

下面是执行前者的一些代码。请注意,这只是对您发布的代码的最小编辑,不会修复变量名称等,也不会提供任何额外的错误检查(您可能想要这样做):

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();
while (getEmail.Read())
{
email = getEmail.GetString(0);
attachment = getEmail.GetString(1);
this.sendMail(email, attachment)
}
getEmail.Close();
conn.Close();
}

private void sendMail(string sendTo, string sendAttachments)
{
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;
}
}
}

关于c# - 在数据库中发送带有附件路径的批量电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9796896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com