知行合一

博客园 首页 新随笔 联系 订阅 管理
代码
Imports System.Net.Mail
Imports System.Net.Configuration
Imports System.Configuration
Imports System.Text
Imports PMS.Rule
Imports PMS.ServerObject.ServerData
Public Class MailHander


    
Private Shared mSMTPServer As String = System.Configuration.ConfigurationManager.AppSettings("SMTPServer")
    
Private Shared mSMTPPort As String = System.Configuration.ConfigurationManager.AppSettings("SMTPPort")
    
Private Shared mSMTPUsername As String = System.Configuration.ConfigurationManager.AppSettings("SMTPUserName")
    
Private Shared mSMTPPassword As String = System.Configuration.ConfigurationManager.AppSettings("SMTPPassword")
    
Private Shared mFrom As String = System.Configuration.ConfigurationManager.AppSettings("MailFrom")
    
Private Shared mMailDisplyName As String = System.Configuration.ConfigurationManager.AppSettings("MailDisplyName")
    
Private Shared mMailBcc As String = System.Configuration.ConfigurationManager.AppSettings("MailBcc")
    
Private Shared mCheckInEmailSubject As String = System.Configuration.ConfigurationManager.AppSettings("CheckInEmailSubject")
    
Private Shared mCheckOutEmailSubject As String = System.Configuration.ConfigurationManager.AppSettings("CheckOutEmailSubject")
    
Private Shared mRegainCheckInEmailSubject As String = System.Configuration.ConfigurationManager.AppSettings("RegainCheckInEmailSubject")


    
Private Delegate Function InvokeSendMailForOneCheckIn(ByVal objGuestBase As GuestBase, ByVal enumSubject As MailSubject) As Boolean
    
Private Delegate Function InvokeSendMailForBatchCheckIn(ByVal lstGuestBase As List(Of GuestBase)) As Boolean
    
Private Delegate Sub InvokeSendMailForCheckOut(ByVal objGuest As Guest)

    
Private delegSendMailForOneCheckIn As InvokeSendMailForOneCheckIn
    
Private delegSendMailForBatchCheckIn As InvokeSendMailForBatchCheckIn
    
Private delegSendMailForCheckOut As InvokeSendMailForCheckOut
    
Private arSendMailForOneCheckIn As IAsyncResult
    
Private arSendMailForBatchCheckIn As IAsyncResult
    
Private arSendMailForCheckOut As IAsyncResult


    
Public Function SendMailForOneCheckIn(ByVal objGuestBase As GuestBase, ByVal enumSubject As MailSubject) As Boolean

        
Dim boolRet As Boolean = False
        
Try

            
If IsFeeHouseMarkCode(objGuestBase.RentKindId) Then
                
Dim strReceive As String = GetAuthorizerEmail(objGuestBase.AuthorizerId)
                
Dim strHtmlBody As String = GetBodyHtmlToCheckin(objGuestBase)


                
Select Case enumSubject

                    
Case MailSubject.CheckInSubject
                        SendMail(strReceive, 
String.Empty, mMailBcc, mCheckInEmailSubject, strHtmlBody, String.Empty)
                    
Case MailSubject.RecoveryCheckInSubject
                        SendMail(strReceive, 
String.Empty, mMailBcc, mRegainCheckInEmailSubject, strHtmlBody, String.Empty)

                
End Select

                
Return True
            
End If

            
Return boolRet

        
Catch ex As Exception
            MessageBox.Show(
"发送邮件失败!")
            
Return False
        
Finally

        
End Try

    
End Function

    
Public Function SendMailForBatchCheckIn(ByVal lstGuestBase As List(Of GuestBase)) As Boolean

        
Dim boolRet As Boolean = False
        
Try
            
For Each itmGuestBase As GuestBase In lstGuestBase

                SendMailForOneCheckIn(itmGuestBase, MailSubject.CheckInSubject)
            
Next
            
Return True
        
Catch ex As Exception
            MessageBox.Show(ex.Message)
            
Return boolRet
        
Finally

        
End Try

    
End Function

    
Private Sub SendMailForCheckOut(ByVal objGuest As Guest)

        
SyncLock objGuest

            SendMailForCheckOutHand(objGuest)

        
End SyncLock

    
End Sub

    
Public Sub BeginSendMailForOneCheckIn(ByVal objGuestBase As GuestBase, ByVal enumSubject As MailSubject)
        delegSendMailForOneCheckIn 
= New InvokeSendMailForOneCheckIn(AddressOf SendMailForOneCheckIn)
        arSendMailForOneCheckIn 
= delegSendMailForOneCheckIn.BeginInvoke(objGuestBase, enumSubject, NothingNothing)

    
End Sub

    
Public Sub BeginSendMailForBatchCheckIn(ByVal lstGuestBase As List(Of GuestBase))
        delegSendMailForBatchCheckIn 
= New InvokeSendMailForBatchCheckIn(AddressOf SendMailForBatchCheckIn)
        arSendMailForBatchCheckIn 
= delegSendMailForBatchCheckIn.BeginInvoke(lstGuestBase, NothingNothing)

    
End Sub

    
Public Sub BeginSendMailForCheckOut(ByVal objGuest As Guest)
        delegSendMailForCheckOut 
= New InvokeSendMailForCheckOut(AddressOf SendMailForCheckOut)
        arSendMailForCheckOut 
= delegSendMailForCheckOut.BeginInvoke(objGuest, NothingNothing)

    
End Sub

    
Public Sub EndSendMailForCheckOut()
        delegSendMailForCheckOut.EndInvoke(arSendMailForCheckOut)
    
End Sub


    
Public Sub EndSendMailForBatchCheckIn()
        delegSendMailForBatchCheckIn.EndInvoke(arSendMailForBatchCheckIn)
    
End Sub

    
Public Sub EndSendMailForOneCheckIn()
        delegSendMailForOneCheckIn.EndInvoke(arSendMailForOneCheckIn)
    
End Sub


    
Public Sub SendMailForCheckOutHand(ByVal objGuest As Guest)

        
Dim cashList As CashItemCollection = objGuest.GetCashList()
        
Dim subCashList As CashItemCollection = objGuest.GetPreCashList()
        
Dim fileName As String = String.Empty
        
Try
            
If IsFeeHouseMarkCode(objGuest.data.RentKindId) Then

                
Dim dataObject As New CheckOutInfo()
                fileName 
= dataObject.GetAccountBill(objGuest, cashList, subCashList)
                
Dim strReceive As String = GetAuthorizerEmail(objGuest.data.AuthorizerId)
                
Dim strHtmlBody As String = GetBodyHtmlToCheckOut(objGuest.data)
                SendMail(strReceive, 
String.Empty, mMailBcc, mCheckOutEmailSubject, strHtmlBody, fileName)
            
End If

        
Catch ex As Exception
            MessageBox.Show(
"发送邮件失败!")
            
'MessageBox.Show(ex.Message)
        Finally

        
End Try


    
End Sub




#Region "邮件发送"

    
Public Function SendMail(ByVal strReceive As StringByVal strCC As StringByVal strBCC As StringByVal strSubject As StringByVal strContent As StringByVal strFileName As StringAs Boolean


        
Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
        
'收件人
        message.To.Add(strReceive)
        
'抄送
        Dim mailCC As String
        
For Each mailCC In strCC.Split(","c)
            
If (Not String.IsNullOrEmpty(mailCC)) Then

                message.CC.Add(mailCC)

            
End If

        
Next
        
'暗送
        Dim mailBcc As String
        
For Each mailBcc In strBCC.Split(","c)
            
If (Not String.IsNullOrEmpty(mailBcc)) Then

                message.Bcc.Add(mailBcc)

            
End If

        
Next
        
'主题
        message.Subject = strSubject

        message.From 
= New System.Net.Mail.MailAddress(mFrom, mMailDisplyName) '设置发件人信箱,及显示名字
        message.IsBodyHtml = True '指定邮件格式,支持HTML格式 
        message.Body = strContent
        message.BodyEncoding 
= System.Text.Encoding.UTF8  '邮件采用的编码 System.Text.Encoding.GetEncoding("GB2312")
        message.Priority = System.Net.Mail.MailPriority.High '设置邮件的优先级为高

        
'发送附件
        If Not String.IsNullOrEmpty(strFileName) Then
            message.Attachments.Add(
New System.Net.Mail.Attachment(strFileName))
        
End If
        
Dim smtp As SmtpClient = New SmtpClient(mSMTPServer, mSMTPPort)
        smtp.Credentials 
= New System.Net.NetworkCredential(mSMTPUsername, mSMTPPassword)
        
'smtp.usedefaultcredentials = true;

        
Try
            smtp.Send(message)

            
Return True
        
Catch ex As System.Net.Mail.SmtpException

            
Return False
        
Finally
            message.Dispose() 
'释放资源
        End Try



    
End Function

#End Region

#Region "授权人信息"


    
Private Function IsFeeHouseMarkCode(ByVal rentKindId As IntegerAs Boolean

        
Dim boolRet As Boolean = False

        
Dim objRentkind As RentKindBase

        
If rentKindId > 0 Then

            objRentkind 
= RentKindCollection.All.getById(rentKindId)

            
If objRentkind.MarkCodeId = 1 OrElse objRentkind.MarkCodeId = 5 Then

                
Return True

            
End If


        
End If


        
Return boolRet

    
End Function


    
Private Function GetAuthorizerEmail(ByVal authorizerId As IntegerAs String

        
Dim strRet As String = String.Empty

        
Dim curAuthorizer As AuthorizerBase

        
If authorizerId > 0 Then

            curAuthorizer 
= AuthorizerCollection.All.GetById(authorizerId)

            
If Not curAuthorizer Is Nothing Then

                strRet 
= curAuthorizer.Email

            
End If

        
End If

        
Return strRet

    
End Function


    
Private Function GetAuthorizerName(ByVal authorizerId As IntegerAs String

        
Dim strRet As String = String.Empty

        
Dim curAuthorizer As AuthorizerBase

        
If authorizerId > 0 Then

            curAuthorizer 
= AuthorizerCollection.All.GetById(authorizerId)

            
If Not curAuthorizer Is Nothing Then

                strRet 
= curAuthorizer.Name

            
End If

        
End If

        
Return strRet

    
End Function

    
Private Function GetAuthorizationReason(ByVal authorizationId As IntegerAs String

        
Dim strRet As String = String.Empty

        
Dim curAuthorizerReasonBase As AuthorizerReasonBase

        
If authorizationId > 0 Then

            curAuthorizerReasonBase 
= AuthorizerReasonCollection.All.GetById(authorizationId)

            
If Not curAuthorizerReasonBase Is Nothing Then

                strRet 
= curAuthorizerReasonBase.Name

            
End If

        
End If

        
Return strRet

    
End Function
#End Region


    
''' <summary>
    
''' 入住邮件体
    
''' </summary>
    
''' <param name="dataObj"></param>
    
''' <returns></returns>
    
''' <remarks></remarks>

    
Public Function GetBodyHtmlToCheckin(ByVal dataObj As GuestBase) As String
        
Dim dataHotel As tHotelsBase = tHotelsCollection.All.curHotel
        
Dim builder As New System.Text.StringBuilder
        ModelLoad.RefreshTimeParams()


        builder.Append(
"<html>")
        builder.Append(
"<head>")
        builder.Append(
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />")
        builder.Append(
"<title>")
        builder.Append(
"</title>")
        builder.Append(
"<style type='text/css'>")
        builder.Append(
"<!--")
        builder.Append(
"td {  font-family: '宋体'; font-size: 9pt}")
        builder.Append(
"-->")
        builder.Append(
"</style>")
        builder.Append(
"</head>")

        builder.Append(
"<body  bgcolor='#FFFFFF' text='#000000'>")
        builder.Append(
"<table width='549' border='0' cellspacing='0' cellpadding='0'>")
        builder.Append(
"<tr>")
        builder.Append(
"<td width='549'  ><div align='left')>尊敬的" & GetAuthorizerName(dataObj.AuthorizerId)) 'AuthorizerCollection.All.GetById(dataObj.data.authorizerId).Key)

        builder.Append(
"您好,您授权使用的自用房、免费房的用户已经入住,下面是入住的详细信息。<br>")

        builder.Append(
" </div>")
        builder.Append(
" <br/>")
        builder.Append(
" <table width='384' border='0' cellspacing='2' cellpadding='2'>")
        builder.Append(
"<tr > ")
        builder.Append(
"<td colspan='3' align='left' ><strong>自用房、免费房入住用户信息: </strong></td>")
        builder.Append(
" </tr>")
        builder.Append(
" <tr> ")
        builder.Append(
" <td width='112' align='right'>&nbsp;</td>")
        builder.Append(
" <td width='251'></td>")
        builder.Append(
" <td width='1'>&nbsp;</td>")
        builder.Append(
"</tr>")
        builder.Append(
"<tr> ")
        builder.Append(
" <td width='112' align='right'>住客姓名:</td>")
        builder.Append(
"<td width='251'>" & dataObj.Name)
        builder.Append(
"</td>")
        builder.Append(
" <td>&nbsp;</td>")
        builder.Append(
"</tr>")
        builder.Append(
" <tr> ")
        builder.Append(
" <td width='112' align='right'>入住房间:</td>")
        builder.Append(
"<td width='251'>" & dataObj.RoomCode)
        builder.Append(
"</td>")
        builder.Append(
"<td>&nbsp;</td>")
        builder.Append(
" </tr>")
        builder.Append(
"<tr> ")
        builder.Append(
" <td width='112' align='right'>入住时间:</td>")
        builder.Append(
"<td width='251' align='left'>" & FormatDateTime(ModelLoad.NowTime, DateFormat.GeneralDate))
        builder.Append(
"</td>")
        builder.Append(
" </tr>")
        builder.Append(
"<tr> ")
        builder.Append(
" <td width='112' align='right'>预计离店时间:</td>")
        builder.Append(
"<td width='251' align='left'>" & FormatDateTime(dataObj.LeftTime, DateFormat.GeneralDate))
        builder.Append(
"</td>")
        builder.Append(
" </tr>")
        builder.Append(
" <tr> ")
        builder.Append(
"<td width='112' align='right'>入住原因:</td>")
        builder.Append(
"<td width='251'>" & GetAuthorizationReason(dataObj.AuthorizationReasonId))
        builder.Append(
"</td>")
        builder.Append(
" <td>&nbsp;</td>")
        builder.Append(
" </tr>")
        builder.Append(
" <tr > ")
        builder.Append(
" <td colspan='3' align='left' ><font size='1'>&nbsp;</font></td>")
        builder.Append(
"</tr>")
        builder.Append(
" <br/>")
        builder.Append(
"</table>")
        builder.Append(
"<table width='549' border='0' cellspacing='0' cellpadding='0'>")
        builder.Append(
"<tr>")
        builder.Append(
"<tr> ")
        builder.Append(
"<td> ")
        builder.Append(
"<div align=center> ")
        builder.Append(
"<p><br>")
        builder.Append(dataHotel.Name)
        builder.Append(
" </font> </p>")
        builder.Append(
" </div>")
        builder.Append(
"</td>")
        builder.Append(
"</tr>")
        builder.Append(
"</table>")

        builder.Append(
" </tbody> ")
        
'builder.Append("<tr><td>酒店服务电话:" & dataHotel.Telephone)
        'builder.Append("</td></tr>")
        'builder.Append("<tr><td>酒店地址:" & dataHotel.Address)
        'builder.Append("</td></tr>")
        builder.Append("<tr><td>" & FormatDateTime(System.DateTime.Now, DateFormat.ShortDate))
        builder.Append(
"</td></tr>")
        builder.Append(
"</table> ")
        builder.Append(
"</body> ")
        builder.Append(
"</html>")


        
Return builder.ToString()
    
End Function

    
''' <summary>
    
''' 离店邮件体
    
''' </summary>
    
''' <param name="dataObj"></param>
    
''' <returns></returns>
    
''' <remarks></remarks>

    
Public Function GetBodyHtmlToCheckOut(ByVal dataObj As GuestBase) As String
        ModelLoad.RefreshTimeParams()
        
Dim dataHotel As tHotelsBase = tHotelsCollection.All.curHotel
        
Dim builder As New System.Text.StringBuilder

        
Dim curAuthorizer As AuthorizerBase
        
If dataObj.AuthorizerId > 0 Then
            curAuthorizer 
= AuthorizerCollection.All.GetById(dataObj.AuthorizerId)
        
End If

        builder.Append(
"<html>")
        builder.Append(
"<head>")
        builder.Append(
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />")
        builder.Append(
"<title>")
        builder.Append(
"</title>")
        builder.Append(
"<style type='text/css'>")
        builder.Append(
"<!--")
        builder.Append(
"td {  font-family: '宋体'; font-size: 9pt}")
        builder.Append(
"-->")
        builder.Append(
"</style>")
        builder.Append(
"</head>")

        builder.Append(
"<body  bgcolor='#FFFFFF' text='#000000'>")
        builder.Append(
"<table width='549' border='0' cellspacing='0' cellpadding='0'>")
        builder.Append(
"<tr>")
        builder.Append(
"<td width='549'  ><div align='left')>尊敬的" & GetAuthorizerName(dataObj.AuthorizerId)) 'AuthorizerCollection.All.GetById(dataObj.data.authorizerId).Key)

        builder.Append(
"您好,您授权使用的自用房、免费房的用户已经离店,下面是离店的详细信息。<br>")

        builder.Append(
" </div>")
        builder.Append(
" <br/>")
        builder.Append(
" <table width='384' border='0' cellspacing='2' cellpadding='2'>")
        builder.Append(
"<tr > ")
        builder.Append(
"<td colspan='3' align='left' ><strong>自用房、免费房离店用户信息: </strong></td>")
        builder.Append(
" </tr>")
        builder.Append(
" <tr> ")
        builder.Append(
" <td width='112' align='right'>&nbsp;</td>")
        builder.Append(
" <td width='251'></td>")
        builder.Append(
" <td width='1'>&nbsp;</td>")
        builder.Append(
"</tr>")
        builder.Append(
"<tr> ")
        builder.Append(
" <td width='112' align='right'>住客姓名:</td>")
        builder.Append(
"<td width='251'>" & dataObj.Name)
        builder.Append(
"</td>")
        builder.Append(
" <td>&nbsp;</td>")
        builder.Append(
"</tr>")
        builder.Append(
" <tr> ")
        builder.Append(
" <td width='112' align='right'>离店房间:</td>")
        builder.Append(
"<td width='251'>" & dataObj.RoomCode)
        builder.Append(
"</td>")
        builder.Append(
"<td>&nbsp;</td>")
        builder.Append(
" </tr>")
        builder.Append(
"<tr> ")
        builder.Append(
" <td width='112' align='right'>离店时间:</td>")
        builder.Append(
"<td width='251' align='left'>" & FormatDateTime(ModelLoad.NowTime, DateFormat.GeneralDate))
        builder.Append(
"</td>")
        builder.Append(
" </tr>")

        builder.Append(
"<tr> ")
        builder.Append(
" <td width='112' align='right'>入住时间:</td>")
        builder.Append(
"<td width='251' align='left'>" & FormatDateTime(dataObj.ArrivedTime, DateFormat.GeneralDate))
        builder.Append(
"</td>")
        builder.Append(
" </tr>")

        builder.Append(
" <tr > ")
        builder.Append(
" <td colspan='3' align='left' ><p>&nbsp;</p><p> 附件为用户的消费汇总单。</p></td>")
        builder.Append(
"</tr>")
        builder.Append(
" <br/>")
        builder.Append(
"</table>")
        builder.Append(
"<table width='549' border='0' cellspacing='0' cellpadding='0'>")
        builder.Append(
"<tr>")
        builder.Append(
"<tr> ")
        builder.Append(
"<td> ")
        builder.Append(
"<div align=center> ")
        builder.Append(
"<p><br>")
        builder.Append(dataHotel.Name)
        builder.Append(
" </font> </p>")
        builder.Append(
" </div>")
        builder.Append(
"</td>")
        builder.Append(
"</tr>")
        builder.Append(
"</table>")

        builder.Append(
" </tbody> ")
        
'builder.Append("<tr><td>酒店服务电话:" & dataHotel.Telephone)
        'builder.Append("</td></tr>")
        'builder.Append("<tr><td>酒店地址:" & dataHotel.Address)
        'builder.Append("</td></tr>")
        builder.Append("<tr><td>" & FormatDateTime(System.DateTime.Now, DateFormat.ShortDate))
        builder.Append(
"</td></tr>")
        builder.Append(
"</table> ")
        builder.Append(
"</body> ")
        builder.Append(
"</html>")

        
Return builder.ToString()
    
End Function


   


End Class
Public Enum MailSubject As Integer

    CheckInSubject 
= 0
    RecoveryCheckInSubject 
= 1
    CheckOutSubject 
= 2

End Enum

 

 

 

调用方法

    asyncOneMail.BeginSendMailForOneCheckIn(itm.data, MailSubject.RecoveryCheckInSubject) 'author ysb data:2010-2-3                                  asyncOneMail.EndSendMailForOneCheckIn() 'author ysb data:2010-2-3 


 

posted on 2010-02-05 18:07  callbin  阅读(410)  评论(0编辑  收藏  举报