我正在开发一个程序,向我们公司的每位销售员工发送每周提醒/更新。每周大约120次,自动化,在短时间内完成。这是一个内部程序,仅适用于内部收件人,我不关心垃圾邮件或不需要的邮件,并且退订不是一种选择。每条消息都是根据每个销售人员的客户名单进行个性化设置的,并且包含每周“待办事项”,供他们打电话给客户。在内部交换smtp服务器上使用system.net.mail发送大量消息的超时
我跑Exhange 2007年内部与我的两个开发环境和运行这些自动化项目的服务器的开放的内部SMTP连接器。该程序对于一两家商店工作正常,但是当我为每家商店运行它时,我会在发送110封邮件后的某个地方得到一个时间。
我不会试图排队邮件或持有他们的块,因为我通过出售人名单迭代,并做相应的每一个查询和信息积累,我试图用下面的子发送消息。
Sub doMail(ByRef MessageBody As String, ByVal nameString As String, ByVal sendTo As ArrayList, Optional ByVal markurgent As Boolean = False, _
Optional ByVal sendCC As ArrayList = Nothing, Optional ByVal sendBcc As ArrayList = Nothing)
Try
' Setup Mail Message
Dim oClient As SmtpClient = New SmtpClient(ConfigurationManager.AppSettings("mail_server").ToString())
oClient.Timeout = 20000
'oClient.Port = 50747
Dim objMessage As New MailMessage()
objMessage.From = New System.Net.Mail.MailAddress(ConfigurationManager.AppSettings("mail_from_address").ToString, ConfigurationManager.AppSettings("mail_from_name").ToString)
objMessage.Subject = String.Format("Weekly Hitlist Report ~ {0}", nameString)
If (ConfigurationManager.AppSettings("debugMode").ToString() = 1) Then
Dim debugSB As StringBuilder = New StringBuilder
For Each s As String In sendTo
debugSB.AppendLine(String.Format("To: {0}<br>", s))
Next
For Each s As String In sendCC
debugSB.AppendLine(String.Format("cc: {0}<br>", s))
Next
For Each s As String In sendBcc
debugSB.AppendLine(String.Format("bcc: {0}<br>", s))
Next
MessageBody = String.Format("Debug Mode is Active. <br> {0} <br><hr noshade>{1}", debugSB.ToString, MessageBody)
objMessage.To.Add(ConfigurationManager.AppSettings("mail_to_address").ToString)
Else
For Each receiver As String In sendTo
Try
objMessage.To.Add(receiver.Trim())
Catch ex As Exception
' do nothing
End Try
Next
For Each receiver As String In sendCC.ToArray
Try
objMessage.CC.Add(receiver.Trim())
Catch ex As Exception
' do nothing
End Try
Next
For Each receiver As String In sendBcc
Try
objMessage.Bcc.Add(receiver.Trim())
Catch ex As Exception
' do nothing
End Try
Next
End If
objMessage.Body = MessageBody
objMessage.Priority = IIf(markurgent, MailPriority.High, MailPriority.Normal)
objMessage.IsBodyHtml = True
oClient.Send(objMessage)
oClient = Nothing
Console.WriteLine(String.Format("{1} - message sent - {0} ", nameString, Now()))
Catch ex As Exception
Console.WriteLine(ex.Message)
Console.WriteLine("****************")
Console.WriteLine(ex.StackTrace)
End Try
End Sub
我试图增加客户端超时为20秒,我明确试图将其设置为无每个消息后关闭了客户端连接到邮件服务器。
一切看起来都进行确定,直到最后一刻,我从下面的输出片段中删除我们的销售人员的名字。现在,一切都在我的身上,因为我在调试模式下运行它,我们还没有试图对整个公司进行实时运行,所以我不知道是否因为所有事情都发生在一个接收者身上。
后,我得到的错误(连接超时)和系统恢复,我得到的异常后,并只有在程序终止发送的任何消息。在.NET的早期版本中,我知道程序必须退出,或者线程必须在发送消息之前结束,但是我有其他程序,我通常不会在.NET 4中再使用它。
输出包括错误;第一部分已经被剪断
...
4/15/2013 9:46:36 AM - message sent -
4/15/2013 9:46:41 AM - message sent -
4/15/2013 9:46:46 AM - message sent -
4/15/2013 9:46:51 AM - message sent - Store 14 Unassigned
4/15/2013 9:46:56 AM - message sent -
4/15/2013 9:47:01 AM - message sent -
4/15/2013 9:47:06 AM - message sent -
4/15/2013 9:47:11 AM - message sent -
4/15/2013 9:47:16 AM - message sent -
4/15/2013 9:47:21 AM - message sent -
4/15/2013 9:47:26 AM - message sent -
4/15/2013 9:47:31 AM - message sent -
4/15/2013 9:47:36 AM - message sent -
4/15/2013 9:47:41 AM - message sent -
Service not available, closing transmission channel. The server response was: 4.4.1 Connection timed out
****************
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at HitListRunner_Main.Module1.doMail(String& MessageBody, String nameString, ArrayList sendTo, Boolean markurgent, ArrayList sendCC, ArrayList sendBcc) in C:\Users\markl\Documents\Visual Studio 2010\Projects\HitListRunner\HitListRunner-Main\Module1.vb:line 391
4/15/2013 9:47:46 AM - message sent - Store 20 Unassigned
4/15/2013 9:47:51 AM - message sent - Store 98 Unassigned
4/15/2013 9:47:58 AM - message sent - !! - UNDETERMINED LIST - !!
ending - enter to exit
我也曾尝试发送每个个体商店和较小的子集。我只在一次跑完整个公司时才收到这个错误。
我没有看到任何东西交换这种禁止,我没有看到一个高的消息队列等待。任何建议在不同的方法发送邮件或其他东西尝试? (我不想使用第三方DLL或外部组件)。
多少来,CC,BCC是这样被一次送往?你可以尝试在每次发送后延迟一段时间,看看是否有影响。 – dbasnett