Adding Attachment via NotesMIMEEntity causes Format loss!

I Have a working application, that sends emails through a VB.net Application using COM. I added new code I found to preserve the formatting using MIME and it works fine until I add an attachment. When I add an attachment the Bold and the Italics are lost and there is a blank line at the top of the email and the spaceing is odd??? Below is some code I created to test functionally. It works fine and is simple code, method 1 send without an attachment and method 2 send with an attachment. I tired a couple of different methods but always lose the format when there is a attachment? anybody have an Idea why?

Thanks in advance for any help!

HTML String:

This is Bold!
This is Italic!
This is Underline

Insert Here!

Thanks,

  • bullet

  • another Bullet




CODE:

Private Sub TestSendFormat(ByVal HTML As String)

    Dim Session As New NotesSession

    Dim DB As NotesDatabase

    Dim Doc As NotesDocument

    Dim rtitem As NotesRichTextItem

    Dim mailServer As String

    Dim mailFile As String

    Dim Body As NotesMIMEEntity

    Dim stream As NotesStream

    Dim bodyChild As NotesMIMEEntity

    Dim bodyParent As NotesMIMEEntity

    Dim header As NotesMIMEHeader

    Dim Filename As String



    Try





        Session.Initialize("Password")  '<- put the password in here

        mailServer = Session.GetEnvironmentString("MailServer", True)

        mailFile = Session.GetEnvironmentString("MailFile", True)

        DB = Session.GetDatabase(mailServer, mailFile) '<- put your server and database in here



        Doc = DB.CreateDocument



        Call Doc.AppendItemValue("From", Session.UserName)

        Call Doc.AppendItemValue("Subject", "Meeting time changed")

        Call Doc.ReplaceItemValue("SendTo", "test@test.com")



        If Method.Text = 1 Then



            Session.ConvertMime = False

            Body = Doc.CreateMIMEEntity("Body") ' MIMEEntity to support HTML

            stream = Session.CreateStream

            Call stream.WriteText(HTML) ' Write the body text to the stream 

            Call Body.SetContentFromText(stream, "text/html;charset=UTF-8", MIME_ENCODING.ENC_IDENTITY_7BIT) 'charset=UTF-8", charset=iso-8859-1",

            Call stream.Truncate()

            Session.ConvertMime = True



        Else



            Session.ConvertMime = False

            Body = Doc.CreateMIMEEntity("Body") ' MIMEEntity to support HTML

            'Child mime entity which is going to contain the HTML which we put in the stream

            bodyChild = Body.CreateChildEntity() 'CreateChildEntity()

            stream = Session.CreateStream

            Call stream.WriteText(HTML) ' Write the body text to the stream 

            ' Write the body text to the stream 

            Call bodyChild.SetContentFromText(stream, "text/html;charset=UTF-8", MIME_ENCODING.ENC_IDENTITY_7BIT)

            Call stream.Truncate()



            Filename = "C:\continentalbreakfast.pdf"

            'A new child mime entity to hold a file attachment         

            bodyChild = Body.CreateChildEntity()

            header = bodyChild.CreateHeader("Content-Type")

            Call header.SetHeaderVal("multipart/mixed")

            'Call header.SetHeaderValAndParams("multipart/related;boundary=""=NextPart_=""")

            header = bodyChild.CreateHeader("Content-Disposition")

            Call header.SetHeaderVal("attachment; filename=" & Filename)

            header = bodyChild.CreateHeader("Content-ID")

            Call header.SetHeaderVal(Filename)

            stream = Session.CreateStream()

            If Not stream.Open(Filename, "binary") Then

                MsgBox("Open failed")

            End If

            If stream.Bytes = 0 Then

                MsgBox("File has no content")

            End If

            Call bodyChild.SetContentFromBytes(stream, "", MIME_ENCODING.ENC_IDENTITY_BINARY)



            Doc.CloseMIMEEntities(True, "Body")



        End If



        Doc.SaveMessageOnSend = True



        Call Doc.Send(0)



        'Call Doc.Save(True, False)

        Session.ConvertMime = True



    Catch ex As Exception

        Debug.Print(ex.ToString)

    Finally

        stream = Nothing

        header = Nothing

        bodyChild = Nothing

        Body = Nothing

        bodyParent = Nothing

        DB = Nothing

        Session = Nothing

        'obj = Nothing

        rtitem = Nothing

        'richText = Nothing

        Doc = Nothing

    End Try



End Sub