加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Outlook.Application发邮件

(2011-07-26 17:27:54)
标签:

杂谈

分类: .Net

   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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有