Asp.net 发送大量邮件超时的解决办法

我们知道在.Net中发送邮件使用的是SmtpClient 类,比如简单的如下:

            SmtpClient client = new SmtpClient(args[0]);
            // Specify the e-mail sender.
            // Create a mailing address that includes a UTF8 character
            // in the display name.
            MailAddress from = new MailAddress("jane@contoso.com",
               "Jane " + (char)0xD8+ " Clayton",
            System.Text.Encoding.UTF8);
            // Set destinations for the e-mail message.
            MailAddress to = new MailAddress("ben@contoso.com");
            // Specify the message content.
            MailMessage message = new MailMessage(from, to);
            message.Body = "This is a test e-mail message sent by an application. ";
            // Include some non-ASCII characters in body and subject.
            string someArrows = new string(new char[] {'/u2190', '/u2191', '/u2192', '/u2193'});
            message.Body += Environment.NewLine + someArrows;
            message.BodyEncoding =  System.Text.Encoding.UTF8;
            message.Subject = "test message 1" + someArrows;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            // Set the method that is called back when the send operation ends.
            client.SendCompleted += new
            SendCompletedEventHandler(SendCompletedCallback);
            // The userState can be any object that allows your callback
            // method to identify this send operation.
            // For this example, the userToken is a string constant.
            string userState = "test message1";
            client.SendAsync(message, userState);
            Console.WriteLine("Sending message... press c to cancel mail. Press any other key to exit.");
            string answer = Console.ReadLine();
            // If the user canceled the send, and mail hasn't been sent yet,
            // then cancel the pending operation.
            if (answer.StartsWith("c") && mailSent == false)
            {
                client.SendAsyncCancel();
            }
            // Clean up.
            message.Dispose();

但是,如果在一个WebForm程序中,如果直接使用上述方法发送大量的邮件(比如:10000封),则必定要出现处理超时的错误提示。

解决办法:在需要发送邮件的时候,使用线程启动另外一个外部程序(比如一个命令行程序,执行完毕后自动关闭),而在这个外部程序中发送这些大量的邮件。这个外部程序可以参考上面发送邮件的代码。

 

    protected void btnSend_Click(object sender, EventArgs e)
    {
        Session["ProcessRunning"] = "1";
        ThreadStart start = new ThreadStart(RunSendMailProcess);
        Thread t = new Thread(start);
        t.Start();
        Thread.Sleep(1000);
        //启动一个页面进行提示
        Page.RegisterStartupScript("newWindow", "<script>window.open('Progress.aspx', null, 'left=250,top=290,height=100,width=300,toolbar=no,status=no,menubar=no,location=center,scrollbars=no,resizable=no');</script>");   
    }

    protected void RunSendMailProcess()
    {
        string arguments = @"""" + txtSubject.Text + @"""  """ + txtBody.Text + @"""  """ + txtSMTP.Text + @"""  """ + txtFrom.Text + @"""  """ + chkViaSql.Checked + @"""";

        Process ps = new Process();
        ps.StartInfo.CreateNoWindow = false;
        ps.StartInfo.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["SendMailPath"];
        ps.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
        ps.StartInfo.Arguments = arguments;
        ps.StartInfo.FileName = "SendMail.exe";
        ps.StartInfo.UseShellExecute = true;
        ps.Start();
        lock (Session.SyncRoot)
        {
            Session["ProcessRunning"] = "1";
        }
        ps.WaitForExit();
        ps.Close();

        lock (Session.SyncRoot)
        {
            Session["ProcessRunning"] = "0";
        }
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值