Private Sub
Button1_Click(sender As System.Object, e As System.EventArgs)
Handles Button1.Click
Me.SendEmailToCS("PeterKong@techland.com.hk",
Nothing, "Test", "Dear All" + vbCrLf + "This is a testing email",
Nothing)
End
Sub
Public
Function SendEmailToCS( _
ByVal strEmailAddress As String, _
ByVal objCollCCAddress As Collections.ArrayList, _
ByVal strSubject As String, _
ByVal strBody As String, _
ByVal objSortedListPath As System.Collections.SortedList) As
Boolean
Dim objApp As Object = Nothing
Dim objNewMail As Object = Nothing
Try
Dim intMailItem As Integer = 0
Dim strFilePath As String = ""
Dim objObject As Object = Nothing
Dim intCounter As Integer = 0
Dim strCCAddress As String = ""
Dim strDefaultSignature As String = ""
' Create outlook object
objApp = CreateObject("Outlook.Application")
intMailItem = 0
objNewMail = objApp.CreateItem(intMailItem)
If (Not objApp Is Nothing) AndAlso (Not objNewMail Is Nothing)
AndAlso strEmailAddress <> ""
Then
' here we get default signature
objApp = objNewMail.GetInspector()
' build cc address
Dim intCounter_CC As Integer = 0
If objCollCCAddress IsNot Nothing Then
For Each objEntry As Object In objCollCCAddress
intCounter_CC += 1
strCCAddress += CType(objEntry, String).Trim()
If intCounter_CC < objCollCCAddress.Count Then
strCCAddress += ";"
End If
Next
End If
If Not objSortedListPath Is Nothing Then
' Get subledger code of first record to build mail subject
For Each objObject In objSortedListPath.Keys
intCounter += 1
If intCounter = 1 Then
strSubject += "(" + CType(objObject, String).Trim() + ")"
Exit For
End If
Next
' Check whether attanch file exists
' if not exist , return false, otherwise , attach
For Each objObject In objSortedListPath.Values
strFilePath = CType(objObject, String).Trim()
If Not System.IO.File.Exists(strFilePath) Then
Return False
Else
objNewMail.Attachments.Add(strFilePath)
End If
Next
End If
'Hide the CC Address
strCCAddress = ""
With objNewMail
' Ricky Remark:
' because default signature has format,picture...
' so must use HTMLBody here
strDefaultSignature = .HTMLBody
.Body = strBody
.HTMLBody += strDefaultSignature
.To = strEmailAddress
.Cc = strCCAddress
.Subject = strSubject
.Send()
'.Display()
End With
Else
Return False
End If
Return True
Catch objComEx As COMException
' Exception cause by COM , may be wrong email address.
Return False
Catch objE As Exception
Return False
Finally
' Release COM object
If objNewMail IsNot Nothing Then
Marshal.ReleaseComObject(objNewMail)
objNewMail = Nothing
End If
If objApp IsNot Nothing Then
Marshal.ReleaseComObject(objApp)
objApp = Nothing
End If
End Try
End
Function
加载中,请稍候......