我正在制定一个计划,每周向我们公司的每位销售员工发送提醒/更新。每周大约 120 个,自动化,在很短的时间内。这是一个内部程序,仅供内部收件人使用,我不担心垃圾邮件或不需要的消息,并且取消订阅不是一种选择。每条消息都根据每个销售人员的客户列表进行个性化,并包含每周“待办事项”项目,以便他们致电客户。
我在内部运行 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
我还尝试为每个单独的商店和较小的子集发送。我只在一次为整个公司竞选时才收到此错误。
我在 Exchange 中没有看到任何禁止此操作的内容,也没有看到高消息队列等待。关于发送消息的不同方法或其他尝试的任何建议?(我宁愿不使用第 3 方 dll 或外部组件)。