I have code that essentially sends an email, it creates the email with a MIME HTML body and a Richtext PDF attachment. The weird part is, is that when the message is saved, both the HTML Body is there and the attachment at the top are both present.
When I send this out externally the PDF attachment is no longer there. If I comment out the HTML MIME part of the code the attachment sends attached at the top externally just fine.
Why is one preventing the other and how would I get both to send the same way the document is saved?
Any help would be appreciated.
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 RichTextHeader 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")
Set NDocument = NMailDb.CreateDocument
Set Nstream = NSession.CreateStream
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", "noreply@test.com")
If attachmentFile <> "" Then
Set AttachedOb = NDocument.Createrichtextitem("attachmentFile")
Set EmbedOb = AttachedOb.embedobject(1454, "", attachmentFile, "")
End If
Call Nstream.Open("Directory\HTML BODY.htm")
Set NBody = NDocument.CreateMIMEEntity '("memo")
Set RichTextHeader = NBody.CreateHeader("Content-Type")
Call RichTextHeader.SetHeaderVal("multipart/mixed")
Set MIMEDoc = NBody.CreateChildEntity()
Call MIMEDoc.SetContentFromBytes(Nstream, "text/html", ENC_IDENTITY_BINARY)
Call Nstream.Close
NDocument.savemessageonsend = True
Call NDocument.replaceitemvalue("PostedDate", Now())
Call NDocument.Send(False)
Set NDocument = Nothing
Set Nstream = Nothing
End If
Next i
End Sub
*Edited code Following Richards guidance
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 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("server 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")
Set NDocument = NMailDb.CreateDocument
Set Nstream = NSession.CreateStream
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", "noreply@test.com")
Set NBody = NDocument.CreateMIMEEntity
Call Nstream.Open("Directory")
Set MIMEDoc = NBody.CreateChildEntity()
Set Header = MIMEDoc.Createheader("Content-Type")
Call Header.SetHeaderVal("multipart/mixed")
Call MIMEDoc.SetContentFromBytes(Nstream, "text/html", ENC_IDENTITY_BINARY)
Call Nstream.Close
Call Nstream.Truncate
Call Nstream.Open("Directory" & File)
Set NChild = NBody.CreateChildEntity()
Set HeaderChild = NChild.Createheader("Content-Type")
Call HeaderChild.SetHeaderVal("multipart/mixed")
Set HeaderChild = NChild.Createheader("Content-Disposition")
Call HeaderChild.SetHeaderVal("attachment; filename=" & File)
Set HeaderChild = NChild.Createheader("Content-ID")
Call HeaderChild.SetHeaderVal(File)
Set HeaderChild = NChild.Createheader("Content-Transfer-Encoding")
Call HeaderChild.SetHeaderVal(base64)
Call NChild.SetContentFromBytes(Nstream, "application/pdf", ENC_BASE64)
Call Nstream.Close
NDocument.savemessageonsend = True
Call NDocument.replaceitemvalue("PostedDate", Now())
Call NDocument.Send(False)
Set NDocument = Nothing
Set Nstream = Nothing
End If
Next i
End Sub
Document now attaches as PDF below the HTML body but is blank.