Hellow,我尝试创建一个pdf文件,并同时将它们同时循环发送到其他电子邮件地址,但是似乎第一个创建的文件并没有关闭以允许创建下一个文件,我使用了相同的名称(覆盖文件)。
这是附加电子邮件的代码
private void sendEmail(string email)
{
sendInfo = new Label();
sendInfo.Font = new Font("Calibri", 11);
sendInfo.AutoSize = true;
MailMessage mail = new MailMessage("ikwabe04@gmail.com", email, "TESTING THE SALARY SLIP EMAIL SENDER", "Habari Rafiki? Usishitushwe na ujumbe huu, tunajaribu system. Asante.");
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("ikwabe04@gmail.com", "mikunjoyamwili");
client.EnableSsl = true;
Attachment file = new Attachment("C:/Users/" + Home.computerName + "/AppData/Roaming/SEC Payroll/Receipts/receipt.pdf");
file.Name = "Salary Slip for " + DateTime.Now.ToString("MMMM yyyy") + ".pdf";
mail.Attachments.Add(file);
try
{
client.Send(mail);
Login.RecordUserActivity("Sent the Salary Slip to " + email);
sendInfo.ForeColor = Color.LightGreen;
sendInfo.Text = "Email sent to: " + email + " (" + DateTime.Now.ToLongTimeString() + ")";
information.Controls.Add(sendInfo);
}
catch
{
sendInfo.ForeColor = Color.Orange;
sendInfo.Text = "Email NOT sent to: " + email + " ("+DateTime.Now.ToLongTimeString()+")";
information.Controls.Add(sendInfo);
}
}
此处创建PDF的代码
using (FileStream file = new FileStream("C:/Users/" + Home.computerName + "/AppData/Roaming/SEC Payroll/Receipts/receipt.pdf", FileMode.Create))
{
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter.GetInstance(pdfDoc, file);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Add(slp);
pdfDoc.Add(Separator);
pdfDoc.Add(table1);
pdfDoc.Add(Separator);
pdfDoc.Add(Tsh);
pdfDoc.Add(incomeTitle);
pdfDoc.Add(incomeTable);
pdfDoc.Add(totaInc);
pdfDoc.Add(taxDeductionTitle);
pdfDoc.Add(taxDeduction);
pdfDoc.Add(otherDeductionTitle);
pdfDoc.Add(OtherDeduction);
pdfDoc.Add(totaDeduc);
pdfDoc.Close();
file.Close();
}
这是发送电子邮件的代码
for (int i = 0; i< table.Rows.Count;i++)
{
PreapareSalarySlip(table.Rows[i][2].ToString(),
table.Rows[i][3].ToString(),
table.Rows[i][5].ToString(),
table.Rows[i][37].ToString()
);
sendEmail(table.Rows[i][38].ToString());
}
这里是发生错误的地方 mscorlib.dll中发生了'System.IO.IOException'类型的未处理异常
其他信息:该进程无法访问文件“ C:\ Users \ Shadrack Ikwabe \ AppData \ Roaming \ SEC Payroll \ Receipts \ receipt.pdf”,因为该文件正在被另一个进程使用。
答案 0 :(得分:1)
锁定文件的不是PDF创建过程。这是附件。 Attachment
类将锁定文件,并且在释放文件之前不会释放该锁定。之所以会出现异常,是因为您试图将同一文件附加到两封不同的电子邮件中,而不通过释放附件来释放锁定。
SmtpClient
和MailMessage
也是一次性的。处置MailMessage
就足够了;当它被处置时,它也会处置其附件。但是,我认为最好使其明确。
您应该使用using
妥善处理它们:
using (MailMessage mail = new MailMessage("ikwabe04@gmail.com", email, "TESTING THE SALARY SLIP EMAIL SENDER", "Habari Rafiki? Usishitushwe na ujumbe huu, tunajaribu system. Asante."))
using (SmtpClient client = new SmtpClient("smtp.gmail.com"))
using (Attachment file = new Attachment("C:/Users/" + Home.computerName + "/AppData/Roaming/SEC Payroll/Receipts/receipt.pdf"))
{
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("ikwabe04@gmail.com", "mikunjoyamwili");
client.EnableSsl = true;
file.Name = "Salary Slip for " + DateTime.Now.ToString("MMMM yyyy") + ".pdf";
mail.Attachments.Add(file);
try
{
client.Send(mail);
Login.RecordUserActivity("Sent the Salary Slip to " + email);
sendInfo.ForeColor = Color.LightGreen;
sendInfo.Text = "Email sent to: " + email + " (" + DateTime.Now.ToLongTimeString() + ")";
information.Controls.Add(sendInfo);
}
catch
{
sendInfo.ForeColor = Color.Orange;
sendInfo.Text = "Email NOT sent to: " + email + " ("+DateTime.Now.ToLongTimeString()+")";
information.Controls.Add(sendInfo);
}
}