使用MailMessage发送电子邮件时出错 [英] Error in sending email using MailMessage

查看:106
本文介绍了使用MailMessage发送电子邮件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到此问题并尝试添加Dispose()函数,但仍然出现此错误:

System.IO.IOException:该进程无法访问文件"c:\ upload \ 07-2011.doc",因为该文件正在被另一个进程使用. System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs) ,位于System.IO.FileStream..ctor处的字符串,msgPath,布尔bFromProxy)(位于System.IO.FileStream..ctor处的字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项,字符串msgPath,布尔bFromProxy). (字符串路径,FileMode模式)位于newsletter.SendmailToAll()的System.Web.HttpPostedFile.SaveAs(String文件名)

Hello Everyone,

I have encountered this problem and trying to add Dispose() function but still i get this error:

System.IO.IOException: The process cannot access the file ''c:\upload\07 - 2011.doc'' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at newsletter.SendmailToAll()

protected void SendmailToAll()
    {
        string sql = "select email from distributors where email <> '''' and var1=''1''";
        oCon = oDB.dbOpen();
        SqlCommand DBCommand = new SqlCommand(sql, oCon);
        SqlDataReader sdr = DBCommand.ExecuteReader();
        while (sdr.Read())
        {
            email_id.Add(sdr["email"].ToString());
        }
        sdr.Close();
        oDB.dbClose();


        try
        {

            System.Net.Mail.MailMessage msgMail = new System.Net.Mail.MailMessage();

            if (FileUpload1.PostedFile.FileName != "")
            {
                string strdir = ConfigurationManager.AppSettings["MailAttachementPath"];
                string strfilename = Path.GetFileName(FileUpload1.PostedFile.FileName);

                FileUpload1.PostedFile.SaveAs(strdir + strfilename);

                msgMail.Attachments.Add(new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, strfilename));
            }


            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.com");
            smtp.Credentials = new NetworkCredential("test@mail.com", "pass");
            
            string strBody = "[message]";
            strBody = strBody.Replace("[message]", txtmsgBox.Text.ToString());
            //string mailto = "";
            for (int i = 0; i < email_id.Count; i++)
            {
                string email = email_id[i].ToString();

                if (email.Contains("@") && email.Contains("."))
                {
                    System.Threading.Thread.Sleep(60);
                    msgMail.To.Clear();
                    msgMail.To.Add(email);
                    msgMail.From = new System.Net.Mail.MailAddress("test@mail.com");
                    msgMail.Subject = "Newsletter";
                    msgMail.IsBodyHtml = true;

                    msgMail.Body = strBody;

                    smtp.Send(msgMail);

                    lblmsg.Text = "";
                    email = "";
                   

                    
                }

            }           //Here I Add Dispose function.
                        msgMail.Dispose();
                        
            if (lblmsg.Text == "")
            {
                lblmsg.Text = "Newsletter has been sent Successfully!";
                txtmsgBox.Text = "";
                txtEmail.Text = "";
            }
            
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.ToString();
        }
        
    }




谢谢.




Thank you.

推荐答案

而不是那样做,为什么不使用 [
Rather than doing it that way, why not use the BCC list[^]? That way, you just send it to a "safe" main address, and all the others get a copy without see the names. Single email leaves your system, arrives in all recipients. Then the file will not be in use for anyone...


有人可能会打开该文件以供阅读.....所以您不能独占它.
因此,检查文件是否存在并且可以以独占方式打开
someone might have the file open for reading.....so you cannot exclusively touch it.
So check if file exists and can be opened exclusive
<code><pre lang="C++">public static FileExist DoesFileExistOnServer(String FolderAndFileName)
        {
            // this class does not work
            FileExist doesExist = FileExist.No;
            if (!String.IsNullOrEmpty(FolderAndFileName))
            {
                FileInfo fi = new FileInfo(FolderAndFileName);
                if (fi.Exists)
                {
                    FileStream stream = null;
                    try
                    {
                        // check if the file can be opened exclusive. If that is possible, then the file is found otherwise in transfer
                        using (stream = File.Open(FolderAndFileName, FileMode.Open, FileAccess.Write, FileShare.None))
                        {
                            if (stream.CanWrite)
                                doesExist = FileExist.Yes;
                            else
                                doesExist = FileExist.Transferring;
                        }
                    }
                    catch (Exception err)
                    {
                        
                        doesExist = FileExist.Transferring;
                    }
                }
            }
            

            return doesExist;
        }



这篇关于使用MailMessage发送电子邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆