发送邮件与任务并行库会抛出错误 [英] Sending mail with task parallel library throws error

查看:163
本文介绍了发送邮件与任务并行库会抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

code: -

List<Task> tasks = new List<Task>();
foreach (var item in arr)//arr contains 1000+ mailids
            {
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    using (MailMessage msg = new MailMessage())
                        {
                            msg=getmsg();//p-shadow code no erorr here
                            SmtpClient smtp = new SmtpClient();
                            smtp.Host = smtpServer;
                            smtp.Port = smtpPort;
                            smtp.EnableSsl = isSmtpSsl != 0 ? true : false;
                            smtp.Credentials = new NetworkCredential(smtpUName,smtpPass);
                            smtp.Timeout = int.MaxValue;
                            smtp.Send(msg);//---throws error....
                            //sql code to insert db that mail is send
                        }
                }, TaskCreationOptions.LongRunning));
            }
            Task.WaitAll(tasks.ToArray<Task>());

ERROR-失败发送邮件。

ERROR-"Failure sending mail."

内部ERROR-无法读取传输连接的数据:net_io_connectionclosed

INTERNAL ERROR-"Unable to read data from the transport connection: net_io_connectionclosed."

推荐答案

对于异常'net_io_connectionclosed,请看看这个链接,因为它可能指向你为什么连接被关闭了正确的方向。这可能是认证问题,防火墙等。

Regarding the exception 'net_io_connectionclosed', please look at this link as it might point you in the right direction about why the connection is being closed. It could be authentication problems, firewall etc.

启用跟踪的System.Net

请在下面找到了code,我会在你的情况,而不是产卵1000任务,这是不是很有效的使用。

Please find below the code that I would use in your situation instead of spawning 1000 tasks which is not very efficient.

发送大量电子邮件与任务Parallel.ForEach

这篇关于发送邮件与任务并行库会抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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