Ok. You may need to make some changes but this is what I have:-
Code:
Public Shared Sub SendAsync(ByVal email As EmailBOL, ByVal pcuser As Integer)
Dim smtp As New SmtpClient
Dim myMail As New MailMessage()
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
Dim arr_email_to As Array = Split(email.sentto, ";")
For Each email_to As String In arr_email_to
myMail.To.Add(New MailAddress(email_to))
Next
If Not String.IsNullOrEmpty(email.filepath) Then
For Each item As String In Directory.GetFiles(email.filepath)
Dim mail_attach As Attachment = New Attachment(item)
myMail.Attachments.Add(mail_attach)
Next
End If
'myMail.To.Add(txt_to.ToString)
myMail.From = New MailAddress(email.from)
myMail.IsBodyHtml = True
If email.style = "client" Then
myMail.Body = client_email_body(email.message)
ElseIf email.style = "adviser" Then
myMail.Body = adviser_email_body(email.message)
ElseIf email.style = "intro" Then
myMail.Body = client_email_body(email.message)
Else
myMail.Body = client_email_body(email.message)
End If
myMail.Subject = email.subject
If email.own Then myMail.Bcc.Add(email.from)
If email.read Then myMail.Headers.Add("Disposition-Notification-To", email.from)
If Not String.IsNullOrEmpty(email.cc) Then myMail.CC.Add(email.cc)
Dim mailBox As Object = myMail
AddHandler smtp.SendCompleted, AddressOf SmtpClient_OnCompleted
smtp.SendAsync(myMail, mailBox)
End Sub
Public Shared Function Send(ByVal email As EmailBOL, ByVal pcuser As Integer) As Boolean
Dim smtp As New SmtpClient
Dim myMail As New MailMessage()
If email.checkerror Then
Using myMail
Try
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
Dim arr_email_to As Array = Split(email.sentto, ";")
For Each email_to As String In arr_email_to
myMail.To.Add(New MailAddress(email_to))
Next
If Not String.IsNullOrEmpty(email.filepath) Then
For Each item As String In Directory.GetFiles(email.filepath)
Dim mail_attach As Attachment = New Attachment(item)
myMail.Attachments.Add(mail_attach)
Next
End If
'myMail.To.Add(txt_to.ToString)
myMail.From = New MailAddress(email.from)
myMail.IsBodyHtml = True
If email.style = "client" Then
myMail.Body = client_email_body(email.message)
ElseIf email.style = "adviser" Then
myMail.Body = adviser_email_body(email.message)
ElseIf email.style = "intro" Then
myMail.Body = introducer_email_body(email.message)
Else
myMail.Body = client_email_body(email.message)
End If
myMail.Subject = email.subject
If email.own Then myMail.Bcc.Add(email.from)
If email.read Then myMail.Headers.Add("Disposition-Notification-To", email.from)
If Not String.IsNullOrEmpty(email.cc) Then myMail.CC.Add(email.cc)
smtp.Send(myMail)
Catch ex As Exception
email.page_error = "Your email has not been sent<br />" & ex.ToString & "<br />" & email.sentto
End Try
End Using
Else
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
Dim arr_email_to As Array = Split(email.sentto, ";")
For Each email_to As String In arr_email_to
myMail.To.Add(New MailAddress(email_to))
Next
If Not String.IsNullOrEmpty(email.filepath) Then
For Each item As String In Directory.GetFiles(email.filepath)
Dim mail_attach As Attachment = New Attachment(item)
myMail.Attachments.Add(mail_attach)
Next
End If
'myMail.To.Add(txt_to.ToString)
myMail.From = New MailAddress(email.from)
myMail.IsBodyHtml = True
If email.style = "client" Then
myMail.Body = client_email_body(email.message)
ElseIf email.style = "adviser" Then
myMail.Body = adviser_email_body(email.message)
ElseIf email.style = "intro" Then
myMail.Body = client_email_body(email.message)
Else
myMail.Body = client_email_body(email.message)
End If
myMail.Subject = email.subject
If email.own Then myMail.Bcc.Add(email.from)
If email.read Then myMail.Headers.Add("Disposition-Notification-To", email.from)
If Not String.IsNullOrEmpty(email.cc) Then myMail.CC.Add(email.cc)
Dim mailBox As Object = myMail
AddHandler smtp.SendCompleted, AddressOf SmtpClient_OnCompleted
smtp.SendAsync(myMail, mailBox)
'smtp.Send(myMail)
End If
If String.IsNullOrEmpty(email.page_error) Then
Return True
Else
Return False
End If
End Function
Public Shared Function func_getfilename(ByVal str_file As String) As String
Dim arr_file As Array = Split(str_file, "\")
Return arr_file(UBound(arr_file))
End Function
Then the EmailBOL is:-
Code:
Namespace myApp.App.BOL
Public Class EmailBOL
Private _error As String = String.Empty
Public Property page_error() As String
Get
Return _error
End Get
Set(ByVal value As String)
_error = value
End Set
End Property
Private _emailref As Integer = 0
Public Property emailref() As Integer
Get
Return _emailref
End Get
Set(ByVal value As Integer)
_emailref = value
End Set
End Property
Private _checkerror As Boolean = True
Public Property checkerror() As Boolean
Get
Return _checkerror
End Get
Set(ByVal value As Boolean)
_checkerror = value
End Set
End Property
Private _visitorref As Integer = 0
Public Property visitorref() As Integer
Get
Return _visitorref
End Get
Set(ByVal value As Integer)
_visitorref = value
End Set
End Property
Private _introref As Integer = 0
Public Property introref() As Integer
Get
Return _introref
End Get
Set(ByVal value As Integer)
_introref = value
End Set
End Property
Private _sentby As Integer = 0
Public Property sentby() As Integer
Get
Return _sentby
End Get
Set(ByVal value As Integer)
_sentby = value
End Set
End Property
Private _priority As Integer = 0
Public Property priority() As Integer
Get
Return _priority
End Get
Set(ByVal value As Integer)
_priority = value
End Set
End Property
Private _completedby As Integer = 0
Public Property completedby() As Integer
Get
Return _completedby
End Get
Set(ByVal value As Integer)
_completedby = value
End Set
End Property
Private _from As String = String.Empty
Public Property from() As String
Get
Return _from
End Get
Set(ByVal value As String)
_from = value
End Set
End Property
Private _sentto As String = String.Empty
Public Property sentto() As String
Get
Return _sentto
End Get
Set(ByVal value As String)
_sentto = value
End Set
End Property
Private _cc As String = String.Empty
Public Property cc() As String
Get
Return _cc
End Get
Set(ByVal value As String)
_cc = value
End Set
End Property
Private _subject As String = String.Empty
Public Property subject() As String
Get
Return _subject
End Get
Set(ByVal value As String)
_subject = value
End Set
End Property
Private _message As String = String.Empty
Public Property message() As String
Get
Return _message
End Get
Set(ByVal value As String)
_message = value
End Set
End Property
Private _sentby_str As String = String.Empty
Public Property sentby_str() As String
Get
Return _sentby_str
End Get
Set(ByVal value As String)
_sentby_str = value
End Set
End Property
Private _sentby_email As String = String.Empty
Public Property sentby_email() As String
Get
Return _sentby_email
End Get
Set(ByVal value As String)
_sentby_email = value
End Set
End Property
Private _attach As String = String.Empty
Public Property attach() As String
Get
Return _attach
End Get
Set(ByVal value As String)
_attach = value
End Set
End Property
Private _filepath As String = String.Empty
Public Property filepath() As String
Get
Return _filepath
End Get
Set(ByVal value As String)
_filepath = value
End Set
End Property
Private _style As String = String.Empty
Public Property style() As String
Get
Return _style
End Get
Set(ByVal value As String)
_style = value
End Set
End Property
Private _sentitems As Boolean = False
Public Property sentitems() As Boolean
Get
Return _sentitems
End Get
Set(ByVal value As Boolean)
_sentitems = value
End Set
End Property
Private _own As Boolean = False
Public Property own() As Boolean
Get
Return _own
End Get
Set(ByVal value As Boolean)
_own = value
End Set
End Property
Private _read As Boolean = False
Public Property read() As Boolean
Get
Return _read
End Get
Set(ByVal value As Boolean)
_read = value
End Set
End Property
Private _datesent As Nullable(Of DateTime) = Nothing
Public Property datesent() As Nullable(Of DateTime)
Get
Return _datesent
End Get
Set(ByVal value As Nullable(Of DateTime))
_datesent = value
End Set
End Property
End Class
End Namespace
You can probably remove quite a few of the properties. Mine links into which users have sent the email and saves them in a sentitems db table. Most of the properties should make sense. The checkerror boolean toggles whether to send asynchronous or not. Basically if you set checkerror to false it sends asynchronously.
Hope that helps. Let me know if it needs explaining.
Bookmarks