当前位置: 首页 > 知识库问答 >
问题:

发送错误邮件时,另一个进程正在使用该邮件。请稍后在。NET中尝试

娄飞鸾
2023-03-14

>

  • 我正在创建一个文件并通过邮件发送该文件,当发送邮件时,我得到了这个错误,你能建议发送什么邮件吗?
  • 请告诉我在哪里添加代码以防止出现“它是由另一个进程bieng userd”的警告

    受保护的void btnMail_Click(object sender,EventArgs e){response.clear();

    FileStream fStream=file.create(strPath);在这条线上,它跳着接住块

        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");
        }
    }
    

    从这一行开始,它将执行catch block.file不是creating FileStream fStream=file.create(strPath);

  • 共有1个答案

    江琦
    2023-03-14

    查看您的代码,您不需要执行对.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最终会挂在文件系统上(这是可取的吗?)

     类似资料:
    • 我正在尝试使用thymeleaf模板发送电子邮件。但我收到一条错误消息 我不能使用WebContext()而不是Context()。因为对于webContext,我需要我无法在这里获得的HttpServletRequest对象,因为它不在请求范围内。我正在尝试使用上下文()发送邮件,但我收到了错误。请任何人对此有解决方案。提前感谢这是我的代码,

    • 我在开快车。js应用程序,其中我有一个返回json对象的路由 https://serene-depths-3284.herokuapp.com/chapters?setLng=en-UK 在我的控制器文件夹中,我有章节。咖啡文件,如下所示: 该代码可在以下网址找到:https://github.com/TZM/tzm-blade/blob/master/app/controllers/chapte

    • 我需要将邮件从我的gmail帐户发送到另一个帐户。我使用了以下代码。 但我得到的错误如下。。。javax。邮政MessaginException:无法连接到SMTP主机:SMTP。gmail。com,端口:25 我该怎么解决这个问题。你能帮我吗?

    • 这是我发送邮件的代码 我在tomcat的lib文件夹中添加了和。我将eclipse与Tomcat7、JRE7和Struts2一起使用。通过Java发送邮件时出现以下控制台错误:

    • 使用根据java文档 使用此客户端进行的所有服务调用都是阻塞的,并且在服务调用完成之前不会返回。 快速的问题是,它是线程安全的吗?。多个线程是否可以使用相同的实例并行调用

    • 我得到这个错误 2020-06-03 21:08:07连接:打开到ssl://smtp.gmail.com:25,超时=300,选项=数组()2020-06-03 21:08:07连接失败。 错误#2:stream_socket_client():代码1的SSL操作失败。 OpenSSL错误消息:错误:1408F10B:SSL例程:ssl3\u get\u记录:错误的版本号[C:\xampp\ht