Skip to content Skip to sidebar Skip to footer

Sending Email From Lotus Notes Using Excel And Having Attachment & Html Body

Right I'm trying to send an Email form an excel spreadsheet though lotus notes, it has an attachment and the body needs to be in HTML. I've got some code that from all I've read sh

Solution 1:

I have managed to solve my own problem.

In teh same way you create a MIME entry and stream in the HTML you need to do the same with the attachment, you also need to put them both inside a MIME entry within the email itself to hold both the HTML and Attachment at the same level otherwise you end up with a situation of the email with the body and a child entry of the attachment which is within another attachment. (it's odd but true) Thus this is my solution:

Sub Send_Lotus_Email(Addresses, Attach, strSubject, strBody)

'Declare VariablesDim s AsObjectDim db AsObjectDim body AsObjectDim bodyChild AsObjectDim header AsObjectDim stream AsObjectDim host AsStringDim message AsObject' Notes variablesSet s = CreateObject("Notes.NotesSession")
 Set db = s.CurrentDatabase
 Set stream = s.CreateStream

 ' Turn off auto conversion to rtf
 s.ConvertMIME = False' Create messageSet message = db.CreateDocument
 message.Form = "memo"
 message.Subject = strSubject
 message.SendTo = Addresses
 message.SaveMessageOnSend = True' Create the body to hold HTML and attachmentSet body = message.CreateMIMEEntity

'Child mime entity which is going to contain the HTML which we put in the streamSet bodyChild = body.CreateChildEntity()
 Call stream.WriteText(strBody)
 Call bodyChild.SetContentFromText(stream, "text/html;charset=iso-8859-1", ENC_NONE)
 Call stream.Close
 Call stream.Truncate

 ' This will run though an array of attachment paths and add them to the emailFor i = 0To UBound(Attach)
    strAttach = Attach(i)
    If Len(strAttach) > 0And Len(Dir(strAttach)) > 0Then' Get the attachment file name
        pos = InStrRev(strAttach, "\")
        Filename = Right(strAttach, Len(strAttach) - pos)

        'A new child mime entity to hold a file attachmentSet bodyChild = body.CreateChildEntity()
        Set header = bodyChild.CreateHeader("Content-Type")
        Call header.SetHeaderVal("multipart/mixed")

        Set header = bodyChild.CreateHeader("Content-Disposition")
        Call header.SetHeaderVal("attachment; filename=" & Filename)

        Set header = bodyChild.CreateHeader("Content-ID")
        Call header.SetHeaderVal(Filename)

        Set stream = s.CreateStream()
        IfNot stream.Open(strAttach, "binary") Then
            MsgBox "Open failed"EndIfIf stream.Bytes = 0Then
            MsgBox "File has no content"EndIfCall bodyChild.SetContentFromBytes(stream, "application/msexcel", ENC_IDENTITY_BINARY)' All my attachments are excel this would need changing depensding on your attachments.EndIfNext'Send the emailCall message.Send(False)

 s.ConvertMIME = True' Restore conversionEndSub

Solution 2:

Here is my actual code. I'm not even using strong type.

Dim mobjNotesSession AsObject' Back-end session reference'Dim bConvertMime AsBooleanDim stream AsObjectDim mimeHtmlPart AsObjectConst ENC_QUOTED_PRINTABLE = 1726

 mobjNotesSession = CreateObject("Lotus.NotesSession")
 mobjNotesSession.Initialize()

 mobjNotesDatabase = mobjNotesSession.GetDatabase("HQ2", "tim4")
 mobjNotesDocument = mobjNotesDatabase.CreateDocument

 bConvertMime = mobjNotesSession.ConvertMime
 mobjNotesSession.ConvertMime = False
 stream = mobjNotesSession.CreateStream()
 Call stream.WriteText(txtBody.Text)

 mobjNotesBody = mobjNotesDocument.CreateMIMEEntity("Body")
 mimeHtmlPart = mobjNotesBody.CreateChildEntity()  'This returns "Type Mismatch" error'Call mimeHtmlPart.SetContentFromText(stream, "text/html; charset=""iso-8859-1""", ENC_QUOTED_PRINTABLE)

 Call stream.Close()
 mobjNotesSession.ConvertMime = bConvertMime
 Call mobjNotesDocument.CloseMIMEEntities(True, "Body")

Solution 3:

No sorry I didn't i was running this in VBA which isn't strong typed so i can get away with not knowing the actual variable type for the body identity. i 've not been able to test this but I belive you need to reset the declarations to

Dim bodyChild As NotesMIMEEntity

this is the one you have trouble with the ones bellow you may find cause problems as well

Dim s AsNew NotesSession

Dim db As NotesDatabase 

Dim body As NotesMIMEEntity 

Dim header As NotesMIMEHeader 

Dim stream As NotesStream 

Dim host AsStringDim message As NotesDocument

Hope this helps

Post a Comment for "Sending Email From Lotus Notes Using Excel And Having Attachment & Html Body"