asp.net 异步群发邮件时遭遇到的问题 ddddddddd-执着-iteye技术网站
asp.net 异步群发邮件时遭遇到的问题 ddddddddd-执着-iteye技术网站
2011年09月01日
客户要求为他的网站注册用户群发邮件
网站注册用户有9860名,因为注册时需要用户必须提供电子邮件,因为要对所有的9860名用户发送
如果采用主线程发送,必然造成连接超时或是漫长的用户等待,因而采用了异步发送的方式处理此群发
于是最初写了一个群发的类
#Region"群发邮件处理类"
'''
'''邮件群发异步处理类
'''
_
PublicClassBulkMail
Private_maxCountAsInteger=0
Private_currentCountAsInteger=0
Private_runStateAsBulkMailRunState=BulkMailRunState.Unkonw
Private_messageAsString
Private_listAsList(OfEntity.Member.OwnerIdentityEntity)
Private_threadAsThreading.Thread
Private_mailSubjectAsString
Private_mailBodyAsString
Private_configAsWeb.Config.WebConfig
'''
'''创建一个邮件群发实例
'''
'''邮件主题
'''邮件内容
PublicSubNew(ByValsubjectAsString,ByValbodyAsString)
_config=Web.Config.WebConfig.GetConfig
_mailSubject=subject
_mailBody=body
EndSub
'''
'''初始化发送事件
'''
'''信息集合
PublicSubSend(ByVallistAsList(OfEntity.Member.OwnerIdentityEntity))
_list=list
_thread=NewThreading.Thread(NewThreading.ThreadStart(AddressOfMe.SendMail))
_thread.Start()
EndSub
'''
'''异步发送邮件
'''
PrivateSubSendMail()
Try
_message="群发开始"
_runState=BulkMailRunState.Run
_currentCount=0
_maxCount=_list.Count
DimmailAsWeb.Mail.Mail
mail=NewWeb.Mail.Mail
mail.From=_config.SmtpEmail
mail.FromName=_config.SiteName
mail.isHtml=True
mail.Priority="high"
mail.Subject=_mailSubject
mail.Body=_mailBody
DimsmtpAsWeb.Mail.SmtpMail
smtp=NewWeb.Mail.SmtpMail
smtp.chkSmtp=_config.SmtpAuth
smtp.smtpPassWord=_config.SmtpLoginPassword
smtp.SmtpPort=_config.SmtpPort
smtp.SmtpServer=_config.SmtpServer
smtp.smtpUserName=_config.SmtpLoginName
ForEachinfoAsEntity.Member.OwnerIdentityEntityIn_list
_currentCount+=1
Ifinfo.EmailIsNotNothingAndAlsoinfo.Email.Contains("@")Then
mail.Recipient.Add(info.Email)
_message=String.Format("正在给{0}发送邮件",info.RealName)
_runState=BulkMailRunState.Run
mail.Subject=_mailSubject
mail.Body=_mailBody
smtp.Send(mail)
mail.Recipient.Clear()
EndIf
Next
_runState=BulkMailRunState.Over
_message="群发结束"
CatchexAsException
_runState=BulkMailRunState.Error
_message=ex.Message&ex.Source&ex.ToString&ex.InnerException.ToString
EndTry
EndSub
#Region"自定义属性"
'''
'''群发状态
'''
'''Thestateoftherun.
PublicReadOnlyPropertyRunState()AsBulkMailRunState
Get
Return_runState
EndGet
EndProperty
'''
'''群发邮件总数
'''
'''Themaxcount.