Reputation: 259
I had some issues with code that I wrote to send 2 attachments via VBA, with some help I figured out how to use MIME to attach 2 different attachments. However now the issue seems to be that the HTML attaches as body fine but the PDF that I want as an attachment doesn't open and errors.
I sent a normal PDF attachment to see what Headers are produced to see if I could solve it. They do seem to differ but I'm not sure how I would achieve the same headers in code as any changes don't seem to produce the desired output in VBA.
Normal email headers for attachment -
--=alternative 002FD1AF802588B4=-- --=mixed 002FD1AF802588B4= Content-Type: application/octet-stream; name="Import Declaration Report (GGRFXT2208000000001019).pdf" Content-Disposition: attachment; filename="Import Declaration Report (GGRFXT2208000000001019).pdf" Content-Transfer-Encoding: base64
My automated send headers -
--==IFJRGLKFGIR38462UHRUHIHD Content-Type: application/octet-stream Content-Disposition: attachment; filename=Import Declaration Report (GGRFXT2208000000001019).pdf
--==IFJRGLKFGIR38462UHRUHIHD--
How in the hell do I get something similar, even though I have created a header for encoding it doesn't seem to do anything and I would really like to work this out.
Public Sub COM_Email_Send()
Dim NSession As Object
Dim NMailDb As Object
Dim NDocument As Object
Dim NBody As Object
Dim NChild As Object
Dim Nstream As Object
Dim Header As Object
Dim HeaderChild As Object
Dim NParent As Object
Dim NMime As Object
Dim i As Long
Dim Row As Long
Dim Recipient As String
Dim File As String
Dim attachmentFile As String
Dim Data As String
Dim AttachedOb As Object
Dim EmbedOb As Object
Dim NHeader As Object
Dim strFileType As Variant
Dim MIMEDoc As Object
Set NSession = CreateObject("Lotus.NotesSession")
Call NSession.Initialize("password")
Set NMailDb = NSession.GetDatabase("Directory", "Server")
If Not NMailDb.IsOpen = True Then
Call NMailDb.Open
End If
Row = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To Row
Recipient = Worksheets("Sheet1").Range("B" & i)
If Recipient <> "" Then
File = Worksheets("Sheet1").Range("A" & i).Value
attachmentFile = "Directory" & File
Data = Format(Now(), "dd/mm/yyyy")
NSession.ConvertMIME = False
Set Nstream = NSession.CreateStream
Set NDocument = NMailDb.CreateDocument
Call NDocument.replaceitemvalue("Form", "Memo")
Call NDocument.replaceitemvalue("SendTo", Recipient)
Call NDocument.replaceitemvalue("Subject", "Please see your clearance documents attached " & Data)
Call NDocument.replaceitemvalue("Sender", "[email protected]")
Set NMime = NDocument.CreateMIMEEntity("Body")
Set NParent = NMime.CreateParentEntity
Call Nstream.Open("Directory")
Set MIMEDoc = NParent.CreateChildEntity()
Call Nstream.WriteText(strBody)
Call MIMEDoc.SetContentFromText(Nstream, "text/html;charset=iso-8859-1", 1725)
Call Nstream.Close
Call Nstream.Truncate
Set MIMEDoc = NParent.CreateChildEntity()
Set HeaderChild = MIMEDoc.Createheader("Content-Disposition")
Call HeaderChild.SetHeaderVal("attachment; filename=" & File)
Set HeaderChild = MIMEDoc.Createheader("Content-Type")
Call HeaderChild.SetHeaderVal("application/pdf")
Set Nstream = NSession.CreateStream()
If Not Nstream.Open("Directory", "binary") Then
MsgBox ("Open Failed")
End If
Call MIMEDoc.SetContentFromBytes(Nstream, "application/pdf", 1727)
Call Nstream.Close
Call Nstream.Truncate
NDocument.savemessageonsend = True
Call NDocument.replaceitemvalue("PostedDate", Now())
Call NDocument.Send(False)
Set NDocument = Nothing
Set Nstream = Nothing
End If
Next i
NSession.ConvertMIME = True
End Sub
**EDIT
It looks like the SetContentFromBytes
is over-writting the Content-Transfer-Encoding
from ENC_BASE64
to ENC_NONE
since SetContentFromBytes
is supposed to set Encoding, I guess the question is how do I set BASE64 Encoding in SetContentFromBytes
as the above declaration doesn't work (Only other way I found was Domino.MIME_ENCODING.ENC_BASE64
which drops an error)??
***EDIT
Solved that issue by declaring it as the number, PDF still doesn't open however.
Upvotes: 1
Views: 105
Reputation: 259
Okay, so VBA only takes the number for encoding and not what is stated in the Lotus Notes Documentation. And also instead of Application/pdf as I had first thought it ended up being Application/octet-stream. In addition octet-stream isn't base 64, its binary so declaring it as Call MIMEDoc.SetContentFromBytes(Nstream, "application/octet-stream", 1730)
in the end solved it.
Upvotes: 1