C# 发送邮件时出现错误,其他进程正在使用它。请稍后在.net中重试 我正在创建一个文件并通过邮件发送该文件,而 发送邮件我收到这个错误,你能建议发送什么吗 邮件
请告诉我在哪里添加代码,以停止“它是”的警告 bieng被另一个进程使用“ 受保护的无效BTN邮件单击(对象发送方,事件参数e) { Response.Clear()C# 发送邮件时出现错误,其他进程正在使用它。请稍后在.net中重试 我正在创建一个文件并通过邮件发送该文件,而 发送邮件我收到这个错误,你能建议发送什么吗 邮件,c#,asp.net,smtp,smtpclient,C#,Asp.net,Smtp,Smtpclient,请告诉我在哪里添加代码,以停止“它是”的警告 bieng被另一个进程使用“ 受保护的无效BTN邮件单击(对象发送方,事件参数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");
}
}
string Projname=Session[“Projectname”].ToString();
System.IO.StringWriter stringWrite=新的System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite=新的HtmlTextWriter(stringWrite);
设计.渲染控制(htmlWrite);
string strBuilder=stringWrite.ToString();
字符串strPath=Request.PhysicalApplicationPath+“\\Temp\\WeeklyReport of”+Projname+“.doc”;
LblNoteMsg.Text=strPath;
if(File.Exists(strPath))
{
删除(strPath);
}
FileStream fStream=File.Create(strPath);
fStream.Close();
fStream.Dispose();
StreamWriter sWriter=新的StreamWriter(strPath);
sWriter.Write(strBuilder);
sWriter.Close();
sWriter.Dispose();
Response.Clear();
DateTime输入=DateTime.Now;
int delta=DayOfWeek.Monday-input.DayOfWeek;
DateTime dats=DateTime.Now.AddDays(增量);
//本周
DateTime monday=input.AddDays(增量);
string MonDate=monday.ToShortDateString();
DateTime sat=星期一。添加天数(5);
字符串SatDate=sat.ToSortDateString();
StreamReader r=新的StreamReader(Server.MapPath(“~/WeeklyMail.txt”);
字符串体=r.ReadToEnd();
MailMessage Msg=新的MailMessage();
字符串MailId=txtMailId.Text;
foreach(MailId.Split(“,”.ToCharArray())中的字符串ss)
{
if(string.IsNullOrEmpty(ss)==false)
{
Msg.To.Add(新邮件地址(ss));
}
}
Msg.Bcc.Add(新邮件地址(“support@sunlightit.com"));
body=body.Replace(“,MonDate”);
body=body.Replace(“,SatDate”);
Msg.Subject=“每周状态报告”+Projname+,“+DateTime.Now.toSortDateString()+”;
Msg.Body=Body;
Msg.IsBodyHtml=true;
Msg.Attachments.Add(新附件(strPath));
SmtpClient MailServer=新的SmtpClient();
尝试
{
MailServer.Send(Msg);
字符串reply=(Msg.DeliveryNotificationOptions=System.Net.Mail.DeliveryNotificationOptions.OnSuccess).ToString();
如果(回复==“成功”)
{
txtMailId.Text=“”;
tblMail.Visible=false;
lblMsg.ForeColor=System.Drawing.Color.Green;
lblMsg.Text=“邮件已成功发送”;
}
其他的
{
lblMsg.ForeColor=System.Drawing.Color.Red;
lblMsg.Text=“邮件传递未成功”;
}
}
捕获(例外情况除外)
{
控制台写入线(ex);
if(例如InnerException!=null)
{
WriteLine(“InnerException为:{0}”,ex.InnerException);
}
}
}
其他的
{
重定向(“~/Login.aspx”);
}
}
捕获(例外)
{
ScriptManager.RegisterClientScriptBlock(第页,typeof(第页),“clentscript”,“警报('它正被另一个进程使用。请稍后再试”);“,true);
}
}FileStream fStream=File.Create(strPath) 你查过目录了吗? 字符串strPath=Request.PhysicalApplicationPath+“\Temp\WeeklyReport of”+Projname+“.doc” 如果(目录.Exists(“…\Temp”)) {
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");
}
}
}完全猜测-但我怀疑您遇到了一个进程,该进程维护的文件锁没有从以前的调用中释放(即第一次创建时打开文件流,留下一个浮动的锁,然后在随后的调用中尝试再次创建文件) 如果是我:我会将此位更改为使用File.writeAllines()(创建文件/覆盖现有文件、写入、关闭和一次性处理所有内容) 检查: 祝你好运 或 甚至更好——为什么要创建文件?如果您只是使用它们来附加到邮件,为什么不将内容写入memorystream,并围绕该流建立附件(在您将其重绕之后) 代码示例:
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 -我从代码中注意到,在发送到任何地方后,您不会删除文件。可能需要考虑。 查看代码,不需要调用.Debug(),因为这是使用块所用的,因此您可以有:
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()()
最后,我将研究将其中的一些(特别是“唯一文件名”位)引入扩展方法或可测试的辅助对象
嗯
Andy不,我没有检查,如果(Directory.Exists(“…\Temp”){,FileMode.Create,FileAccess.Write,FileShare.ReadWrite)){//…}}另一个代码段也使用临时文件夹吗?不..它没有被另一个代码使用。我也尝试过删除,但没有成功。文件流未清除。所以在t