发送邮件时出现错误 It is being used by another process.Please 稍后在 .net 中尝试
while sending mail giving error It is being used by another process.Please Try after sometime in .net
- 我正在创建一个文件并通过邮件发送该文件,同时
发送邮件我收到这个错误,你能建议发送什么吗
邮件?
请告诉我在哪里添加代码以停止发出“它是”的警告
bieng 被另一个进程使用
protected void btnMail_Click(object sender, EventArgs e)
{
Response.Clear();
FileStream fStream = File.Create(strPath);在这一行,它跳转到 catch 块
Response.Clear();
尝试
{
if (Session["Projectname"] != null && Session["Projectname"].ToString() != string.Empty)
{
string Projname = Session["Projectname"].ToString();
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
design.RenderControl(htmlWrite);
string strBuilder = stringWrite.ToString();
string strPath = Request.PhysicalApplicationPath + "\Temp\WeeklyReport of " + Projname + ".doc";
LblNoteMsg.Text = strPath;
if (File.Exists(strPath))
{
File.Delete(strPath);
}
FileStream fStream = File.Create(strPath);
fStream.Close();
fStream.Dispose();
StreamWriter sWriter = new StreamWriter(strPath);
sWriter.Write(strBuilder);
sWriter.Close();
sWriter.Dispose();
Response.Clear();
DateTime input = DateTime.Now;
int delta = DayOfWeek.Monday - input.DayOfWeek;
DateTime dats = DateTime.Now.AddDays(delta);
//this week
DateTime monday = input.AddDays(delta);
string MonDate = monday.ToShortDateString();
DateTime sat = monday.AddDays(5);
string SatDate = sat.ToShortDateString();
StreamReader r = new StreamReader(Server.MapPath("~/WeeklyMail.txt"));
string body = r.ReadToEnd();
MailMessage Msg = new MailMessage();
string MailId = txtMailId.Text;
foreach (string ss in MailId.Split(",".ToCharArray()))
{
if (string.IsNullOrEmpty(ss) == false)
{
Msg.To.Add(new MailAddress(ss));
}
}
Msg.Bcc.Add(new MailAddress("support@sunlightit.com"));
body = body.Replace("<%MonDate%>", MonDate);
body = body.Replace("<%SatDate%>", SatDate);
Msg.Subject = "Weekly status Report of " + Projname + "," + DateTime.Now.ToShortDateString() + "";
Msg.Body = body;
Msg.IsBodyHtml = true;
Msg.Attachments.Add(new Attachment(strPath));
SmtpClient MailServer = new SmtpClient();
try
{
MailServer.Send(Msg);
string reply = (Msg.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess).ToString();
if (reply == "OnSuccess")
{
txtMailId.Text = "";
tblMail.Visible = false;
lblMsg.ForeColor = System.Drawing.Color.Green;
lblMsg.Text = "Mail has send succesfully";
}
else
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = "Mail delivery unsuccessfull";
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
if (ex.InnerException != null)
{
Console.WriteLine("InnerException is: {0}", ex.InnerException);
}
}
}
else
{
Response.Redirect("~/Login.aspx");
}
}
赶上(异常)
{
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "clentscript", "alert('It is being used by another process.Please Try after sometime ');", true);
}
}
从这一行开始,它将捕获块 .file 未创建
文件流 fStream = File.Create(strPath);
你检查目录了吗?
string strPath = Request.PhysicalApplicationPath + "\Temp\WeeklyReport of " + Projname + ".doc";
if(Directory.Exists("...\Temp"))
{
}
完全猜测 - 但我怀疑你 运行 进入了一个维护文件锁的进程,该文件锁尚未从以前的调用中释放(即 fileStream 在第一次创建时打开,留下锁定浮动然后在随后尝试再次创建文件的调用中)
如果是我:我会更改此位以使用 File.WriteAllLines()(创建文件/覆盖现有文件,一次写入、关闭和处理所有内容)
祝你好运!
或
更好 - 为什么要创建文件?如果您仅使用它们来附加到邮件,为什么不将内容写入内存流并围绕该流建立附件(在您回绕它之后)。
Here's a link to the attachment CTor you'll need on MSDN
代码示例:
System.Net.Mail.Attachment attachment = null;
using (var ms = new System.IO.MemoryStream())
{
using (var sw = new System.IO.StreamWriter(ms))
{
sw.Write("contents here");
sw.Flush();
}
ms.Position = 0;
attachment = new System.Net.Mail.Attachment(ms, "FileNameHere.txt");
}
var msg = new System.Net.Mail.MailMessage()
{
Body = "body text here",
Subject = "some subject"
};
msg.Attachments.Add(attachment);
PS - 我从你的代码中注意到你在发送到任何地方后都没有删除文件 - 可能需要考虑这一点。
看看你的代码,你不需要调用 .dispose() 因为这是 using 块的用途,所以你可以:
using (var fStream = File.Create(strPath).Close())
{
// these can go - (arguable you dont even need to do the close above,
// but it's there for completeness's sake)
//fStream.Close();
//fStream.Dispose();
}
using(StreamWriter sWriter = new StreamWriter(strPath))
{
sWriter.Write(strBuilder);
sWriter.Close();
//sWriter.Dispose(); - this can go too
Response.Clear();
}
此外,您可能应该对使用 StreamReader 的位使用类似的方法。
此外 - strPath 最终会在文件系统上徘徊(这是可取的吗?)
如果您只是想确保唯一的文件名,您可以使用 Path.GetRandomFilename() (see this MSDN article)
最后,我会考虑将其中的一些(尤其是 "unique file name" 位)拉入扩展方法或可测试的辅助对象中。
HTH!
安迪
- 我正在创建一个文件并通过邮件发送该文件,同时 发送邮件我收到这个错误,你能建议发送什么吗 邮件?
请告诉我在哪里添加代码以停止发出“它是”的警告 bieng 被另一个进程使用
protected void btnMail_Click(object sender, EventArgs e) { Response.Clear();
FileStream fStream = File.Create(strPath);在这一行,它跳转到 catch 块
Response.Clear();
尝试 { if (Session["Projectname"] != null && Session["Projectname"].ToString() != string.Empty) {
string Projname = Session["Projectname"].ToString(); System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); design.RenderControl(htmlWrite); string strBuilder = stringWrite.ToString(); string strPath = Request.PhysicalApplicationPath + "\Temp\WeeklyReport of " + Projname + ".doc"; LblNoteMsg.Text = strPath; if (File.Exists(strPath)) { File.Delete(strPath); } FileStream fStream = File.Create(strPath); fStream.Close(); fStream.Dispose(); StreamWriter sWriter = new StreamWriter(strPath); sWriter.Write(strBuilder); sWriter.Close(); sWriter.Dispose(); Response.Clear(); DateTime input = DateTime.Now; int delta = DayOfWeek.Monday - input.DayOfWeek; DateTime dats = DateTime.Now.AddDays(delta); //this week DateTime monday = input.AddDays(delta); string MonDate = monday.ToShortDateString(); DateTime sat = monday.AddDays(5); string SatDate = sat.ToShortDateString(); StreamReader r = new StreamReader(Server.MapPath("~/WeeklyMail.txt")); string body = r.ReadToEnd(); MailMessage Msg = new MailMessage(); string MailId = txtMailId.Text; foreach (string ss in MailId.Split(",".ToCharArray())) { if (string.IsNullOrEmpty(ss) == false) { Msg.To.Add(new MailAddress(ss)); } } Msg.Bcc.Add(new MailAddress("support@sunlightit.com")); body = body.Replace("<%MonDate%>", MonDate); body = body.Replace("<%SatDate%>", SatDate); Msg.Subject = "Weekly status Report of " + Projname + "," + DateTime.Now.ToShortDateString() + ""; Msg.Body = body; Msg.IsBodyHtml = true; Msg.Attachments.Add(new Attachment(strPath)); SmtpClient MailServer = new SmtpClient(); try { MailServer.Send(Msg); string reply = (Msg.DeliveryNotificationOptions = System.Net.Mail.DeliveryNotificationOptions.OnSuccess).ToString(); if (reply == "OnSuccess") { txtMailId.Text = ""; tblMail.Visible = false; lblMsg.ForeColor = System.Drawing.Color.Green; lblMsg.Text = "Mail has send succesfully"; } else { lblMsg.ForeColor = System.Drawing.Color.Red; lblMsg.Text = "Mail delivery unsuccessfull"; } } catch (Exception ex) { Console.WriteLine(ex); if (ex.InnerException != null) { Console.WriteLine("InnerException is: {0}", ex.InnerException); } } } else { Response.Redirect("~/Login.aspx"); } }
赶上(异常) { ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "clentscript", "alert('It is being used by another process.Please Try after sometime ');", true); }
}
从这一行开始,它将捕获块 .file 未创建 文件流 fStream = File.Create(strPath);
你检查目录了吗? string strPath = Request.PhysicalApplicationPath + "\Temp\WeeklyReport of " + Projname + ".doc";
if(Directory.Exists("...\Temp")) {
}
完全猜测 - 但我怀疑你 运行 进入了一个维护文件锁的进程,该文件锁尚未从以前的调用中释放(即 fileStream 在第一次创建时打开,留下锁定浮动然后在随后尝试再次创建文件的调用中)
如果是我:我会更改此位以使用 File.WriteAllLines()(创建文件/覆盖现有文件,一次写入、关闭和处理所有内容)
祝你好运!
或
更好 - 为什么要创建文件?如果您仅使用它们来附加到邮件,为什么不将内容写入内存流并围绕该流建立附件(在您回绕它之后)。
Here's a link to the attachment CTor you'll need on MSDN
代码示例:
System.Net.Mail.Attachment attachment = null;
using (var ms = new System.IO.MemoryStream())
{
using (var sw = new System.IO.StreamWriter(ms))
{
sw.Write("contents here");
sw.Flush();
}
ms.Position = 0;
attachment = new System.Net.Mail.Attachment(ms, "FileNameHere.txt");
}
var msg = new System.Net.Mail.MailMessage()
{
Body = "body text here",
Subject = "some subject"
};
msg.Attachments.Add(attachment);
PS - 我从你的代码中注意到你在发送到任何地方后都没有删除文件 - 可能需要考虑这一点。
看看你的代码,你不需要调用 .dispose() 因为这是 using 块的用途,所以你可以:
using (var fStream = File.Create(strPath).Close())
{
// these can go - (arguable you dont even need to do the close above,
// but it's there for completeness's sake)
//fStream.Close();
//fStream.Dispose();
}
using(StreamWriter sWriter = new StreamWriter(strPath))
{
sWriter.Write(strBuilder);
sWriter.Close();
//sWriter.Dispose(); - this can go too
Response.Clear();
}
此外,您可能应该对使用 StreamReader 的位使用类似的方法。
此外 - strPath 最终会在文件系统上徘徊(这是可取的吗?)
如果您只是想确保唯一的文件名,您可以使用 Path.GetRandomFilename() (see this MSDN article)
最后,我会考虑将其中的一些(尤其是 "unique file name" 位)拉入扩展方法或可测试的辅助对象中。
HTH!
安迪