【发布时间】:2017-02-19 00:03:00
【问题描述】:
我想使用 for 循环发送多封包含相同 pdf 文档的电子邮件, 现在第一个电子邮件地址总是收到电子邮件,但随后的电子邮件地址出现错误`进程无法访问文件'file\path\file.pdf',因为它正在被另一个进程使用。
foreach (GridViewRow Row in GridView1.Rows)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(smtpServer);
mail.From = new MailAddress(Sender);
mail.To.Add(new MailAddress(to));
mail.Subject = Subject;
mail.Body = TextBox2.Text;
Attachment attachment;
attachment = new Attachment(Server.MapPath("~/temp/file.pdf"));
mail.Attachments.Add(attachment);
SmtpServer.Port = SmtpPort;
SmtpServer.Credentials = new System.Net.NetworkCredential(Sender, Password);
SmtpServer.EnableSsl = true;
try
{
SmtpServer.Send(mail);
sent++;
}
catch (Exception ex)
{
Label1.Visible = true;
Label1.Text = ex.Message.ToString();
if (ex.InnerException != null)
{
Label1.Text = ex.InnerException.ToString();
Label1.Visible = true;
}
}
Label2.Text = "Sent: " + sent + "Failed: " + failed + " Without Email:" + NoEmail;
Label2.Visible = true;
}
【问题讨论】:
-
请贴出相关代码
-
听起来您要么不处理文件,要么不处理第一封电子邮件中的 MailAttachment
-
刚刚添加了代码